Thread View: comp.os.msdos.djgpp
3 messages
3 total messages
Started by Michael E. Wesolowski
Tue, 28 Nov 1995 03:37
Declaring arrays inside generic function
Author: Michael E. Wesolowski
Date: Tue, 28 Nov 1995 03:37
Date: Tue, 28 Nov 1995 03:37
23 lines
1576 bytes
1576 bytes
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. --------------------------------------------------------------------- Michael Wesolowski (mewesolo@freenet.calgary.ab.ca)
Re: Declaring arrays inside generic function
Author: Pieter Kunst
Date: Tue, 28 Nov 1995 10:35
Date: Tue, 28 Nov 1995 10:35
47 lines
2274 bytes
2274 bytes
In article <Pine.A32.3.91.951127202934.29743A-100000@srv1.freenet.calgary.ab.ca> "Michael E. Wesolowski" <mewesolo@freenet.calgary.ab.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@natlab.research.philips.com)
Re: Declaring arrays inside generic function
Author: Dietmar Kuehl
Date: Wed, 29 Nov 1995 00:44
Date: Wed, 29 Nov 1995 00:44
27 lines
1824 bytes
1824 bytes
Hi, "Michael E. Wesolowski" (mewesolo@freenet.calgary.ab.ca) wrote: : 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]; Dynamically determining the size of an array allocated on the stack is a gcc extension. If you need a dynamic array, you should use 'vector' instead of a built-in array. Actually, you should not use built-in array any where else than in some classes to implement them (this is not a language requirement but results from the style built-in arrays are dealt with by the language). : 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. dcl.array verse 1: there is explicitly stated that the expression used to determine the size of an array has to be const. You can also have a look at a current discussion in comp.std.c++ concerning dynamic array bounds (for built-in arrays). The thread is titled "Array Initialization". -- dietmar.kuehl@uni-konstanz.de http://www.informatik.uni-konstanz.de/~kuehl I am a realistic optimist - that's why I appear to be slightly pessimistic
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads