Re: What is the maximum acceptable size of an array?

From: Konstantin Olchanski <olchansk_at_triumf.ca>
Date: Mon, 9 Jul 2007 12:48:17 -0700


On Mon, Jul 09, 2007 at 06:02:41PM +0200, John Zoidberg wrote:
> #define MAX 1972432
> int toto() {
> char a[MAX]; ...

John - you allocated the array "a" "on the stack". Space available in the stack is never too big and is also system dependant (i.e. threads other than the main thread tend to have small stacks). When you run out of stack space, you may see wierd failures, ranging from core dumps (from "limit stacksize") to things similar to buffer overflows.

Best is to allocate big arrays somewhere else. Here are your options:

int foo() {
  int a[1000000000]; // on the stack. avoid this   static int a[1000000000]; // in BSS. this is not reentrant - do not do this for recursive functions   int *a = new int[1000000000]; // in the heap, the C++ way. When you run out of space, you crash   int *a = (int*)malloc(sizeof(int)*1000000000); // in the heap, the C way. When you run out of space, you get a NULL pointer }

-- 
Konstantin Olchanski
Data Acquisition Systems: The Bytes Must Flow!
Email: olchansk-at-triumf-dot-ca
Snail mail: 4004 Wesbrook Mall, TRIUMF, Vancouver, B.C., V6T 2A3, Canada
Received on Mon Jul 09 2007 - 22:21:50 CEST

This archive was generated by hypermail 2.2.0 : Tue Jul 10 2007 - 17:50:02 CEST