C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 19th, 2012, 02:12 PM
Aliii Aliii is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 13 Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 5 h 3 m 59 sec
Reputation Power: 0
Heap vs Stack confusion

I have a class that stores 3 floats and has no other members.

class CVector{

private:
float coord[3];
...
...
};

If I make an istance of it(CVector *V= new CVector()) should I call delete to V when I dont need it anymore? coord[3] is stored on the stack and its deleted automatically I guess.

Also what if I allocate coord[3] in the CVector() constructor with new, then I make a CVector[100] array(outside of the constructor). Will those 100 floats be stored on the heap or the stack?
Thanks!

Reply With Quote
  #2  
Old September 19th, 2012, 02:43 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,139 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 23 h 8 m 12 sec
Reputation Power: 1974
Quote:
Originally Posted by Aliii
If I make an istance of it(CVector *V= new CVector()) should I call delete to V when I dont need it anymore?

Yes, of course. Though what's much more important is that you delete V before it goes out of scope or before you use it in another new, either of which will cause a memory leak. But if V is global and hence will never go out of scope and will never get reused, then not deleting it when you no longer need it doesn't really cause any problem, but rather is simply wasteful.

Quote:
Originally Posted by Aliii
coord[3] is stored on the stack and its deleted automatically I guess.

Huh??? Whatever gave you that idea?

coord[3] is part of the CVector object you created. Assuming that you instantiated that CVector object with a new, then coord[3] is part-and-parcel of that object, which resides in the heap.

Why do you think that it's in the stack? The pointer V may be in the stack if it's a local variable, but what it points to isn't.

Quote:
Originally Posted by Aliii
Also what if I allocate coord[3] in the CVector() constructor with new, then I make a CVector[100] array(outside of the constructor). Will those 100 floats be stored on the heap or the stack?

So I assume you're talking about declaring coord as float *coord; and that in the CVector constructor you will have something like:
coord = new float[3];

OK, think about it. Whenever you new something, where was that new something allocated? From the heap, right? The only things that are on the stack are directly related to function calls and include the argument list and local variables, though not local variables declared as static, which are stored in the read-write data segment along with the global variables.

Function data -- stack.
new and delete -- heap.

It's really that simple.
Comments on this post
Aliii agrees!

Reply With Quote
  #3  
Old September 19th, 2012, 03:02 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
I don't know C++. In C (I suspect it's the same in C++) after declaring an array with 3 objects, the array with index 3 does not exist.

Code:
/* ... */
struct whatever array[3];
array[3]; /* error in C. array[3] does not exist */


Only array[0], array[1], and array[2] are valid elements.

Reply With Quote
  #4  
Old September 19th, 2012, 03:28 PM
Aliii Aliii is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 13 Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level)Aliii User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 5 h 3 m 59 sec
Reputation Power: 0
Thanks a lot!

"coord[3], Why do you think that it's in the stack?"

....thats why I said "confusion" I thought that cause there is no new there.
Now its clear.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Heap vs Stack confusion

Developer Shed Advertisers and Affiliates



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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap