-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
  • 8,192
  • 17
  • 60
  • 115
  • 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,047
  • 10
  • 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
  • 333,147
  • 50
  • 533
  • 760