Mail Archives: djgpp/1996/07/30/02:01:24
David M Barrett <dbarrett AT engin DOT umich DOT edu> wrote:
: Wow, thanks a lot. I to malloc a meg and it worked just fine. But, when
: I tried to defind a static array (ie- char Array[1024 * 1024]) I got a
: wierd runtime error
: Exiting due to signal SIGSEGV
: Stack fault at ...
: eax=...
: ebp=...
: Call from traceback EIPs:
: 0x000015b9
: (the ...'s should be replaced with a ton of numbers and info).
: I tried to allocate the array on the first line of the main function.
: The rest of the program is simply printf statements (except for a line
: that sets one of the array values to 5, but it never gets that far). Is
: there something wrong with allocating a buffer this way?
Arrays defined inside of functions are usually not static, unless you
add the keyword static to it. If you have an automatic array inside a
function, the necessary memory will be allocated on the stack (well,
allocated is not really right, the stack pointer is reduced
appropriately), which means that you put a 1MB object on the
stack. Since the default stack is usually 256 or 512k, you will
overflow the stack. Either you can increase the stack size or you can
use a dynamic malloc, but you have to remember to free that when you
don't need it anymore (well, in main, you can just exit, but any other
function).
bye, Alexander
--
Alexander Lehmann, | "On the Internet,
alex AT hal DOT rhein-main DOT de (plain, MIME, NeXT) | nobody knows
lehmann AT mathematik DOT th-darmstadt DOT de (plain) | you're a dog."
!!CHANGED!! <URL:http://www.mathematik.th-darmstadt.de/~lehmann/>
- Raw text -