1

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?

Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
Harish
  • 23
  • 5
  • 1
    Show the C function as MCVE that exhibits the problem. – Weather Vane Oct 18 '16 at 18:59
  • added the code for c function @WeatherVane – Harish Oct 19 '16 at 05:25
  • You know by declaring `static string myVal;`, `myVal` is preserved across the various calls to `myFun`, Each time your return it, you are returning the ***same*** pointer. I suspect your are just overwriting the same string each time `myFun` is called. Remove the `static` and pass `string myVal` as a parameter. (need the full code to help further) – David C. Rankin Oct 19 '16 at 06:17
  • @DavidC.Rankin : if that is the case, I shud see same values for both str and str2, which is not the case :( – Harish Oct 19 '16 at 06:51
  • 1
    It's hard to tell with what you have posted. It appears you are using some type of copy constructor with `myVal = myDictionary[key];` If that is so, then the memory for `myVal` is being overwritten on each call to `myFun`. If you post a ***MCVE*** we can help further (and not be guessing) See: [**How to create a Minimal, Complete, and Verifiable example**](http://stackoverflow.com/help/mcve). – David C. Rankin Oct 19 '16 at 07:00

2 Answers2

2

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.

Rodion Melnikov
  • 338
  • 1
  • 4
1

In general, it's not a good idea to pass C strings directly to e, because such strings won't be handled properly by Specman's memory management mechanisms such as garbage collection, even if there is no reuse of the memory by the C code itself.

While using .copy() is indeed a possible solution, a different solution is to use SN_STRING_COPY macro inside the C function, which copies a string and allocates memory to the new string with Specman memory allocation mechanism. But this will be only relevant if the C function is being written with interfacing to e in mind, rather than being a generic C utility unaware of e.

Yuri Tsoglin
  • 963
  • 4
  • 7