GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Apr-2007, 23:01
goochcrabs goochcrabs is offline
Awaiting Email Confirmation
 
Join Date: Jan 2007
Posts: 17
goochcrabs is on a distinguished road

Dynamic allocation and container objects


a class (class 1) uses a dynamically allocated array. the default constructor for class 1 simply assigns a NULL value to the pointer that would be used to access the dynamically allocated array and allocates no memory for that array. if an array of objects of class 1 were allocated, and memory was, later, allocated for (at least some of) the arrays within those objects, would that cause a segmentation fault/ruin data in the array?
  #2  
Old 13-Apr-2007, 07:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: dynamic allocation and container objects


Quote:
Originally Posted by goochcrabs
a class (class 1) uses a dynamically allocated array....?
Instead of just passing a question along for someone else to give a short answer, I suggest that you try the following:

1. Make an example that does this. That is: allocates memory and then puts something in it and then passes the address to some other part of the program (causes the pointer in the object to reference that block).

2. Look at the code.

3. Realize that that once a program has obtained the address of a block of memory by using the new operator (or one of the malloc family of allocation functions), that block of memory belongs to that program until either the program terminates or the block is returned to the operating system by use of the delete operator (or the free() function). Whatever you put into that block stays there until that program de-allocates it or overwrites it with something else.

4. Look at your code and see where it uses one of the allocation functions and passes the address along to other parts of the program.

5. Look at your code and see where the memory was deallocated.

6. Draw your conclusion.

If you still have questions, then post the code that you are considering and tell us what you don't understand.

Regards,

Dave
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Do I need RTTI? edt C++ Forum 7 25-Aug-2006 13:26
Dynamic vs Static Arrays WaltP Miscellaneous Programming Forum 5 16-Feb-2006 15:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:43.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.