delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2004/04/22/13:16:45

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
From: "Alex Vinokur" <alexvn AT foot DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Trouble of using very large arrays
Date: Thu, 22 Apr 2004 20:10:08 +0300
Lines: 131
Message-ID: <c68u9l$9fevb$1@ID-79865.news.uni-berlin.de>
References: <c68d6j$g57$03$1 AT news DOT t-online DOT com> <c68e70$96gqq$1 AT ID-231750 DOT news DOT uni-berlin DOT de>
NNTP-Posting-Host: 82.166.217.90
X-Trace: news.uni-berlin.de 1082653814 9944043 I 82.166.217.90 ([79865])
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"Hans-Bernhard Broeker" <broeker AT physik DOT rwth-aachen DOT de> wrote in message news:c68e70$96gqq$1 AT ID-231750 DOT news DOT uni-berlin DOT de...
> Mok-Kong Shen <mok-kong DOT shen AT t-online DOT de> wrote:
>
> > I have difficulty in using very large arrays. In a
> > simple program I could have 'unsigned int a[70000][2]'
> > without problem but with 'unsigned int a[80000][2]' the
> > program aborts without even the code being changed to
> > access the increased new part of the array. Why is
> > this so? Thanks in advance.
>
> You don't provide all the necessary details for anyone to be sure, but
> this very strongly feels like you're blowing the stack.  Don't make
> large variables like this automatic (i.e. local to some function).
> The usual recommended solution is to use the heap instead of the
> stack, i.e. use 'new' or malloc(), depending on what language you're
> working in, or make such variables static.
>
[snip]

Here is a sample.

------ C code : BEGIN ------
/* File foo.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

int main(int argc, char** argv)
{
int columns;
int rows;
unsigned int* a;
int i, j;

  if (argc < 3)
  {
    printf ("USAGE : %s <number of rows> <number of columns>\n\n", argv[0]);
    return 1;
  }

  rows    = atoi (argv[1]);
  columns = atoi (argv[2]);

  printf ("Rows - %u, Columns - %u\n", rows, columns);
  a = (unsigned int*)malloc (rows * columns * sizeof (unsigned int));
  if (a == NULL)
  {
    printf ("Failure : Unable to allocate storage\n");
    return 1;
  }

  printf ("Success : the storage allocated\n");
  memset (a, 0, columns * rows * sizeof (unsigned int));

  /* --- Sample --- */

  printf ("\nSample\n");
  printf ("------\n\n");
#define TEST_ROW_NO  3
#define TEST_COL_NO  0
  assert (TEST_COL_NO < columns);
  assert (TEST_ROW_NO < rows);
  a[TEST_ROW_NO * columns + TEST_COL_NO] = 100;

  for (i = 0; i <= (TEST_ROW_NO + 1); i++)
  {
    for (j = 0; j <= (TEST_COL_NO + 1); j++)
    {
      printf ("a[%d][%d] = %d\n", i, j, a[i * columns + j]);
    }
    printf ("\n");
  }

  /* ---------------*/

  return 0;
}
------ C code : END ------


------ Compilation & Run : BEGIN ------

$ gcc -W -Wall foo.c



$ a
USAGE : a <number of rows> <number of columns>



$ a 10000000 3
Rows - 10000000, Columns - 3
Success : the storage allocated

Sample
------

a[0][0] = 0
a[0][1] = 0

a[1][0] = 0
a[1][1] = 0

a[2][0] = 0
a[2][1] = 0

a[3][0] = 100
a[3][1] = 0

a[4][0] = 0
a[4][1] = 0



$ a 100000000 3
Rows - 100000000, Columns - 3
Failure : Unable to allocate storage


------ Compilation & Run : END --------

--
   Alex Vinokur
     mailto:alexvn AT connect DOT to
     http://mathforum.org/library/view/10978.html



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019