2

i have a problem regarding the output of char ptr. following is the sample code which i am trying to execute..

struct DeviceInfo{

char * name;
int id;

};
void testFunc(DeviceInfo *info){

    char temp[50] = "test input";
    info->name = temp;

}
void main(){

    DeviceInfo deviceInfo;
    testFunc(&deviceInfo);
    std::cout<<"Output is "<<deviceInfo.name;


}

Output i get in the main is some kind of weird one .... while debugging i find out that when "test.name" is passed on to output stream it not only gives garbage output but it also changes the "name" value for "deviceInfo" object to that garbage value... this is kind of test scenario but in actual application this testFunc has to be executed in the same way i.e. constant char string should be assigned name to char ptr.

i tried puting '\0' at the end of char temp like this temp[strlen(temp)] = '\0';

but it still dont work

Any help is appreciated...

Raza

mwigdahl
  • 16,268
  • 7
  • 50
  • 64
Asim Raza
  • 21
  • 3
  • Maybe temp[50] is going out of scope and then `name` is pointing to deallocated memory. Trying putting the `cout` inside of testFunc and see what happens. – Nic Foster Jan 23 '12 at 15:22
  • If I remove the "temp" variable, it works. I think your temporary "temp" array is getting deallocated when the function returns. –  Jan 23 '12 at 15:23
  • possible duplicate of [Can a local variable's memory be accessed outside its scope?](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – Mike Seymour Jan 23 '12 at 15:43

6 Answers6

5

This is failing because you are assigning a pointer to an automatic variable to a member of a structure with a different scope (and therefore a different lifetime). temp is going to get destroyed at the end of testFunc, and further access to it is illegal.

mwigdahl
  • 16,268
  • 7
  • 50
  • 64
4

temp is a stack-allocated variable, it will get destroyed after testFunc() returns.

So your char* will point to destroyed memory.

This causes undefined behavior.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
4

This is why we have a string class.

struct DeviceInfo{
    std::string name;
    int id;
};

void testFunc(DeviceInfo *info){    
    info->name = "test_input";
}

int main(){
    DeviceInfo deviceInfo;
    testFunc(&deviceInfo);
    std::cout<<"Output is "<<deviceInfo.name;
}
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
3

You are storing a pointer to local storage, that disappears when testFunc() goes out of scope.

The proper way for a constant string is:

info->name = "test input";

or for something more dynamic:

info->name = strdup("test input");

You can also opt to not make name into pointer, but instead have char name[50]; in the structure, and then just do:

strcpy(info->name, "test input");

It would also be advisable to use a size-aware version, if you have it:

snprintf(info->name, sizeof info->name, "%s", "test input");

Note that this assumes the array solution, sizeof info->name will not make sense if name is still a pointer.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • If you use `strdup`, remember to `free` the memory once you're done. I'd suggest `std::string` instead, unless there's a good reason for manual memory management. – Mike Seymour Jan 23 '12 at 15:48
  • Well using char name[50] solves the problem .... :) but i just need to know why it was problematic with char ptr ??? does it due to the reason that char ptr has no longer access to local temp variable as it is being destroyed after the exit of testFunction??? – Asim Raza Jan 23 '12 at 15:49
1

You are assigning a pointer to a temporary array. Once testFunc() returns, your temp array will be destroyed and your info->name pointer will be pointing to random memory.

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
0
char *temp = "test input";
info->name = (char*)malloc(sizeof(char) * strlen(temp) + 1); // Include room for \0    
strcpy(info->name, temp);

instead of:

char temp[50] = "test input"; 
info->name = temp; 

Your char temp[50] is no longer valid after the function returns.

poy
  • 10,063
  • 9
  • 49
  • 74