CFanatic

Go Back   CFanatic > Programming > C++ Programming

Join CFanatic Forum Now

dynamic memory allocation topic posted under C++ Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 08-26-2008, 12:44 PM
Junior Member
 
Join Date: Aug 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
vamsi1990 is on a distinguished road
| More
Question dynamic memory allocation

Hi,
i just want to know about dynamioc memopry allocation.i allocated the space using new operator for an entire array of 5 numbers of type int.
i deallocated the space using delete keyword for the entire array.
now my doubt is hw we can see that the memory hasbeen deallocated.if we think that the memory is deallocated in side the system implictly trhen when again i started printing the array the elements inside it again gettng printed.so after the space hasbeen deallocated hw the elements inside gets printed.if this is possible then we can save many bytes of space.
if the memory hasbeen deallocated then from wher the elements geting printed.
iam postng u the program related to my doubt


Code:
#include <iostream>
using namespace std;

int main ()
{
  int i,n;
  int * p;
  cout << "How many numbers would you like to type? ";
  cin >> i;
  p= new (nothrow) int[i];
  if (p == 0)
    cout << "Error: memory could not be allocated";
  else
  {
    for (n=0; n<i; n++)
    {
      cout << "Enter number: ";
      cin >> p[n];
    }
    cout << "You have entered: ";
    for (n=0; n<i; n++)
      cout << p[n] << ", ";
    delete[] p;
  }
  for (n=0; n<i; n++)
      cout << p[n] << ", ";
  return 0;
}
this is the program done by me wher i got the above doubts

Last edited by shabbir; 08-26-2008 at 10:45 PM. Reason: Code block
Reply With Quote
  #2  
Old 09-04-2008, 02:22 AM
Junior Member
 
Join Date: Jul 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ranjittechie is on a distinguished road
| More
Smile Re: dynamic memory allocation

Hello ,
If u saw carefullly after deleting P array only junk values were printed not the ones which are assigned to it. so it means that space got freed.

i Hope this shall clear ur doubt

regards
~ranjit
Reply With Quote
  #3  
Old 12-31-2008, 12:29 AM
Member
 
Join Date: Dec 2008
Posts: 48
Thanks: 0
Thanked 1 Time in 1 Post
hkp819 is on a distinguished road
| More
Re: dynamic memory allocation

To solve your problem you should visit in following link......
http://www.parashift.com/c++-faq-lit...html#faq-16.11
Reply With Quote
Reply

Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
in-memory pdf2text? AllanEbdrup C# Programming 1 03-02-2011 01:58 PM
Please suggest a C++ source code analysis tool for debugging memory leak tommy_24 C++ Programming 5 05-20-2009 09:49 PM
Toolbar help: How to make buttons dynamic using images Echelo Win32 Programming 1 08-27-2008 04:32 AM
is there a memory allocation issue here mijal C++ Programming 5 07-14-2007 07:43 AM
How to calculate total free memory Iqbal_h_a C Programming 1 12-26-2006 03:23 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 03:55 PM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board