Why does declaring an int array of length INT_MAX create a segmentation fault? [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • Segmentation Fault, large arrays

    1 answer




The following code, when compiled and run, gives me a segmentation fault. Why is this?



#include <stdio.h>
#include <limits.h>

int main(void)
{
int fat_array[INT_MAX];

return 0;
}









share|improve this question













marked as duplicate by P.P. c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 10 at 22:33


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Your system, that time, did not have enough memory for a local array that big. It tried and failed at run time.
    – chux
    Nov 10 at 22:22








  • 1




    stackoverflow without the .com
    – PSkocik
    Nov 10 at 22:22






  • 2




    You'd need 8 GiB of stack space for int fat_array[INT_MAX]; Unix-like systems are generous and normally give you 8 MiB of stack space; Windows is more conservative and normally only gives you 1 MiB. Either way, it's massively less space than needed. Either allocate the array at file scope (outside main()), or use dynamic memory allocation (malloc() et al).
    – Jonathan Leffler
    Nov 10 at 22:25

















up vote
0
down vote

favorite













This question already has an answer here:




  • Segmentation Fault, large arrays

    1 answer




The following code, when compiled and run, gives me a segmentation fault. Why is this?



#include <stdio.h>
#include <limits.h>

int main(void)
{
int fat_array[INT_MAX];

return 0;
}









share|improve this question













marked as duplicate by P.P. c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 10 at 22:33


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Your system, that time, did not have enough memory for a local array that big. It tried and failed at run time.
    – chux
    Nov 10 at 22:22








  • 1




    stackoverflow without the .com
    – PSkocik
    Nov 10 at 22:22






  • 2




    You'd need 8 GiB of stack space for int fat_array[INT_MAX]; Unix-like systems are generous and normally give you 8 MiB of stack space; Windows is more conservative and normally only gives you 1 MiB. Either way, it's massively less space than needed. Either allocate the array at file scope (outside main()), or use dynamic memory allocation (malloc() et al).
    – Jonathan Leffler
    Nov 10 at 22:25















up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • Segmentation Fault, large arrays

    1 answer




The following code, when compiled and run, gives me a segmentation fault. Why is this?



#include <stdio.h>
#include <limits.h>

int main(void)
{
int fat_array[INT_MAX];

return 0;
}









share|improve this question














This question already has an answer here:




  • Segmentation Fault, large arrays

    1 answer




The following code, when compiled and run, gives me a segmentation fault. Why is this?



#include <stdio.h>
#include <limits.h>

int main(void)
{
int fat_array[INT_MAX];

return 0;
}




This question already has an answer here:




  • Segmentation Fault, large arrays

    1 answer








c arrays segmentation-fault






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 22:20









Uclydde

6991419




6991419




marked as duplicate by P.P. c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 10 at 22:33


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by P.P. c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 10 at 22:33


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    Your system, that time, did not have enough memory for a local array that big. It tried and failed at run time.
    – chux
    Nov 10 at 22:22








  • 1




    stackoverflow without the .com
    – PSkocik
    Nov 10 at 22:22






  • 2




    You'd need 8 GiB of stack space for int fat_array[INT_MAX]; Unix-like systems are generous and normally give you 8 MiB of stack space; Windows is more conservative and normally only gives you 1 MiB. Either way, it's massively less space than needed. Either allocate the array at file scope (outside main()), or use dynamic memory allocation (malloc() et al).
    – Jonathan Leffler
    Nov 10 at 22:25
















  • 1




    Your system, that time, did not have enough memory for a local array that big. It tried and failed at run time.
    – chux
    Nov 10 at 22:22








  • 1




    stackoverflow without the .com
    – PSkocik
    Nov 10 at 22:22






  • 2




    You'd need 8 GiB of stack space for int fat_array[INT_MAX]; Unix-like systems are generous and normally give you 8 MiB of stack space; Windows is more conservative and normally only gives you 1 MiB. Either way, it's massively less space than needed. Either allocate the array at file scope (outside main()), or use dynamic memory allocation (malloc() et al).
    – Jonathan Leffler
    Nov 10 at 22:25










1




1




Your system, that time, did not have enough memory for a local array that big. It tried and failed at run time.
– chux
Nov 10 at 22:22






Your system, that time, did not have enough memory for a local array that big. It tried and failed at run time.
– chux
Nov 10 at 22:22






1




1




stackoverflow without the .com
– PSkocik
Nov 10 at 22:22




stackoverflow without the .com
– PSkocik
Nov 10 at 22:22




2




2




You'd need 8 GiB of stack space for int fat_array[INT_MAX]; Unix-like systems are generous and normally give you 8 MiB of stack space; Windows is more conservative and normally only gives you 1 MiB. Either way, it's massively less space than needed. Either allocate the array at file scope (outside main()), or use dynamic memory allocation (malloc() et al).
– Jonathan Leffler
Nov 10 at 22:25






You'd need 8 GiB of stack space for int fat_array[INT_MAX]; Unix-like systems are generous and normally give you 8 MiB of stack space; Windows is more conservative and normally only gives you 1 MiB. Either way, it's massively less space than needed. Either allocate the array at file scope (outside main()), or use dynamic memory allocation (malloc() et al).
– Jonathan Leffler
Nov 10 at 22:25














1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










What you are requesting is to have about 2,147,483,647integer spaces allocated to you. Each integer is usually four bytes so that's 8,589,934,588 bytes which is 8 gigabytes of memory. This is likely above the allowed amount of memory a single process is allowed to reserve, and for good reason, so you get an error.






share|improve this answer

















  • 2




    It's slightly misleading to say "above the allowed amount of memory a single process is allowed to reserve". If it were to be heap allocted and the system has enough it's fine to use to reserve/allocate as much and there's no general limit for memory. The specific problem here is that it's "stack" allocated (aka automatic storage).
    – P.P.
    Nov 10 at 22:45




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










What you are requesting is to have about 2,147,483,647integer spaces allocated to you. Each integer is usually four bytes so that's 8,589,934,588 bytes which is 8 gigabytes of memory. This is likely above the allowed amount of memory a single process is allowed to reserve, and for good reason, so you get an error.






share|improve this answer

















  • 2




    It's slightly misleading to say "above the allowed amount of memory a single process is allowed to reserve". If it were to be heap allocted and the system has enough it's fine to use to reserve/allocate as much and there's no general limit for memory. The specific problem here is that it's "stack" allocated (aka automatic storage).
    – P.P.
    Nov 10 at 22:45

















up vote
2
down vote



accepted










What you are requesting is to have about 2,147,483,647integer spaces allocated to you. Each integer is usually four bytes so that's 8,589,934,588 bytes which is 8 gigabytes of memory. This is likely above the allowed amount of memory a single process is allowed to reserve, and for good reason, so you get an error.






share|improve this answer

















  • 2




    It's slightly misleading to say "above the allowed amount of memory a single process is allowed to reserve". If it were to be heap allocted and the system has enough it's fine to use to reserve/allocate as much and there's no general limit for memory. The specific problem here is that it's "stack" allocated (aka automatic storage).
    – P.P.
    Nov 10 at 22:45















up vote
2
down vote



accepted







up vote
2
down vote



accepted






What you are requesting is to have about 2,147,483,647integer spaces allocated to you. Each integer is usually four bytes so that's 8,589,934,588 bytes which is 8 gigabytes of memory. This is likely above the allowed amount of memory a single process is allowed to reserve, and for good reason, so you get an error.






share|improve this answer












What you are requesting is to have about 2,147,483,647integer spaces allocated to you. Each integer is usually four bytes so that's 8,589,934,588 bytes which is 8 gigabytes of memory. This is likely above the allowed amount of memory a single process is allowed to reserve, and for good reason, so you get an error.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 22:26









Mitchel Paulin

1,484317




1,484317








  • 2




    It's slightly misleading to say "above the allowed amount of memory a single process is allowed to reserve". If it were to be heap allocted and the system has enough it's fine to use to reserve/allocate as much and there's no general limit for memory. The specific problem here is that it's "stack" allocated (aka automatic storage).
    – P.P.
    Nov 10 at 22:45
















  • 2




    It's slightly misleading to say "above the allowed amount of memory a single process is allowed to reserve". If it were to be heap allocted and the system has enough it's fine to use to reserve/allocate as much and there's no general limit for memory. The specific problem here is that it's "stack" allocated (aka automatic storage).
    – P.P.
    Nov 10 at 22:45










2




2




It's slightly misleading to say "above the allowed amount of memory a single process is allowed to reserve". If it were to be heap allocted and the system has enough it's fine to use to reserve/allocate as much and there's no general limit for memory. The specific problem here is that it's "stack" allocated (aka automatic storage).
– P.P.
Nov 10 at 22:45






It's slightly misleading to say "above the allowed amount of memory a single process is allowed to reserve". If it were to be heap allocted and the system has enough it's fine to use to reserve/allocate as much and there's no general limit for memory. The specific problem here is that it's "stack" allocated (aka automatic storage).
– P.P.
Nov 10 at 22:45





Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values