Harish Harish - 1 year ago 152
C Question

c string to specman e code

I have a c method that returns a

const char *
and I imported this function into my specman code. After executing few more statements in "e" the value in the string is getting corrupted. I guess may be because it is referring to a pointer in C space.

C signature:

const char* myFun(const char* key)
static string myVal;
myVal = myDictionary[key];
return myVal.c_str();

in e:

myFun(key : string) : string is foreign dynamic C routine

in e usage:

var str : string;
var str2 : string;
str = myFun("my_test");
outf("%s",str) ---> here it gives the correct value
str2 = myFun("my_test2");
outf("%s",str) ---> here it gives some garbage value, statements in the middle doesn't edit this string in anyway.

thoughts on what is wrong with this code?

Answer Source

In e strings are immutable, there is no legal way to change its contents. I think you need to look into your C code - it probably does some reuse to the memory of the string it sent to e in the previous call. If pointer to C string is passed to e and not immediately disposed in e code, it can be copied, as a precaution, with .copy(), for example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download