M
M
Makaleks2014-03-24 20:30:18
C++ / C#
Makaleks, 2014-03-24 20:30:18

How to create a large array in different initializations?

We need a large array of type char
Array will be needed all the time, that is, dynamic memory allocation, in fact, is absolutely useless.
But:
Why can't I create a large array (static)

So,
char p[1000000];snapshot1.png

but I can allocate the same memory dynamically
like that.
char *kuk = new char[1000000];
delete [] kuk;
snapshot2.png

Questions:
  1) Whence such difference at different implementations?
  2) Is it possible in my case to create large static arrays?
   2.1) What is the size limit of an array?
    Thank you!
why do I need it (not very relevant)
 Всего лишь реализация редактируемой 3D карты средствами OpenGL.
 Конечно, есть небезызвестная карта высот (это где по насыщенности цвета определяешь высоту), но в этом случае нельзя создавать пещеры, тоннели и некоторые углубления, да и редактировать это дело хочется (новые дырки делать).
 С ресурсами приложения (пока) и близко не знаком

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2014-03-24
@Makaleks

Why can't I create a large static array

Because in the above code, the array is not static, but on the stack.
It becomes static if you add the word static to it or remove its definition from a function.
Why the program crashes - because the stack size is not enough. The size of the reserved stack (i.e. the maximum possible stack size, memory for it is allocated as it is used) is written in the properties of your PE file and can be set during linking.
A dynamically allocated array is allocated on the heap. There are also limits on the heap size, but they are usually much larger. In addition, the application can create new heaps of the desired size at runtime.
The size of an array is limited by the following things:
1. the size of the pointer (i.e. the memory model under which the assembly is made. So, for example, in the x32 model with 32-bit pointers, the total size of all arrays is limited to 4 gigabytes)
2. the type of array allocation:
- automatic (on the stack) - - reserved stack size;
- static (in data segment/bss) -- pointer size;
- dynamic (on the heap) -- by reserved heap size/memory manager implementation;
- in memory allocated by VirtualAlloc/MapViewOfFile/... -- pointer size and current address space fragmentation.

A
AxisPod, 2014-03-26
@AxisPod

Apparently because you must first read books, and then program. If you don’t even know the basics, they will write in books much better than on forums, etc. And you seem too lazy to read books. Working with memory is far from the most trivial task, first study how memory is organized in the OS, the mechanisms of interaction and memory management in the programming language. Eventually, the questions will disappear. And the fact that they will answer you here, you still will not learn, because. said above, you don't know the basics.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question