-1

I'm new to c

int main(int argc,char *argv[]) {
        char *p[1234567] = { NULL };
        return 1;

}

gives Segmentation fault

if I change to 12345, it will work.

Sato
  • 7,432
  • 15
  • 54
  • 101
  • There are actually several candidates for duplicates. – jxh Sep 01 '17 at 05:08
  • A specific solution here is to move the variable to the static memory area just before main. That area is usually much larger than the stack. – Bo Persson Sep 01 '17 at 11:29

2 Answers2

2

Thats most likely because your stack dont have 1234567 * sizeof(char *) bytes of space as needed by variable p

Pras
  • 4,012
  • 9
  • 20
2

An array of 1234567 pointers will be more than 4MB. That is larger than the stack capacity for a thread on many systems. For example, if I recall correctly, on Win32 the address space reserved for a thread's stack defaults to 1MB.

Michael Burr
  • 321,763
  • 49
  • 514
  • 739