![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |||||
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Heap vs. Stack memory
A while back, we had this discussion already. Found here
However, recently after reading through "Sam's c++ in 24 hours" by Jesse Liberty. I came up across this line: in a section on "arrays of pointers": Quote:
Quote:
I bolded the points that were a contrast to what you guys were saying. Now saying that "Usually stack memory is severely limited, isn't BS is it?" For, many people have not mentioned that. I'm completely confused now, you guys are saying one thing... the books telling me another thing. What's really going on and why has this author gotten away with being able to write (on the back of the book cover) "Over 250,000 readers have learned c++ from author Jesse Liberty". /------------------------------------------------- As Grumpy stated "is only", Quote:
Quote:
Quote:
__________________
Death smiles at us all. All a man can do is smile back. |
|||||
|
|
|
|
|
#2 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
Now, in the world of multiple processes, each user (let's say they're all C/C++ programs) gets so much memory for global/static data, so much for code, and so much for stack (presuming the really common implementation that actually uses stack, and frequently the processor's call stack). The heap is an area common to all and belonging to none until memory is borrowed, by one of the processes, using malloc or new or kin. You are expected to return this borrowed memory. Generally speaking, returning the memory with delete or free doesn't actually put the memory back on the common heap. It's free for use by the process that originally borrowed it. It is returned to the heap when the program exits or when the program overtly returns it to the heap. Once again, you must distinguish between how an implementation chooses to work with the language, and what the language expresses as requirements. It would be stupid for the language to try to specify what kind of keyboard or drive or display you have. It would be stupid for your keyboard or display to specify what language you must use. You must have intermediaries that marry the two. That's called platform dependency. Many, many machines still use a processor-controlled stack, often the call stack, to manage local variables, argument passing, and so forth. It's just a fairly simple way to accomplish the task. These machines mostly DO have registers in addition. I don't know of a commercially viable one that doesn't, in fact. I have only used one uP that didn't implement a call stack. That was an old one, and a piece of crap. Many of the earlier non-micro processors did not implement such a beast. Call tracing and parameter passing were the job of the programmer. I have at least one post on the forum that describes call-stack operation. It is generalized but fairly detailed, as usage varies somewhat from processor to processor and language implementation to language implementation. See here. Incidentally, if you look into your compiler documentation, you can PROBABLY find a way to increase your stack space and heap space. Today's typical desktop machine (large numbers, but definitely in the minority when it comes to all computers) often have grossly huge amounts of memory. The term 'severely limited', in reference to such machines, should not be used by a knowledgeable, modern author. Of course, I also take the position that "Learn ________ in 24 hours (or days, or weeks)" is a flagrantly stupid and misleading statement. It's the ploy of a used-car salesman or a politician or some other brand of thief, cheat, liar, or crook.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Thanks for your response.
I did notice that if I were to do this: main()
{
Cat aRovert[300000];
//Car aRovert[30000]; this would work.
system("PAUSE");
return EXIT_SUCCESS;
}my program would crash. Whereas, this: main()
{
Cat *aRover[300000];
for (int i = 0; i < 300000; i++)
{
aRover[i] = new Cat;
}
system("PAUSE");
return EXIT_SUCCESS;
}Furthermore, the crash would happend right as I launched the program. As opposed to getting to a specific point then crashing. In case you want the class, here it is: #include <iostream>
using namespace std;
//int main(int argc, char *argv[])
class Animal
{
public:
Animal () {cout << "In Animal Constructor \n";}
~Animal () {cout << "In Animal Destructor \n";}
void setAge(int age){itsAge = age;}
int getAge(){return itsAge;}
virtual void doSomething() {cout << "doing animal things";}
protected:
int itsAge;
};
class Cat : public Animal
{
public:
Cat() {cout << "Cat constructor \n";}
~Cat() {cout << "Cat destructor \n";}
void doSomething () {cout << "Meow \n";}
void JumpHigh () {cout << "Jumping high";}
protected:
int itsHealth;
};Is this not a symptom of having less memory on the stack then on the heap?
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Cat *aRover[300000];
defines an array of 300,000 pointers; for a typical 32-bit machine, 1.2 megabytes. Cat aRover [300000]; defines an array of 300,000 Cats; a much larger quantity. Neither of those sizes are to be considered "severely limited". Severely limited is when you have to spend 3 weeks whittling out 7 bytes so you don't have to buy another 2K EEPROM in production quantities. That's at old sizes and old prices, but you get the picture. If you need housing for 300,000 cats, you should consider spaying/neutering .As I mentioned, the heap is available for multiple processes. If everyone used their share fairly, or if you added all the stack made available for ALL processes, you'd probably recognize the equality.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Okay, thank you.
Now, considering that the application crashes when I make 300,000 cats, wouldn't that be a problem in real life situations or am I simply leaving something out. For instance, using a database to do this kind of work? main()
{
Cat aRovert[300000];
//Car aRovert[30000]; this would work.
system("PAUSE");
return EXIT_SUCCESS;
}
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#6 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>Now, considering that the application crashes when I make 300,000 cats, wouldn't that be a problem in real life situations
Well designed applications avoid that kind of situation by dividing data sets into manageable pieces. Two good examples are streaming and persistence. If you only need one item at a time then you can stream through the items one by one either by generating or retrieving them, processing them, and then reclaiming or reusing the memory for the next item. If you don't have to have RAM-speed access for every item in the entire set all at once, you can persist most of the set to disk and keep a cache of a much smaller set for quick access. If you do have to have RAM-speed access for every item in a data set all at once and the size breaks your application, you can start looking for another job because you'll get fired with solutions like that. ![]()
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#7 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
Sorry, I meant Cat * aRover = new Cat[300000]; // doesn't crash program //Cat aRovert[300000]; // does crash program. The first line doesn't crash the program. While, if don't comment out the second line, it crashes.
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
|
#8 | |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,197
Rep Power: 5
![]() |
Quote:
std::cout << "300000 pointers to Cat = " << 300000 * sizeof(Cat*) << " bytes needed\n"; std::cout << "300000 actual Cat objects = " << 300000 * sizeof(Cat) << " bytes needed\n";
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
|
#9 | ||
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
This: Quote:
Whereas, this.. as Dawei said Cat *aRover[300000]; Defines an array of 300,000 pointers to Cats.
__________________
Death smiles at us all. All a man can do is smile back. |
||
|
|
|
|
|
#10 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>The first line doesn't crash the program. While, if don't comment out the second line, it crashes.
As expected. You can only declare an array of a certain size before it trashes your computer. The second line creates such an array while the first line doesn't create an array at all. It simply allocates a block of dynamic memory and points a pointer to it. If you don't mind me being inaccurate for the sake of simplicity, it goes like this: Anything you create with new is on the heap, and the heap is virtually (haha! I made a funny) limitless. Anything else is on the stack, and the stack is very limited by comparison.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Combining languages | titaniumdecoy | Other Programming Languages | 12 | Jul 13th, 2006 3:03 PM |
| Assembly tutorial, part one. | Mad_guy | Software Design and Algorithms | 21 | Apr 15th, 2006 8:02 PM |
| A noob to Assembly | CodeJunkie | Assembly | 12 | Jan 25th, 2006 3:06 PM |
| STACK- acessing argc and argv outside main() | mguarin | C | 9 | Oct 28th, 2005 9:38 PM |
| Pointers in C (Part I) | Stack Overflow | C | 4 | Apr 28th, 2005 8:03 PM |