delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/12/06:48:55

Date: Thu, 12 Feb 1998 13:48:33 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Noam Rotem <nrotem AT johnbryce DOT co DOT il>
cc: zager AT post DOT comstar DOT ru, djgpp AT delorie DOT com
Subject: Re: Dynamically declaring 2-d arrays
In-Reply-To: <Chameleon.980211224106.nrotem@netvision.netvision>
Message-ID: <Pine.SUN.3.91.980212134246.19988B-100000@is>
MIME-Version: 1.0

On Wed, 11 Feb 1998, Noam Rotem wrote:

> >{ int arr[x][y];
>
> This sounds very odd... Can I then return arr and use it as every other 
> dynamic allocation?

No, this array is allocated from the stack, not from the heap (where 
`malloc' gets its memory).  What GCC does is to call `alloca' function 
after the array dimensions are computed at run time.

> What's the type of arr in this case?

Exactly as if you were to declare it at compile time: the type is arr[x][].

> Can you explain how it is implemented by GCC?

See above: it's just a call to `alloca'.  You can do it yourself as well:

	{
	  size_t a = strlen(foo) + 1;
	  char *arr = alloca (a);

	  ....

	  arr[i] = 'x';
	}

For more details, type "info gcc 'C Extensions' 'Variable Length'" from 
the DOS prompt.

- Raw text -


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