I think that I am having problems with the scope of variables. (Using GNU C++ on Solaris)
Let me summarise the code :
main()
{
char *ptr;
my_cls *problem;
ptr=func();
problem=new my_cls;
printf("--%s--",ptr);
}
char *func()
{
string temp;
char *returnptr;
temp="Hello";
temp=temp+" World";
returnptr=temp.c_str(); // Returns char* from string
return(returnptr);
}
This simple example shows what I am doing - a function returns a pointer to char.
When the processing returns to the calling main() function, the pointer 'ptr' is not pointing to anything, as the 'temp' which was local to func() has been deleted and the memory freed (I am assuming).
Someone must have seen this happen before? Is there a simple cure to this problem?
Let me summarise the code :
main()
{
char *ptr;
my_cls *problem;
ptr=func();
problem=new my_cls;
printf("--%s--",ptr);
}
char *func()
{
string temp;
char *returnptr;
temp="Hello";
temp=temp+" World";
returnptr=temp.c_str(); // Returns char* from string
return(returnptr);
}
This simple example shows what I am doing - a function returns a pointer to char.
When the processing returns to the calling main() function, the pointer 'ptr' is not pointing to anything, as the 'temp' which was local to func() has been deleted and the memory freed (I am assuming).
Someone must have seen this happen before? Is there a simple cure to this problem?