Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

how to delete the array in c++

Question posted by: karthigeyantp (Newbie) on July 24th, 2008 07:33 AM
hi, i have declared array at 1st ,in middel of the program i want to delete the array how to do it.
arnaudk's Avatar
arnaudk
Needs Regular Fix
330 Posts
July 24th, 2008
07:40 AM
#2

Re: how to delete the array in c++
Like this:
Expand|Select|Wrap|Line Numbers
  1. a = new int[5]; // Declare an array
  2. delete [] a;   // Delete an array
  3. a = 0;      // Clear a to prevent using invalid memory reference

If you declared it on the stack: int a[5], you can not delete it; its memory will be freed when it falls out of scope. Read this.

Reply
karthigeyantp's Avatar
karthigeyantp
Newbie
20 Posts
July 24th, 2008
08:52 AM
#3

Re: how to delete the array in c++
Quote:
Like this:
Expand|Select|Wrap|Line Numbers
  1. a = new int[5]; // Declare an array
  2. delete [] a;   // Delete an array
  3. a = 0;      // Clear a to prevent using invalid memory reference

If you declared it on the stack: int a[5], you can not delete it; its memory will be freed when it falls out of scope. Read this.



i have decleared it in this way position = new int [300]
it is displaying undecleard position. what is wrong?..

Reply
arnaudk's Avatar
arnaudk
Needs Regular Fix
330 Posts
July 24th, 2008
08:58 AM
#4

Re: how to delete the array in c++
You are assigning something to position without having declared it yet. Since position is a pointer to an int, you should declare it as such before using it: int * position;
If a is not declared yet, line 1 should read: int * a = new int[5];

Reply
Reply
Not the answer you were looking for? Post your question . . .
190,472 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top C / C++ Forum Contributors