Hi I got some help earlier on a problem similar to this which helped me find stack values for
"List myList;": stack 8,
"List *special = new List(10);": stack 4,
unknown about "List lists[5];"
all within int main
Not sure what the heap is for any of the individual calls when ran, don't know stack for last one, here is code:
#include <iostream>
#include <iomanip>
#include <sstream>
usingnamespace std;
class List {
private:
int numItems;
int *array;
public:
List(int n = 20) {
numItems = n;
array = newint [numItems];
}
};
int main()
{
//finding heap and stack from:
List myList;
//or List lists[5];
//or List *special = new List(10);
}
I guess I still don't exactly know what the question is. What level of detail are you looking for? I'll try to be specific.
- You have an object of type List.
- This object is allocated "on the stack" on line 22.
- Specifically, sizeof(List) bytes are allocated; enough to hold the 'numItems' and 'array' (just the pointer) variables, plus implementation-defined padding.
However, inside the implementation of the constructor, more is being allocated. You call new int[numItems]. This allocates at least sizeof(int) * numItems bytes (+some bookkeeping memory) on the heap.
___________________________________
List lists[5]; does the same as the above, but 5x. So it will allocate 5 * sizeof(List) on the stack, and do 5 separate heap allocations for each dynamic array.
I am wondering about the size of stack and heap in bytes, there's variations to the program (i.e. lines 22-24) and I'm not sure what impact it has on the heap or how to find it in this instance. I found the sizeof (the stack) for the mylist and pointer but list lists[5] remains unknown along with heap bytes. I'm not sure on the process to find it in these cases. (I'll try out what you said though in the last 2 lines soon)
"List myList;": stack 8 bytes,
"List *special = new List(10);": stack 4 bytes,
unknown about "List lists[5];"
pointers are just an integer, that is an offset into memory.
if list*special is 4 bytes, then you are on a 32 bit system. A 64 bit system would have had 8 bytes there.
list lists[5] is an array on the stack. it uses sizeof(list)*5 bytes. The heap will use whatever each array location list item allocates in subsequent code.
It seems like you are making this harder than it needs to be :)
pointers take up one integer (machine word size) of stack each, and when used for dynamic memory take up sizeof(pointer's type) * (number of things allocated) on the heap.
non pointers take up sizeof(type) each: when poked into an array, you get sizeof * number of them.
objects with internal pointers work the exact same way... an instance of the object locally on the stack still takes up sizeof(type) (this includes word sized int storage for each pointer in it) and the internal pointers each store on the heap whatever you allocate to them (size of pointer type * number of items). Its the same as simple variable types in main, just has a wrapper around it.
c++ containers work like the objects, but you can't see the pointers (private members, not accessible to you).
Says I am wrong, I tried to do sizeof(List) * 5 bytes for stack and 5 for heap for"List lists[5];"
for
"List *special = new List(10);" I do sizeof(special) for stack which is correct, but for heap I did sizeof(special)*#allocated (which I assume is 10), but its wrong,
"List myList;", I obtained sizeof(list) for stack--correct, and then I tried sizeof(list)*sizeof(myList) for heap which is wrong.
It's sizeof(special) on the stack, then sizeof(List) + <whatever is allocated within List ctor> on heap.
sizeof(list)*sizeof(myList)
This is definitely not it. sizeof(List) and sizeof(some instance of a List object) are the same thing. So you're just squaring sizeof(List).
It's sizeof(List) on the stack, and <whatever is allocated within the List ctor> on the heap.
"<whatever is allocated within the List ctor>" for the heap would then be numItems * sizeof(int) + <internal bookkeeping>.
My objective is to obtain bytes of stack and heap for each variation of the program (program changes to have 1 line of either
"List myList;"
"List *special = new List(10);"
"List lists[5];"
I used sizeof(xxx) to get stack for most of the questions (all but 1) heap involves obscure values and allocations. Here is a word-for-word statement:
This code results in allocation of [x] bytes in stack memory and [y] bytes in heap memory.
List's constructor allocates n objects of type int. Therefore List myList;
Consumes sizeof(myList) bytes from the stack and 20 * sizeof(int) bytes of free store. Likewise, List lists[5];
Consumes sizeof(lists) == 5 * sizeof(List) bytes from the stack and 5 * 20 * sizeof(int) bytes of free store; finally List *special = new List(10);
Consumes sizeof(special) bytes from the stack and sizeof(List) + 10 * sizeof(int) bytes from the free store.
Thanks for your help mbozzi, however I inputted the values according to the equations and the only right answer was for the "sizeof(List) + 10 * sizeof(int)" (for heap) aside from my own determinations stated earlier. list lists[5] was also completely incorrect
(although I was kinda confused by " sizeof(lists) == 5 * sizeof(List) " does this mean stack contains 5*sizeof(List) or what?)
Does this mean stack contains 5*sizeof(List) or what?
The object lists is an array of 5 objects of type List. So its size is the same as 5 individual List objects together. That's what I mean by sizeof(lists) == 5 * sizeof(List)
If you wanted to get technical, these figures don't imply anything about the stack or heap usage. But try not to worry about this, it's not what your professor cares about.
List myList;" //one list variable on the stack. stack size is sizeof(list) now.
"List *special = new List(10);" //heap has 10 lists, sizeof(list)*10.
"List lists[5];" //stack gets 5 more, total 6, lists. stack size += sizeof(list)*5
When I refer to equations, I refer to the obtaining of values of bytes. For example:
sizeof(lists) == 5 * sizeof(List) which is true, both of their values equate to exactly 40 bytes but according to the q&a, stack is not 40.
5 * 20 * sizeof(int) is == 5*20*4 (4 is the standard size of int in bytes, 8 for double but I digress)
this equates to 400 bytes allocated on the heap (or which I assume such) but is not correct.
sizeof(List) + 10 * sizeof(int) == 48 which is true and according to q&a is correct for that particular line variation.
This program outputs 400 on various online compilers and my own system, although 400 is not the only permissible output. The sizes or alignment requirements of certain types could differ, and the compiler could perform allocation elision, or dead code elimination, or various other optimizations that could affect the output. The implementation could allocate more bytes for book-keeping information, or to satisfy alignment requirements. http://coliru.stacked-crooked.com/a/95142d8451a77dd3
The as-if rule says the compiler optimizer is not allowed to affect the observable behavior of a program. However observable effects in replaceable allocation functions are exempt in order to allow the compiler to perform allocation elision. Here "observable" has a standardese definition. The actual amount of heap memory allocated is not observable either.
I'm pretty sure your professor would be satisfied with 400. If not, maybe some data types are different sizes on their system or we misunderstand the question.