Advertisement Jump to content
_WeirdCat_

C++ Dynamic/static allocation of an array c11

Recommended Posts

The following code crashes the app, however i cannot catch the error message, i think its about heap/stack size allocation problem, but anyway could someone explain me why

	
	  poly3f modelset[mset_count];

Breaks

And this does not

poly3f * modelset2 = new poly3f[mset_count];
	

Where mset_count is 128 and each poly3f jas a static arrays

vec3 V[128];

TPlane<float> side_planes[128]; //which contains floats A,B,C,D / vec3 n float d

Share this post


Link to post
Share on other sites
Advertisement

The problem is most likely that you are allocating more memory on the stack than the stack has space.

 

On what operating system are you executing the program and what compiler do you use? On Windows with MSVC you should have by default 1MB of stack-memory. With GCC you should have at least 1MB. You are allocating 128*(128*12 + 128*16?) bytes -> ~460KB, if I am correct about the size of the TPlane<float>. This should in theory fit but by the fact that your program crashes it seems like you are going over the limit.

To fix it you can make the stack bigger. This is possible with compiler options but I would not recommend it.

It's better to just use dynamic allocation via new. The overhead caused by memory allocation is being easily exceeded by any operation you perform over all that data and you do not risk crashes because of using too much stack-memory.

Edited by Cararasu

Share this post


Link to post
Share on other sites

The first one is not actually legal in standard C++ unless mset_count is const. Assuming this is in a function, you are putting your array on the call stack and in that case the size has to be declared at compile time not run time.  Some compilers have an extension to allow mset_count to be non const, but many don't including Visual C++. If it compiles and you are getting a runtime error, it's likely a stack overflow as @Cararasu stated. You can use your second way which puts your array on the heap, or again assuming mset_count is const, you can change the compiler settings to give you a larger stack.

There is also another way to put it on the stack, but you'll still have to increase the stack size. It is also technically not supported on every compiler, but everyone I've ever seen does support it. You can allocate memory using alloca which is similar to malloc but allocates memory on the stack instead of the heap.

It technically should be faster to put stuff on the heap, but if poly3f has to be initialized on creation, the difference may not be significant since most of the time might be spent initializing your array objects and the actually memory allocation time may small by comparison. One thing about stack allocation is you don't have to worry about deallocation. It's automatic. In your second example you of course need a delete somewhere or you will have a memory leak. 

Share this post


Link to post
Share on other sites

Variable Length Arrays are perfectly legal and standard in C11 (C++ is not C --  they're not standard in C++ and even most compilers that used to support them as a vendor extension no longer do).

A float is 32 bits, or 4 bytes.  I would assume from the words you randomly arranged on the page that a vec3 is 3 floats, or 12 bytes.  128 of those beggars is 1536 bytes.

I going to guess that TPlane has 5 floats and a vec3, for a total of 8x4 or 32 bytes.  128 of those daddies is 4096.

That would make your poly3f 5632 bytes.  If you allocate 128 of those babies, you're going to munch 720896 bytes out of the stack.  That's 75% of the 1048576 default stack size reserved by Microsoft Windows for the initial thread.  If you're deep in the call stack and ancestor functions have also been busy munching on auto storage then yeah, you're going to blow something real good.  Other OSes can have much much smaller default stack sizes -- don't try this on your phone, folks.

It's a rule of thumb, but avoid large memory allocations in automatic storage.  Remember, "large" is a relative term, but 0.75 MB is definitely on the bariatric side and a candidate for dynamic allocation.  Remember, C11 does not support the new operator: that's C++.  In C11 you have to use the malloc family for dynamic allocation.

Share this post


Link to post
Share on other sites

Why is this tagged "C++" but has "c11" in the title? Which is it? It's good to know what language we are talking about.

Share this post


Link to post
Share on other sites

Unless the OP suddenly decided to change languages between the first and second example, it's C++. The c11 in the title is most likely a typo.

Share this post


Link to post
Share on other sites

OK, to be clear, C11 is an old version of the C programming language (originally created by Brian Kernighan and Dennis Ritchie in the early 1970s).  The current version of C is C17 as defined in international standard ISO/IEC 9899:2017(E).   C++11 is an ancient version of the C++ programming language (originally created by Bjarne Stroustrup in the mid-1980s).  The current version is C++17 as defined in international standard ISO/IEC 14882:2017(E).

Variable Length Arrays are not valid C++.  They have never been valid C++.  No recent C++ compiler supports VLAs in C++.  Use a std:vector instead.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!