delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/11/29/13:32:29

Xref: news-dnh.mv.net comp.lang.c++:59045 comp.os.msdos.djgpp:3476
Newsgroups: comp.os.msdos.djgpp,comp.lang.c++
Path: news-dnh.mv.net!mv!barney.gvi.net!news.netrail.net!news.sprintlink.net!newsfeed.internetmci.com!EU.net!sun4nl!phcoms4.seri.philips.nl!newssvr!kunst
From: kunst AT natlab DOT research DOT philips DOT com (Pieter Kunst)
Subject: Re: Declaring arrays inside generic function
Sender: news AT natlab DOT research DOT philips DOT com (USENET News System)
Organization: Philips Research Laboratories, Eindhoven, The Netherlands
References: <Pine DOT A32 DOT 3 DOT 91 DOT 951127202934 DOT 29743A-100000 AT srv1 DOT freenet DOT calgary DOT ab DOT ca>
Date: Tue, 28 Nov 1995 10:35:50 GMT
Lines: 49
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Dj-Gateway: from newsgroup comp.os.msdos.djgpp

In article <Pine DOT A32 DOT 3 DOT 91 DOT 951127202934 DOT 29743A-100000 AT srv1 DOT freenet DOT calgary DOT ab DOT ca> "Michael E. Wesolowski" <mewesolo AT freenet DOT calgary DOT ab DOT ca> writes:
>I don't know if this is a problem with gcc, the DOS port of gcc, or 
>something (i.e., not a bug) in the implementation that I don't know 
>about. I'm using the DOS port of gcc (djgpp), v1.12m4. This is equivalent 
>ot gcc 2.6.3, if I remember correctly.
>
>I have a generic function which has as one of its input parameters an int 
>which identifies an array size. Within the function, I attempt to declare 
>an array of int's:
>
>int item_count [array_size];
>
>where array_size is the input parameter. When I look at the array in the 
>debugger (gdb) however, what i see is an array of int pointers (I think) 
>- something like int (*) [60000] (the 60000 is approximate). If I 
>explicitly declare the array as, for example, item_count [10], I get the 
>expected array of 10, uninitialized ints. SO, what's the problem?
>
>If it's something in the ANSI standard, I'd appreciate the paragraph 
>reference as well as a simple description of what's going on. Thanks.
>

If I understand you correctly, you're trying to do some sort of
dynamic allocation of an automatic array, e.g:

    void myfunc (int size)
    {
      int i, item[size];

      for (i=0; i<size; i++) item[i] = i;
    }

I don't think this is supported by ANSI C.
To obtain what you want, you can use malloc():

    void myfunc (int size)
    {
      int i, *item;

      item = (int *)malloc(size*sizeof(int));
      for (i=0; i<size; i++) item[i] = i;
      free (item);
    }

If you're programming in C++, you can use 'new' for this purpose.

Pieter Kunst (kunst AT natlab DOT research DOT philips DOT com)


- Raw text -


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