www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - If structures places data to stack why we do not getting stackoverflow

reply Suliman <evermind live.ru> writes:
If structures placing data on the stack why we do not getting 
stackoveflow while we creating array of structures? Or for 
example big structure.

Am I right understand that structures placing data _only_ on 
stack? But the stack size is very limited (on Widnows it's just 
1MB).

So how it's work?
Aug 16
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 16/08/2017 8:06 AM, Suliman wrote:
 If structures placing data on the stack why we do not getting 
 stackoveflow while we creating array of structures? Or for example big 
 structure.
 
 Am I right understand that structures placing data _only_ on stack? But 
 the stack size is very limited (on Widnows it's just 1MB).
 
 So how it's work?
Struct's by themselves go on the stack. If they are allocated via new/malloc its on the heap (and hence are pointers). Same situation with arrays or inside a class.
Aug 16
parent reply Suliman <evermind live.ru> writes:
On Wednesday, 16 August 2017 at 07:09:02 UTC, rikki cattermole 
wrote:
 On 16/08/2017 8:06 AM, Suliman wrote:
 If structures placing data on the stack why we do not getting 
 stackoveflow while we creating array of structures? Or for 
 example big structure.
 
 Am I right understand that structures placing data _only_ on 
 stack? But the stack size is very limited (on Widnows it's 
 just 1MB).
 
 So how it's work?
Struct's by themselves go on the stack. If they are allocated via new/malloc its on the heap (and hence are pointers). Same situation with arrays or inside a class.
But for example if I am getting array of structs and getting data to it, where it's locating?
Aug 16
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 16/08/2017 8:14 AM, Suliman wrote:
 On Wednesday, 16 August 2017 at 07:09:02 UTC, rikki cattermole wrote:
 On 16/08/2017 8:06 AM, Suliman wrote:
 If structures placing data on the stack why we do not getting 
 stackoveflow while we creating array of structures? Or for example 
 big structure.

 Am I right understand that structures placing data _only_ on stack? 
 But the stack size is very limited (on Widnows it's just 1MB).

 So how it's work?
Struct's by themselves go on the stack. If they are allocated via new/malloc its on the heap (and hence are pointers). Same situation with arrays or inside a class.
But for example if I am getting array of structs and getting data to it, where it's locating?
On the heap, unless you are allocating it via e.g. alloca.
Aug 16
parent reply Suliman <evermind live.ru> writes:
 On the heap, unless you are allocating it via e.g. alloca.
If struct MyStruct { int x; int y; } MyStruct mystruct; is located on stack, why: MyStruct [] mystructs; should located on heap?
Aug 16
next sibling parent Patrick Schluter <Patrick.Schluter bbox.fr> writes:
On Wednesday, 16 August 2017 at 07:39:01 UTC, Suliman wrote:
 On the heap, unless you are allocating it via e.g. alloca.
If struct MyStruct { int x; int y; } MyStruct mystruct; is located on stack, why: MyStruct [] mystructs; should located on heap?
because in D MyStruct [] mystructs; is the equivalent of struct { MyStruct *ptr; size_t size; }
Aug 16
prev sibling parent reply Biotronic <simen.kjaras gmail.com> writes:
On Wednesday, 16 August 2017 at 07:39:01 UTC, Suliman wrote:
 On the heap, unless you are allocating it via e.g. alloca.
If struct MyStruct { int x; int y; } MyStruct mystruct; is located on stack, why: MyStruct [] mystructs; should located on heap?
MyStruct[] is actually a struct similar to this: struct MyStruct[] { MyStruct* ptr; size_t length; } That struct is placed on the stack, but the data it points to, via the ptr field, is heap allocated. One explanation of why is that the compiler doesn't know how many elements are in the array, and that that number may change. If it was stack-allocated and a new element was added to the array, everything on the stack would have to be moved. If the compiler does know the number of elements, it can allocate the array on the stack (theoretically, this could be done as an optimization, but in practice I don't think it is). You can give the compiler this information: MyStruct[10] mystructs; This will allocate 10 MyStructs (80 bytes) on the stack, and if you change 10 to a large number, will give a stack overflow. -- Biotronic
Aug 16
parent reply Suliman <evermind live.ru> writes:
 MyStruct[] is actually a struct similar to this:

 struct MyStruct[] {
     MyStruct* ptr;
     size_t length;
 }

 That struct is placed on the stack, but the data it points to, 
 via the ptr field, is heap allocated.
What is struct? Just name and size?
Aug 16
parent reply Biotronic <simen.kjaras gmail.com> writes:
On Wednesday, 16 August 2017 at 12:50:07 UTC, Suliman wrote:
 MyStruct[] is actually a struct similar to this:

 struct MyStruct[] {
     MyStruct* ptr;
     size_t length;
 }

 That struct is placed on the stack, but the data it points to, 
 via the ptr field, is heap allocated.
What is struct? Just name and size?
I'm sorry, I don't understand what you're asking. Can you please repeat with more information? -- Biotronic
Aug 16
parent reply Suliman <evermind live.ru> writes:
On Wednesday, 16 August 2017 at 13:41:29 UTC, Biotronic wrote:
 On Wednesday, 16 August 2017 at 12:50:07 UTC, Suliman wrote:
 MyStruct[] is actually a struct similar to this:

 struct MyStruct[] {
     MyStruct* ptr;
     size_t length;
 }

 That struct is placed on the stack, but the data it points 
 to, via the ptr field, is heap allocated.
What is struct? Just name and size?
I'm sorry, I don't understand what you're asking. Can you please repeat with more information? -- Biotronic
I am trying to understand what structure is. It's name + associated with this name data? I can't understand for my self what mean no put structure to stack. Just put it's name to it or something another?
Aug 16
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 8/16/17 10:32 AM, Suliman wrote:
 On Wednesday, 16 August 2017 at 13:41:29 UTC, Biotronic wrote:
 On Wednesday, 16 August 2017 at 12:50:07 UTC, Suliman wrote:
 MyStruct[] is actually a struct similar to this:

 struct MyStruct[] {
     MyStruct* ptr;
     size_t length;
 }

 That struct is placed on the stack, but the data it points to, via 
 the ptr field, is heap allocated.
What is struct? Just name and size?
I'm sorry, I don't understand what you're asking. Can you please repeat with more information? -- Biotronic
I am trying to understand what structure is. It's name + associated with this name data? I can't understand for my self what mean no put structure to stack. Just put it's name to it or something another?
The structure is just a pointer and length. What it points at is not on the stack, it's in the heap. This is how a dynamic array works. Indeed: auto x = new int[10000]; pragma(msg, x.sizeof); // 16LU -Steve
Aug 16