The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to delete content in a file (delete content only) kittusri9 Shell Programming and Scripting 5 05-15-2008 02:12 PM
delete in c++ nandlal High Level Programming 4 02-02-2008 12:48 PM
Can't delete anything in /usr ms1951 UNIX for Dummies Questions & Answers 4 10-22-2004 12:49 PM
delete lesstjm UNIX for Dummies Questions & Answers 3 11-08-2001 03:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-01-2005
arun.viswanath arun.viswanath is offline
Registered User
  
 

Join Date: Jun 2005
Location: INDIA
Posts: 13
C++ = new and delete

This is one basic question regarding C++ new and delete operator.


# include <iostream>
int main()
{
int *ptr = new int(10);
int i=0;
for (i=0 ; i<10 ;i++)
ptr[i] = 10;

delete [] ptr;
printf("%d",ptr[5]); // It prints the value 10

}

In the above program memory for the *ptr is created using new operator and the same memory is deleted using the delete. But after deleting the ptr also I can able to access the memory location using the ptr .
So any one please explain me what new() and delete() actually does.
  #2 (permalink)  
Old 09-03-2005
sarwan sarwan is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 54
It's not working in vc++ compiler

Hi,
the code which you given is not working in VC++ compiler ..it gives run time error.....
sarwan
  #3 (permalink)  
Old 09-05-2005
RishiPahuja's Avatar
RishiPahuja RishiPahuja is offline
Registered User
  
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Thumbs up

Quote:
Originally Posted by arun.viswanath
This is one basic question regarding C++ new and delete operator.


# include <iostream>
int main()
{
int *ptr = new int(10);
int i=0;
for (i=0 ; i<10 ;i++)
ptr[i] = 10;

delete [] ptr;
printf("%d",ptr[5]); // It prints the value 10

}

In the above program memory for the *ptr is created using new operator and the same memory is deleted using the delete. But after deleting the ptr also I can able to access the memory location using the ptr .
So any one please explain me what new() and delete() actually does.


Dear,
Try this code which is the correct way to create a array.


Code:
# include <iostream>
int main()
{
int *ptr = new int[10];
int i=0;
for (i=0 ; i<10 ;i++)
  ptr[i] = i;


delete ptr;
printf("%d",ptr[5]);  // It prints the value 10

}

for a detailed understanding refer a good manual

rishi
  #4 (permalink)  
Old 09-13-2005
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,965
Quote:
Originally Posted by arun.viswanath
In the above program memory for the *ptr is created using new operator and the same memory is deleted using the delete. But after deleting the ptr also I can able to access the memory location using the ptr. So any one please explain me what new() and delete() actually does.
There's an error in that program, which someone else has already pointed out.

The reason you're able to use that memory after deleting it, is because it still exists. To oversimplify a bit, the new operator doesn't create new memory, the memory already exists; the new operator is there to arbitrate who gets what piece of memory. So when you free the memory, it still exists, but now the new operator "knows" it's allowed to give it to someone else.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0