C Question - Global Arrays

Soldato
Joined
22 Oct 2005
Posts
2,883
Location
Moving...
I want to define an array globally, but I don't know the size of the array initially. I have a function that calculates the required array size in the program, I want to set the size of it after I have executed this function.

How can you define an array globally without specifying it's size at the same time? Is this is even possible? If not, what else can I do apart from creating the array locally and passing it to every function that needs it?

(I'm using straight C btw)

Thanks for any help.
 
I've never used C, but shouldn't this work?
Code:
int arraySize = something; // Dynamically obtained array size.
SomeType* array = malloc(arraySize * sizeof(SomeType));
 
Last edited:
Define the array globally as:

Code:
char* myArray;

then as soon as you know the size:

Code:
myArray = (char*)malloc(numberOfThings * sizeof(char));
 
Thanks again voodooflux. Works perfectlly.

As you may have guessed I'm a bit of a novice, would you mind explaining how this works briefly?

I understand that malloc is used basically to allocate a certain amount of space, the space it creates is based on what's in the brackets. That I understand.

The thing I dont understand is how you've defined a pointer (i think) with the code char* myArray, and basically created an array from that. The only difference I can see is that when I go to debug it, I can only view one element. It still seems to work in exactly the same way as an array:confused:
 
Array's are basically just a specific version of pointers, that are a fixed size.

So defining:

char foo[10]

Or doing:

char *foo

foo = malloc (10 * sizeof(char))

in terms of memory are the same thing, however when debugging the reason you can view all 10 values in the array but not all 10 in the pointer is just that the debugger *knows* that the array is the size of 10 chars, whilst the pointer can be anything, and can be changed dynamically whilst the program is running.

If that makes sense...
 
Array's are basically just a specific version of pointers, that are a fixed size.

So defining:

char foo[10]

Or doing:

char *foo

foo = malloc (10 * sizeof(char))

in terms of memory are the same thing, however when debugging the reason you can view all 10 values in the array but not all 10 in the pointer is just that the debugger *knows* that the array is the size of 10 chars, whilst the pointer can be anything, and can be changed dynamically whilst the program is running.

If that makes sense...

To clarify: a fixed-length array must be created with a constant length, i.e. a number that is known at compile time (e.g. a literal or const variable), so the debugger knows exactly how long the array is and so can show you each of the elements.

A dynamic array, however, can be created with dynamically allocated memory, whose length does not have to be known at compile time. Because of this, the debugger can't infer the length of the array from the array itself; only its starting address.
 
Array's are basically just a specific version of pointers, that are a fixed size.

So defining:

char foo[10]

Or doing:

char *foo

foo = malloc (10 * sizeof(char))

in terms of memory are the same thing
I don't think they are exactly the same. The memory is allocated in different places. When you do malloc or calloc, memory is allocated on the heap and you have to make sure you free that space up at some point. If you declare an array within a function, it gets put on the stack and will get freed when the system exits that function. Not sure what happens when you declare globally, they might be equivalent then.

Can anyone confirm I'm not talking out of my bottom? I'm not 100% sure of this.
 
Back
Top Bottom