large array?? how to avoid stack overflow?

A

A.E lover

Hi all,

I need to generate a float-type array with size 2^25. However, I got
the error: stack over flow due to too large array.
Do you know how to have the array with that big size? (I need exactly
that fixed size so the dynamic array does not work for my case).

Can I use a computer with larger memory? or what else?

Thanks,
 
Q

qarnos

Hi all,

I need to generate a float-type array with size 2^25. However, I got
the error: stack over flow due to too large array.

I'm not surprised. 2^25 is a big number. What of Earth do you need an
array that size for? Maybe you need to rethink your design.
Do you know how to have the array with that big size? (I need exactly
that fixed size so the dynamic array does not work for my case).

Exactly why can't you use a dynamic array?

float * array = malloc(sizeof(float) * 33554432);
 
R

Richard Tobin

A.E lover said:
I need to generate a float-type array with size 2^25. However, I got
the error: stack over flow due to too large array.

All reasonable operating systems will let you control the stack limit.
In unix with bash, for example, the "ulimit" command is what you
need.
Do you know how to have the array with that big size? (I need exactly
that fixed size so the dynamic array does not work for my case).

That doesn't make any sense, and I'm not sure what you mean by
"dynamic array". What exactly goes wrong if you use malloc(),
which is the nearest-to-portable solution?
Can I use a computer with larger memory? or what else?

It's very unlikely that the amount of memory in your computer is the
cause of this problem, if you're using a modern general-purpose
system.

-- Richard
 
C

CBFalconer

A.E lover said:
I need to generate a float-type array with size 2^25. However, I
got the error: stack over flow due to too large array. Do you
know how to have the array with that big size? (I need exactly
that fixed size so the dynamic array does not work for my case).

Can I use a computer with larger memory? or what else?

Think about it. 2^25 is about 32e6. A float normally occupies 4
bytes, thus requiring 128e6 bytes. If the array is of doubles they
normally require 8 bytes each, so the storage needs 256e6 bytes.

To get this (and it is not always possible) you will probably need
to use malloc to allocate the space.
 
R

Richard Tobin

CBFalconer said:
Think about it. 2^25 is about 32e6. A float normally occupies 4
bytes, thus requiring 128e6 bytes. If the array is of doubles they
normally require 8 bytes each, so the storage needs 256e6 bytes.
To get this (and it is not always possible) you will probably need
to use malloc to allocate the space.

Nonsense. You will probably just need to increase your stack limit.
No modern processor is likely to have such a small fixed limit; it's
just limited by default, mainly to trap accidental infinite recursion.

-- Richard
 
K

Keith Thompson

Nonsense. You will probably just need to increase your stack limit.
No modern processor is likely to have such a small fixed limit; it's
just limited by default, mainly to trap accidental infinite recursion.

But using malloc is likely to be a better solution anyway.

(Note that "stack" usually refers to the memory space used for objects
with automatic storage duration.)

Increasing the stack limit typically applies only to one user, and to
all programs run by that user; it does no good if sombody else wants
to run the same program. And presumably the maximum stack space is
limited to a relatively small value for a reason. malloc() typically
allocates its space from a place that typically allows larger
allocations *without* having to change each user's configuration. It
also has the advantage that it informs you of failure by returning a
null pointer.

Much of the above is unspecified by the C standard, but it tends to be
true of typical implementations. Allocation limits are often
different for different storage durations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top