Thread: Deallocating a character pointer array

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Deallocating a character pointer array

    I'm a noob to C++ so please excuse my ignorance.

    I declared a character pointer array as follows and initialized it.

    Code:
    char* c[30];
    
    for(i=0; i<30; i++)
       c[i] = new char[20];
    I tinker around with it for a while, then I try to deallocate its memory.

    Code:
    delete [] c;
    Somehow, this would cause an assertion failure (I'm using VC++ 2003).

    So I tried the following
    Code:
    delete [] *c;
    There was no error with this one, but it only ended up deallocating the first element of the array, and none of the other 29 elements were affected.

    Is there another way to free up memory for character arrays (or do I have to iterate through each element and delete it)?

    One final question. I initialized each char pointer to have 20 characters. But when I check on it in runtime, it would always have 80 characters (with one or two exceptions).

    I tried switching the numbers to 5, 10, 30, whatever, and the length of the character array is always 80. Is there any way I can determine this behavior?

    Thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    delete [] c;
    That won't work. You need to free it in a for loop:
    Code:
    for(x = 0; x < 30; x ++)
        delete [] c[x];
    [edit]
    Code:
    delete [] *c;
    is the same as
    Code:
    delete [] c[0];
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    Do exactly the same, only in reverse

    for(i=0; i<30; i++)
    c[i] = new char[20];


    for(i=0; i<30; i++)
    delete [] c[i];

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Beat you.

    ... when I check on it in runtime, it would always have 80 characters (with one or two exceptions).
    Are you sure? You can't easily check the size of the allocated memory.

    [edit]
    Is there another way to free up memory for character arrays (or do I have to iterate through each element and delete it)?
    That's how you allocated it. That's how you have to free it.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    I don't know if this is the same as checking the size of allocated memory, but I tried printing out the following in my code:

    Code:
    printf("The address offset between %p and %p is %d", c[i], c[i+1], c[i+1]-c[i]);
    And the offset was usually 80 (with one exception of 3000 something)

  6. #6
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    > And the offset was usually 80 (with one exception of 3000 something)
    You can't compare the two results from two separate new() calls and get any kind of meaningful (ie portable) answer.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That just prints the difference between the addresses of the allocated pointers. They aren't guarenteed to be contiguous. (In fact, they usually aren't.) You can assume that new allocated whatever size you passed it (unless new returns NULL).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Okay, got that. Off to the next task, then.

    Thanks a lot, folks.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by cunnus88
    I don't know if this is the same as checking the size of allocated memory, but I tried printing out the following in my code:

    Code:
    printf("The address offset between %p and %p is %d", c[i], c[i+1], c[i+1]-c[i]);
    And the offset was usually 80 (with one exception of 3000 something)
    Try this:

    printf("The address offset between %p and %p is %d", &c[j][0], &c[j][29], &c[j][29] - &c[j][0]);
    Last edited by 7stud; 11-12-2005 at 11:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character array pointer
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-13-2007, 11:28 PM
  2. Help with character array pointer and strcmp
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-13-2007, 08:47 PM
  3. Pros pls help, Pointer to array
    By kokopo2 in forum C Programming
    Replies: 7
    Last Post: 08-17-2005, 11:07 AM
  4. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 07:23 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM