Creating 2D arrays on heap

This is a discussion on Creating 2D arrays on heap within the C++ Programming forums, part of the General Programming Boards category; If we have to create a single dimension array on heap, and then deallocate the memory assigned to it, the ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    79

    Creating 2D arrays on heap

    If we have to create a single dimension array on heap, and then deallocate the memory assigned to it, the code would be somthing like this...
    Code:
    char *array=new char[50];
    //....
    delete array;
    Similarly I need to create a 2D array on heap, and deallocate the memory using double pointers. Please help me out.

  2. #2
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Am I going crazy? Didn't we just have a thread just like this a few days ago?

  3. #3
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380

  4. #4
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380

    Re: Creating 2D arrays on heap

    Originally posted by sundeeptuteja
    Code:
    char *array=new char[50];
    //....
    delete array;
    Should actually be
    Code:
    char *array=new char[50];
    //....
    delete[] array;

  5. #5
    elad
    Guest
    you could search the board here. There are numerous posts on this topic. In general the rule is you need to use the delete [] command if you use the new[] command.

    char * array;
    //this may be a pointer to a single char or represent an array of
    //char. we can't say on the basis of this information alone

    array = new char[80];
    //now we can say it is an array of 80 char

    delete [] array;
    //here we delete the memory for the 80 char in the array but the pointer, array still exits and isn't pointing at anything now. That is dangerous. Unless your program is closing immediately after the deletion you should assign NULL to array as below to stop array from dangling.

    array = NULL;

    Since a 2d array is an array of arrays you can do this:

    char ** array;
    //can't say if this is a pointer to a pointer to a single char or a 2D array yet

    int x = 90;
    int y = 33;

    array = new char * [x];
    /*Now there are 90 pointers to char arrays in array. But we have no idea what size each of the char arrays in array are. So we have to declare memory for each of the arrays, and each array must have the same size memory. Therefore using a loop seems reasonable:
    */

    int i;
    for(i = 0; i < x; i++)//do the body of this loop x times
    {
    array[i] = new char[y];
    //each pointer in array is assigned memory for an array of 33 char.
    }

    and to delete the memory allocated we reverse the process:

    for(i = 0; i < x; i++)
    {
    delete [] array[i];
    //delete the memory for each 33 char array in array first
    }
    //then delete the memory to the array of 90 pointers
    delete [] array;

    //again array still exists after the deletions. Only the memory pointed to by array has been deleted. So NULL it out if your program isn't stopping immediately.

  6. #6
    sundeeptuteja
    Guest
    Elad, I tried your method. My problem is that everytime I try to use this technique to create an array of strings, and then try to sort the strings in alphabetical order, I get an access violation error. Why is that?

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by sundeeptuteja
    Elad, I tried your method. My problem is that everytime I try to use this technique to create an array of strings, and then try to sort the strings in alphabetical order, I get an access violation error. Why is that?
    1-One reason could be that you tried to access an index that did not have a string in it.
    2-another reason could be that you have gone past the boundaries of the array of strings.
    3-finally, you could have just overwritten the string. are you accounting for null termination when you allocate them?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-25-2009, 09:29 PM
  2. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 10:18 AM
  3. Passing 2d arrays to a function
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 07-18-2005, 09:30 AM
  4. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 04:03 AM
  5. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 08:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21