CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Array deletion

  1. #1
    Join Date
    Aug 2004
    Posts
    138

    Array deletion

    What happens if you don't delete arrays that you've declared and defined? Is there any harm done to the memory of the computer?


    Another problem is that when I declare arrays as class members, everything works fine. But when I try to delete the arrays in the destructor of the class, the program runs but also gives an assertion error!!! Why?

  2. #2
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: Array deletion

    You're only supposed to delete arrays that you allocated memory for using new or new[]. Deleting static arrays (arrays not allocated with new or new[]) is undefined behaviour.
    Old Unix programmers never die, they just mv to /dev/null

  3. #3
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: Array deletion

    If U delete array (created with new operator !!!) u should do it that way:

    Code:
     int *pArray= new int [10];
    ...
    delete [] pArray;
    Best regards,
    Krzemo.

  4. #4
    Join Date
    Aug 2004
    Posts
    138

    Re: Array deletion

    Oh! I didn't know that I only need to delete static arrays, thank you.

  5. #5
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: Array deletion

    Oh! I didn't know that I only need to delete static arrays, thank you.
    It should be:
    Oh! I didn't know that I only need to delete nonstatic arrays, thank you.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Array deletion

    See this FAQ about memory leaks.

    Do not make the confussion between statically declared arrays
    Code:
    char array[10];
    and static arrays:
    Code:
    static char array[10]; //static statically declared array
    static char* parray = new char[..]; // static dinamically declared array
    When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).

    A variable declared static in a function retains its state between calls to that function.

    When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: Array deletion

    To cut it short... When you new you delete, when you new[] you delete[], and when you malloc you free. Other than that, don't worry about it!
    Insert entertaining phrase here

  8. #8
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Array deletion

    A vector (from the C++ standard library) is often better than an ordinary array. It has automatic memory management.

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Array deletion

    ...and in any case, no harm will ever occur to the memory of the computer, and in general even sloppy programs will not hold onto memory after the process completes... NOT advocating sloppy programming
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Join Date
    Feb 2005
    Posts
    3

    Re: Array deletion

    Quote Originally Posted by TheCPUWizard
    ...and in any case, no harm will ever occur to the memory of the computer, and in general even sloppy programs will not hold onto memory after the process completes... NOT advocating sloppy programming
    unless you could use up all the free mem... but that would have to be one big program...

  11. #11
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Array deletion

    Actually a run-away can easily exhaust the entire 2GB address space for the process. Depending on virtual memory limits, this may have a fatal impact on the execution of other processes, but still no harm will come to the memory
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured