473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how to delete the array in c++

hi, i have declared array at 1st ,in middel of the program i want to delete the array how to do it.
Jul 24 '08 #1
3 19328
arnaudk
424 256MB
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
  4.  
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.
Jul 24 '08 #2
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
  4.  
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?..
Jul 24 '08 #3
arnaudk
424 256MB
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];
Jul 24 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Shea Martin | last post by:
Which of the following do I use delete instead of just delete. //1.) // not sure about this one, as char is of size 1 char *str = new char; //2.) //not sure about this one, as it is a...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
15
by: Roy Smith | last post by:
I understand that "delete xp" deletes a scalar object and "delete xp" deletes an array of objects, but what I don't understand is why you need to tell the compiler which you're doing. When you...
10
by: felixnielsen | last post by:
Hi, im pretty new to this so plz cut me some slag ;-) anyhow, i need to delete an array, or what i really need is to increase the array size, i was think i could be done by copying the array into...
6
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
6
by: valerij | last post by:
Please help. I have been on this problem for a week now. I am using Windows 98SE, Microsoft Visual C++ 6.0 The following program only works when the function is not called, BUT the function does...
12
by: yufufi | last post by:
Hello, How does delete know how much memory to deallocate from the given pointer? AFAIK this informations is put there by new. new puts the size of the allocated memory before the just before...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
29
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I remembered delete is implemented through operator overloading, but I am not quite clear. Could anyone recommend some links about how delete is implemented so that I can...
1
by: jmarcrum | last post by:
Hello all! i have a “monitor-type” program, where my program recognizes my defined "commands”, and executes the given command, or produces an error message that says “unrecognized command”. After...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.