Link to home
Start Free TrialLog in
Avatar of manav_mathur
manav_mathur

asked on

conceptual pointers question

Hi,

excuse me if Im missing something here.....
but

char* retstring()
{
    char a[] = "Somestring" ;
    return a;
}
void main
{
    char* p=retstring() ;
    cout<< p;
}

prints garbage in my C++ compiler.

Please explain
Manav
Avatar of cjjclifford
cjjclifford

the local variable "char a[]" is created on the stack, and shouldn't be returned like this (G++/GCC warns about this...) since the contents of the stack could be changed between your call and the use (which is obvious in your example, as garbage is being output!)
If you must do something like this, use strdup() for the return string (not good, an easy source of memory leaks), or better yet pass in an char array (and length) and use strncpy() to fill the array (to the specified length) to get the string out....
Avatar of manav_mathur

ASKER

cjjclifford,
Can you please explain in a bit more detail. AFAIK, the compliler takes care of segregation of program stacks and data stacks. i.e, the function stack would be seperate from the data stack......

>contents of the stack could be changed between your call and the use
Can you give an example scenario so that it makes it clear??

Manav
cjjclifford,
Please explain in as much detail as you can, because I wont be able to access this site for 10 hours now........(have to go to sleep)

Manav
The local variable "a" is given memory space on function entry, and that space is reclaimed at function exit.   You're passing back the address of "a" to a place AFTER "a"'s space has been returned to the stack.  Outside of the function, that address is no longer of any use.

It's like going to a whorehouse, requesting Alice, using Alice, writing down her social security number on a piece of paper, releasing Alice, then going back home with the number.   Once you're home you can't use that SSN as a substitute for Alice.   :)

Hope this clears things up.



ASKER CERTIFIED SOLUTION
Avatar of cjjclifford
cjjclifford

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
grg99, I love that analogy!!!
A better analogy, you go there, the front desk says "You''re all set to use Alice in Room 3749", you do your business with Alice, then you exit the establishment.  

Once out of the place, you can't use the number "3749" for anything anywhere near as much fun (Obligatory PC note: or as DIsgusting, Expoitative, and just plain Jerky).

Avatar of Kent Olsen

Hi grg99,

Your mainframe background is showing.  :)

Which leads to the old CDC joke about the difference between a CPU and a PPU .....
>Which leads to the old CDC joke about the difference between a CPU and a PPU .....
Never heard that joke.   What's the punchline? :)

The only CDC tie I can see is if one told a joke that was in any way racy, people would say "Watch Out, 88", which was the number of the page in the company rule book about "Sexual Harrassment".

Oh, now I remember another CDC joke.  During the Crash of 1970, lots of people got the axe.  This black humor went around.
Manager calls in a female employee:  "I'm going to have to lay you or Jack off". "Well, you're not going to lay me, so ...."

 

Page 88, huh?  I don't have a company rule book anymore, but I do still have an old CDCNET directory.  :)

Well unfortunately, the punchline contains a couple of key words that I can't put in this forum.  :(

Sunny doesn't know any way for me to give up the punchline without getting banned from the site.

SIDKE,
Kent
Okay, that's a big enough hint.

Just read Augesten Burroughs book, and learned the advertising term for a dishwashing liquid commercial:

"Two C's in a K"


"Two C's", I can surmise.  K?
> "Two C's", I can surmise.  K?

  K = Kitchen

You may have to re-evaluate C given that bit of info.



'Kitchen' was all that I could come up with, but I can't make much sense of the expression.  I'm assuming that C is the same as the C in CPU in the joke above, which means that the expression could be used for any kitchen product.


char* retstring()
{
  char a[] = "Somestring" ;
  char *b;
  b=malloc(sizeof(char)*strlen(a)+1);
  strcpy(b,a);
  return b;
}
void main()
{
  char* p=retstring() ;
  printf("%s",p);
}

Otherwise better to write a function to take a pointer as argument retstring(char*);
Hi SaMuEl,

>char* retstring()
>{
>  char a[] = "Somestring" ;
>  char *b;
>  b=malloc(sizeof(char)*strlen(a)+1);
>  strcpy(b,a);
>  return b;
>}

Is this anything like:

char * strdup (const char* source);


:)

lol, yes, I can see a vague simalarity :D
Thanks for pointing that out Kdo
Please note this isnt totally thread-safe:

>  b=malloc(sizeof(char)*strlen(a)+1);
>  strcpy(b,a);

grg99, why not?  "a" is a local variable...
Despite grg99's analogy :) , I think cjjclifford has explained the scenario in his third mail itself.
KDo, samuel...thanx for the alternative solution.

DO I close this and accept cjjclifford's answer or somebody else still has to say a thing or two about the "topic in discussion" :) ??

Manav
grg99, why not?  "a" is a local variable...

Well, there's the obvious: it's not checking for NULL return from malloc().

And you're right, but people don't easily see the subtle requirement that "a" HAS to be local AND non-volatile AND never overriden by some #define or template AND somehow protected from access from other threads and processes and hardware.

One careless cut and paste and you have a hidden time bomb in your code.

I generally like to wear a belt AND suspenders, just my personal quirk.




>> I generally like to wear a belt AND suspenders, just my personal quirk.

A good argument was made against such redundancy in "Once Upon a Time in the West".  :)