19114

When I want for define to set 1000000, program in process crashed [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/1847789/segmentation-fault-on-large-array-sizes" dir="ltr" rel="nofollow">Segmentation fault on large array sizes</a> <span class="question-originals-answer-count"> 5 answers </span> </li> </ul>

When I want to #define for SIZE 1.000.000 , my program crashed before it start main function, but when i #define for SIZE 100.000 it work. I have two arrays initialization in my program.

#define SIZE 1000000 char *baza_vocka[SIZE]; char b_vocka[SIZE];

<strong>EDIT</strong>: They are local variables.

Answer1:

In case of 1M you're trying to allocate an array on the stack, which is bigger than the stack size. For such sizes you need to allocate the memory on heap.

For example:

int main() { // allocation on the heap char **baza_vocka = malloc(sizeof(char*)*SIZE); char *b_vocka = malloc(sizeof(char)*SIZE); // working code baza_vocka[1] = 0x0; b_vocka[1] = 'a'; // cleaning the heap free(b_vocka); free(baza_vocka); }

Answer2:

I guess that baza_vocka is a <em>local</em> variable, perhaps inside main

You are experimenting some <a href="http://en.wikipedia.org/wiki/Stack_overflow" rel="nofollow"><em>stack overflow</em></a>, .... Local call frames should usually be small (a few kilobytes) in your <a href="http://en.wikipedia.org/wiki/Call_stack" rel="nofollow">call stack</a>

You should not have so big local variables. Allocate such big arrays in the heap using malloc or calloc. Don't forget to test that they are not failing. Read documentation of <a href="http://man7.org/linux/man-pages/man3/malloc.3.html" rel="nofollow">malloc(3)</a>, and don't forget to free such a heap allocated array. Beware of <a href="http://en.wikipedia.org/wiki/Memory_leak" rel="nofollow">memory leaks</a> & <a href="http://en.wikipedia.org/wiki/Buffer_overflow" rel="nofollow">buffer overflows</a>. Use <a href="http://valgrind.org/" rel="nofollow">valgrind</a> if available.

On current desktops, stack space is a few megabytes, so you usually should limit each call frame to a few kilobytes (or a few dozens of kilobytes).

Recommend

  • llvm: value of pointer operation
  • error LNK2001: unresolved external symbol _IID_IDirectDraw2
  • jQuery Mobile doesn't apply style on dynamic checkboxes and can't clicked
  • Incompatible heap sizes error in Sonarqube
  • Error in building MPI program
  • java 1.6 32-bit min and max heap memory issue
  • Ruby Metaprogramming: Dynamically defining method based on child instance attribute
  • Swift 1.2 assigning let after initialization
  • How can I pass the index of a for loop as the argument for pthread_create
  • How to create a decimal.Decimal object with a given number of significant figures?
  • Append dynamic vue component after specific element
  • C++ Object Initialization (Stack)
  • Fortran array automatically growing when adding a value
  • jQuery UI .sortable() call is slow when applies to thousands of elements
  • IE8 stops network access after 5 long polling request
  • CHAR vs VARCHAR for password security
  • Netlink sockets and libnl - nl_recvmsgs_default returning -16 (EBUSY)
  • Initialization section of the package
  • MRO with multiple inheritance in python
  • How to use JavaScript to determine whether a file exists in a directory?
  • ADO and msqli connections very slow
  • PHP buffered output depending on server setting?
  • Fetching methods from BroadcastReceiver to update UI
  • AES padding and writing the ciphertext to a disk file
  • How to extract text from Word files using C#?
  • Symfony2: How to get request parameter
  • Convert array of 8 bytes to signed long in C++
  • How to make Safari send if-modified-since header?
  • ORA-29908: missing primary invocation for ancillary operator
  • GridView Sorting works once only
  • How to pass list parameters for each object using Spring MVC?
  • Linker errors when using intrinsic function via function pointer
  • WPF Applying a trigger on binding failure
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • LevelDB C iterator
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • JaxB to read class hierarchy
  • Does armcc optimizes non-volatile variables with -O0?
  • costura.fody for a dll that references another dll
  • Conditional In-Line CSS for IE and Others?