Re: About c++ pointer

From: abcd (abcd_68_at_yahoo.co.uk)
Date: 11/22/04


Date: 22 Nov 2004 12:51:30 -0800

Andrey <stingo000@yahoo.ca> wrote in message news:<rzgod.296877$Pl.79890@pd7tw1no>...
> zhou xiang wrote:
> [...]
> > Q: What is the difference between code 1 and code 2?
> The code 1 will not work, the pointer will be empty when it returns,
> because the object(string array) is created in stack. While the second
> is fine since you dynamically allocate memory in the heap.

Nope. In neither case is the memory allocated off the heap (I can't
see any 'new' or 'malloc'). In both cases the string is statically
allocated in read-only memory (try to change it and the program will
likely crash e.g., *p='f').

The difference between the two is that in the first listing p is an
actual array, allocated on the stack. The contents of the constant
string is copied into it. It will be destroyed as soon as the flow of
control leaves function GetString(). This means that you're returning
the address of a local (and a clever compiler issues a warning). The
second listing is fine, not because you're allocating memory off the
heap (which you're not) but because you're returning a pointer to a
statically allocated string which will be there as long as the program
runs.

HTH
Andy



Relevant Pages

  • Re: Advice on how to return a list of values
    ... The caller passes a pointer to a previously allocated array of 64 ... My function allocates the array and returns it. ... well allocate that much space and be done with it. ...
    (comp.lang.c)
  • Re: Alignment requirements of LZO working memory
    ... The calculation done to compute the number of elements in the array ... same alignment question gets raised again if you allocate an ... One pointer to an allocated ... one trivial fixup and you get another pointer to an aligned ...
    (comp.compression)
  • Re: null terminated strings
    ... allocate an array dynamically based on some variable. ... pointer to the first byte in that blob. ... to the connection specific context and can update it. ...
    (comp.os.vms)
  • Re: Initialization of character array in derived type
    ... In fact, if the array is large enough for the time to be measurable, ... be faster than doing it with static initialization. ... I see that your init-mat_data is a pointer. ... allocate or assign the pointer somewhere. ...
    (comp.lang.fortran)
  • Re: Does C guarantee the data layout of the memory allocated by malloc?
    ... The C standard guarantees that the compiler will make things work so that person_t is big enough that you can allocate CNT of the structures sequentially with mallocand the result is equivalent to an array of such structures. ... it says "A pointer is not just a pointer, but a pointer with some particular data type". ... OTOH, if the base type is 'float', then the address will be incremented by 4 bytes == 4, which is also conventional), and if the base type is a 256-byte structure, then incrementing the pointer by 1 will increment the address by 256. ...
    (comp.lang.c.moderated)