Re: About c++ pointer

From: Cheng Mo (mo.cheng_at_nospam.nospam)
Date: 11/24/04


Date: Wed, 24 Nov 2004 10:51:38 +0800
To: abcd <abcd_68@yahoo.co.uk>

abcd wrote:
> 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
Perhaps your words is a little misleading.
The statement "char p[] = "hello world";" equals "char p[] =
{'h','e','l','l','o',' ','w','o','r','l','d','\0'};"
It is just a initializtion of a char array. No "copy" is performed.

> 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: performance question related to BST traversal
    ... Suppose I have to traverse the BST by Preorder, say, without using ... Suppose I use a stack ... Instead of an array of nodes, I use a singly linked list. ... allocate an array of nodes and link them into a free list. ...
    (comp.lang.c)
  • Re: Stack or Heap
    ... that often allocated on the stack. ... on an array then the compiler *might* attempt to allocate a temporary ... array result off of the stack to hold the result. ... is no such protection for C programmers using gcc. ...
    (comp.lang.fortran)
  • Re: On VLAs and incomplete types
    ... from a stack. ... The real advantage, though, comes with code like your 2D array ... allocate and deallocate memory, so there must be an explanation for ... The generation of dynamic memory is more complicated and requires ...
    (comp.lang.c)
  • Re: Linked List & Dynamic Memory Allocation
    ... Although it seems kind of pointless given that these items are already in an array. ... typdef struct node ... I am trying to dynamically allocate memory if user chooses to enter ...
    (microsoft.public.vc.mfc)
  • Re: Stack or Heap
    ... are allocated on the stack whereas allocated variables ... that often allocated on the stack. ... on an array then the compiler *might* attempt to allocate a temporary ... array result off of the stack to hold the result. ...
    (comp.lang.fortran)