* Maximum array size?
@ 1999-02-10 5:00 Owen Peter Vajk
1999-02-21 5:00 ` Shana Tackett
0 siblings, 1 reply; 7+ messages in thread
From: Owen Peter Vajk @ 1999-02-10 5:00 UTC (permalink / raw)
Is there a maximum size for an array in C on an alpha? I have a program
that uses a character array that's about 27 MB, on an alpha with 128 MB
of RAM. I compile it under gcc, and when I run it I get a segmentation
fault immediately, even before it hits my first debug print statement.
Any suggestions? The limit seems to be a bit below 2^23 characters.
This bit of code produces a segmentation fault:
----------------------------
#include <stdio.h>
int main()
{
char array[8384432];
printf("Hello, world!\n");
}
----------------------------
while this one runs normally:
----------------------------
#include <stdio.h>
int main()
{
char array[8384433];
printf("Hello, world!\n");
}
----------------------------
Does this hold true for anyone else? And does anyone know how to get
around this limit, since there is certainly more than enough physical
memory to hold such an array. Thanks,
Owen Vajk
ovajk*@leland.stanford.edu
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Maximum array size?
1999-02-10 5:00 Maximum array size? Owen Peter Vajk
@ 1999-02-21 5:00 ` Shana Tackett
1999-02-21 5:00 ` Joachim Wesner
1999-03-09 5:00 ` Shana Tackett
0 siblings, 2 replies; 7+ messages in thread
From: Shana Tackett @ 1999-02-21 5:00 UTC (permalink / raw)
It just so happens that 2^24 = 1Mb. This leads me to believe that
the default stack size is 1Mb. Since other variables are also on the
stack (some which you may not be aware of implicitly, e.g. argc, argv, etc.)
you will always have < 1Mb for your local variables. Unless you are
willing to right your code in assembler or patch the compiler to allocate
more stack by default this is going to be a limitation. The second
probably would not be wise since most applications do not use
1Mb in local variables. A better alternative is to dynamically allocate
memory in the exact amounts needed using malloc or calloc. Refer
to the man pages for a description. Here is a simple example.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *ptr;
ptr = (char *) malloc(1<<24);
if(ptr == NULL) printf("Failed\n");
else
{
printf("Sucess\n");
free(ptr);
}
return 0;
}
I have 96 Mb and this worked fine.
For string variables, I prefer calloc since it sets the buffer to zero thus
guaranteeing
a NULL terminated string. This assumes you have at least one extra byte.
Owen Peter Vajk wrote in message <36C208BE.1D15@leland.Stanford.EDU>...
>Is there a maximum size for an array in C on an alpha? I have a program
>that uses a character array that's about 27 MB, on an alpha with 128 MB
>of RAM. I compile it under gcc, and when I run it I get a segmentation
>fault immediately, even before it hits my first debug print statement.
>Any suggestions? The limit seems to be a bit below 2^23 characters.
>This bit of code produces a segmentation fault:
>
>----------------------------
>#include <stdio.h>
>
>int main()
>{
>char array[8384432];
>printf("Hello, world!\n");
>}
>----------------------------
>
>while this one runs normally:
>
>----------------------------
>#include <stdio.h>
>
>int main()
>{
>char array[8384433];
>printf("Hello, world!\n");
>}
>----------------------------
>
>Does this hold true for anyone else? And does anyone know how to get
>around this limit, since there is certainly more than enough physical
>memory to hold such an array. Thanks,
>
>Owen Vajk
>ovajk*@leland.stanford.edu
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Maximum array size?
1999-02-21 5:00 ` Shana Tackett
@ 1999-02-21 5:00 ` Joachim Wesner
1999-02-21 5:00 ` Nobody
[not found] ` <7bqfk7$q53@bgtnsc03.worldnet.att.net>
1999-03-09 5:00 ` Shana Tackett
1 sibling, 2 replies; 7+ messages in thread
From: Joachim Wesner @ 1999-02-21 5:00 UTC (permalink / raw)
Shana Tackett wrote:
>
> It just so happens that 2^24 = 1Mb. This leads me to believe that
> the default stack size is 1Mb. Since other variables are also on the
> stack (some which you may not be aware of implicitly, e.g. argc, argv, etc.)
> you will always have < 1Mb for your local variables.
Hu ??? 2^24 = Nearly 4*10e6 or 1 Mega**Words** a 32 Bit
JW
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Maximum array size?
1999-02-21 5:00 ` Joachim Wesner
@ 1999-02-21 5:00 ` Nobody
[not found] ` <7bqfk7$q53@bgtnsc03.worldnet.att.net>
1 sibling, 0 replies; 7+ messages in thread
From: Nobody @ 1999-02-21 5:00 UTC (permalink / raw)
Joachim Wesner wrote in message <36D08674.4B8EE1F@frankfurt.netsurf.de>...
>Shana Tackett wrote:
>>
>> It just so happens that 2^24 = 1Mb. This leads me to believe that
>> the default stack size is 1Mb. Since other variables are also on the
>> stack (some which you may not be aware of implicitly, e.g. argc, argv,
etc.)
>> you will always have < 1Mb for your local variables.
>
>Hu ??? 2^24 = Nearly 4*10e6 or 1 Mega**Words** a 32 Bit
>
>JW
The above isn't really clear. I think JW is trying to say that 2^24 is not
1Mb. It is 16Mb. 1Mb is 2^20.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Maximum array size?
[not found] <rz7n22fx6kc.fsf@corton.inria.fr>
@ 1999-02-22 5:00 ` Greg Lindahl
0 siblings, 0 replies; 7+ messages in thread
From: Greg Lindahl @ 1999-02-22 5:00 UTC (permalink / raw)
Robert Harley <harley@corton.inria.fr> writes:
>
> lindahl@pbm.com (Greg Lindahl) writes:
> > You have a funny definition of "best solution". Did you realize that
> > Fortran programs exist? That important programs like XFree86 allocate
> > lots of memory on the stack? That not all the world is one style of C
> > program?
>
> Did you get out of the wrong side of the bed this morning, Greg?
>
>
> The question was undoubtedly about C, since it asked why the following
> program didn't work:
That C program does the same thing that most F77 programs do, so my
posting was quite relevant.
> The suggestion to use malloc() and free() is quite certainly the best
> one in general, although allocating a global array is fine for this
> example.
No, because that solution does not generally solve the problem for
this class of program, which includes many F77 codes as well as
complicated programs such as XFree86, which are in C and can't be
trivially converted to use malloc() and free(). Perhaps I should have
spent more time explaining so you didn't think I was just trying to be
an asshole. This is a real problem that many kernel people just don't
understand; they jump to the same conclusion you do.
> In answer to your question, the openfiles limit does seem to be
> increased on new kernels (but stacksize isn't):
Well, I suppose 1024 fixed is better than 256 fixed...
-- g
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Maximum array size?
[not found] ` <7bqfk7$q53@bgtnsc03.worldnet.att.net>
@ 1999-03-06 5:00 ` Ron Flory
0 siblings, 0 replies; 7+ messages in thread
From: Ron Flory @ 1999-03-06 5:00 UTC (permalink / raw)
> >> It just so happens that 2^24 = 1Mb. This leads me to believe that
> >> the default stack size is 1Mb. Since other variables are also on
> >> the stack (some which you may not be aware of implicitly, e.g.
> >> argc, argv, etc.) you will always have < 1Mb for your local
> >> variables.
huh?
I missed the original message, but declaring more that a (very) few Kb
from the stack is unwise. Use new/malloc for such things- thats what
the heap is for...
ron
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Maximum array size?
1999-02-21 5:00 ` Shana Tackett
1999-02-21 5:00 ` Joachim Wesner
@ 1999-03-09 5:00 ` Shana Tackett
1 sibling, 0 replies; 7+ messages in thread
From: Shana Tackett @ 1999-03-09 5:00 UTC (permalink / raw)
It just so happens that 2^24 = 16Mb. This leads me to believe that
the default stack size is 16Mb. Since other variables are also on the
stack (some which you may not be aware of implicitly, e.g. argc, argv, etc.)
you will always have < 16Mb for your local variables. Unless you are
willing to right your code in assembler or patch the compiler to allocate
more stack by default this is going to be a limitation. The second
probably would not be wise since most applications do not use
1Mb in local variables. A better alternative is to dynamically allocate
memory in the exact amounts needed using malloc or calloc. Refer
to the man pages for a description. Here is a simple example.
>#include <stdio.h>
>#include <stdlib.h>
>
>int main()
> {
> char *ptr;
>
> ptr = (char *) malloc(1<<24);
> if(ptr == NULL) printf("Failed\n");
> else
> {
> printf("Sucess\n");
> free(ptr);
> }
> return 0;
> }
>
>I have 96 Mb and this worked fine.
>For string variables, I prefer calloc since it sets the buffer to zero thus
>guaranteeing
>a NULL terminated string. This assumes you have at least one extra byte.
>
>
>Owen Peter Vajk wrote in message <36C208BE.1D15@leland.Stanford.EDU>...
>>Is there a maximum size for an array in C on an alpha? I have a program
>>that uses a character array that's about 27 MB, on an alpha with 128 MB
>>of RAM. I compile it under gcc, and when I run it I get a segmentation
>>fault immediately, even before it hits my first debug print statement.
>>Any suggestions? The limit seems to be a bit below 2^23 characters.
>>This bit of code produces a segmentation fault:
>>
>>----------------------------
>>#include <stdio.h>
>>
>>int main()
>>{
>>char array[8384432];
>>printf("Hello, world!\n");
>>}
>>----------------------------
>>
>>while this one runs normally:
>>
>>----------------------------
>>#include <stdio.h>
>>
>>int main()
>>{
>>char array[8384433];
>>printf("Hello, world!\n");
>>}
>>----------------------------
>>
>>Does this hold true for anyone else? And does anyone know how to get
>>around this limit, since there is certainly more than enough physical
>>memory to hold such an array. Thanks,
>>
>>Owen Vajk
>>ovajk*@leland.stanford.edu
>
>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~1999-03-09 5:00 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-02-10 5:00 Maximum array size? Owen Peter Vajk
1999-02-21 5:00 ` Shana Tackett
1999-02-21 5:00 ` Joachim Wesner
1999-02-21 5:00 ` Nobody
[not found] ` <7bqfk7$q53@bgtnsc03.worldnet.att.net>
1999-03-06 5:00 ` Ron Flory
1999-03-09 5:00 ` Shana Tackett
[not found] <rz7n22fx6kc.fsf@corton.inria.fr>
1999-02-22 5:00 ` Greg Lindahl
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox