delete 2D array - Created Dynamically


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 23

Thread: delete 2D array - Created Dynamically

  1. #1
    Join Date
    Oct 2004
    Location
    India
    Posts
    34

    Question delete 2D array - Created Dynamically

    hello friends

    I had created a 2D array dynamically and it works fine but when i want to delete it it crash and exited from system.



    COLORREF **temp ; //// Declared as public variable of class
    //mHeight and mWidth are also Public and Assigend values
    temp = new COLORREF* [mHeight];
    for (i = 1; i < mHeight; i++)
    {
    temp[i] = new COLORREF[mWidth];
    }

    I had written this code in Destructor
    for(int i=2;i<=mHeight;i++)
    {
    delete [] temp[i];
    }
    delete [] temp;
    TO LOVE YOU HAVE TO HATE FIRST

    Jitendra Sanghani
    Wipro Technologies
    Pune - India
    jitendra.sanghani@gmail.com

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    loop over width .. thats probably it. But even if they are equal (wide and high), you will crash because of the <= in the loop. Should be <

    you should use from 0...N -- you allocate from 1--N for some reason, and then you deallocate from 2--N for some reason. Confusing and stuff.

  3. #3
    Join Date
    Oct 2004
    Location
    India
    Posts
    34
    Thanks Jonin
    I get your point but my programm crash even when i am looping over 1 to n--.
    the program crash on first loop only so it doesn't had ANy problem with <= or <.
    TO LOVE YOU HAVE TO HATE FIRST

    Jitendra Sanghani
    Wipro Technologies
    Pune - India
    jitendra.sanghani@gmail.com

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    My best advice is: don't. Don't create 2 dimensional arrays using new. Maintaining such code is a nightmare and a fertile source of bugs, as you may have witnessed already. If you can allocate the array statically:

    COLOREF elements[mWidth][mHeight];

    It would be the best solution. If your compiler supports C99 extensions, this should be possible even though mWidth and mHeight aren't constants (for further information about variable length arrays, see my latest 10 Minute Solution: http://www.devx.com/cplus/10MinuteSolution/27219 ). If your compiler doesn't support this, you can use a one dimensional array:

    COLOREF *pelements= new COLREF[mWidth*mHeight];

    Then create a summy pointer to pointer:

    COLOREF ** pp= &pelements;

    Now you can use the double subscript notation with pp:

    pp[0][0]=foo;

    pelements[0][1]=bar;

    deleting such an array is a cinch:

    delete [] pelemenets; //don't delete pp!
    Last edited by Danny; 02-23-2005 at 07:14 AM.
    Danny Kalev

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    did you replace the variable? you allocate width and deallocate height. That and the < should cure it.
    And do what Danny said, I have not used a 2-d array like this since college, its too slow and too much trouble.

  6. #6
    Join Date
    Mar 2005
    Posts
    67

    Same problem with delete[]

    Hi there,

    I am having the similar problems when using delete [] on dynamic created arrays:

    Here is my code in a nutshell:

    Code:
    int index;
    int max_records[2]; 
    char **aa[2];
    max_records[0] = Variable1 // int Variable1 is assigned at runtime
    max_records[1] = max_records[0];
    aa[0] = new (char * [max_records[0]]);
    aa[1] = new (char * [max_records[1]]);
    
    
    for(index = 0; index != max_records[0]; index++) {
    
    char *a = new char[Variable2]; // int Variable2 is assigned at runtime
    aa[0][index] = a;
    strcpy(a, (char *) Variable3); // Variable3 previously defined and initialized
    a = new char[Variable4]; // int Variable4 is assigned at runtime
    aa[1][index] = a;
    strcpy(a, (char *) Variable5); // Variable5 previously defined and initialized
    
    }
    
    
    // The problem begins when try to delete [] arrays created dynamically:
    
    for(index = 0; index != max_records[0]; index++) {
    delete [] aa[0][index];
    delete [] aa[1][index];
    }
    Why cannot delete[] accomplish this?

    I appreciate some help.

    Thanks!
    Last edited by FededS; 02-03-2008 at 05:35 PM.

  7. #7
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    seems you r using Visual Studio 6 .. rn't u ?
    it's some bug with "char" pointers ... in this version of VS ... try to cast them to void first :

    delete[] (void*)aa[0][index];

    or better use an iso standard compiler .. try netbeans with g++ compiler it's very good .
    Last edited by Amahdy; 02-04-2008 at 10:56 AM.
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  8. #8
    Join Date
    Mar 2005
    Posts
    67
    Thanks Amahdy. I am using VS 6 but this does not work and it is not right I presume to cast a pointer to overpass this issue.

    I´ve seen previous posts regarding the delete [] on mutidimesional arrays, my question is why does delete[] fall short to accomplish this deallocation and is there any workaround to deallocate multidimensional arrays allocated with operator new[].

    Thanks

  9. #9
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    I just tested this now and seems working (did you see my post after edit ? I put a void* ) ..
    using this method is just a way for a bug in VS6 with char pointers ..I discussed this problem here in this forums and casting to void was Danny's idea and I think it hasn't any problem ... (because we cast to void nothing else)

    your method is correct and if you test it with g++ or with int pointer will work too ... the problem with VS C++ compiler that it still consider this as string :
    char *x = new char ;
    here if you write "cout<<x" the output will be a string ending with the first NULL character in the memory after x's address ...
    the problem with delete[] x is that it doesn't recognize how to delete a string as "x" for it is a string not a pointer !! that's the bug actually so we cast to void so that x is considered a pointer again and delete can accept it ..
    and as you can see there is not a compiling error but run-time error .. because this is accepted in c++ language and should be compiled correctly .. suddenly the vs compiler compile it incorrectly ..
    hope this can help ..
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  10. #10
    Join Date
    Mar 2005
    Posts
    67
    Amahdy, I have changed

    delete [] aa[0][index];

    to

    delete [] (void *) aa[0][index];

    and it crashes at runtime, I am using Visual C++ 6. I wanted to use new[] and delete [] for the sake of simplicity, do not want to go into templates. Just was wondering if there is a workaround through this delete [] problem.

  11. #11
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    I don't know but I'm closly sure this should works.. I can just ask if you r sure about the error is in the delete ? maybe inside the previous for loop ? test the code without the "strcpy" for example ?? I don't know what are you doing exactly I have just tested the code by creating the 2 of [2 dimentions pointers] ... then initialized them throw a loop then deleting them throw another loop and seems every thing is ok and hasn't any problems ..
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  12. #12
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    FYI: (maybe you know that) ... you have to delete the pointers after the final for loop :

    delete[] aa[0];
    delete[] aa[1];

    ..
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  13. #13
    Join Date
    Nov 2003
    Posts
    4,118
    First of all, check your strcpy() call. It might be corrupting the heap.
    Secondly, the rule about delete[] is simple: if you used new[]. you must use delete[] for the same pointer.
    Also, what does
    max_records[0] = Variable1
    stand for? What's Variable1 exactly?
    This stuff is particularly ominous:
    aa[0] = new (char * [max_records[0]]);
    You should try this instead:
    aa[0] = new char [sizeof(max_records[0])];
    and then delete it like this:
    delete[] aa[0];
    Danny Kalev

  14. #14
    Join Date
    Mar 2005
    Posts
    67
    Thanks Danny. The problem was indeed in the strcpy(), I was corrupting the heap. I compile the code without the void * casting and worked perfectly.

    Just to summarize I would like to know in which cases does the delete[] doesn´t work to deallocate multdimensional arrays? For example, if you dynamically create an arrays of pointers to char and you assign each pointer a dynamically created arrays of chars, should delete[] work?

    Example:

    char **pointer = new char *[2];
    pointer[0] = new char[5];
    pointer[1] = new char[4];

    delete[] pointer[0]; // Works??
    delete[] pointer[1]; // Works??
    delete[] pointer; // Works??


    Thanks!

  15. #15
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    Ok great your version is "visual studio 6 professional edition " ? and do you have any extra fixes or updates for it after "SP6" ?? .. this is the exact version here and it crashes since years because it can't delete char pointers, the "void" was a very good solution i though ...

    anyway looking for Danny explication just wanna say that actually you need all the THREE delete to delete this pointer from the memory ...
    *informally : each new correspond delete
    *specifically:
    -- you have created 2 pointers dynamically "pointer[0]" and "pointer[1]" >> you need to delete pointer >> delete[] pointer.
    -- you have created dynamic array for the pointer "pointer[0]" >> you need to delete it >> delete pointer[0] . and so on ... but of course as the order in your post you should delete "sub-pointers" first then the array of pointers itself .
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center