Click to See Complete Forum and Search --> : indexing dynamic arrays in C
hi, if i have a piece of memory that gets malloc'ed 12 floats is array[1][5] the same as array[11]?? It would be more logical and understanable if i could index the memory as if it where 2 sections of 6 floats rather than 12 floats in a row.
MrNewbie
07-01-2001, 08:47 PM
When you declare arrays you need to declare the number of elements and pretend it doesnt start with 0, if you want an array with 6 elements you'd do int array[6]; which would then be accessed by array[0] .. array[5]. But to answer you're question, they are baically the same in that you will be able to store the same amount of things in float array[12]; as you could in float array[2][6];
[ 01 July 2001: Message edited by: MrNewbie ]
Energon
07-02-2001, 12:55 AM
array[2][6] takes more memory because you have 2 additional "pointers" along with 2 6 blocks of memory... array[12] is just one contiguous block of memory...
jemfinch
07-02-2001, 01:09 AM
Originally posted by Energon:
array[2][6] takes more memory because you have 2 additional "pointers" along with 2 6 blocks of memory... array[12] is just one contiguous block of memory...
Actually, according to "The C Programming Language", array[2][6] is one contiguous block of memory, and the [x][y] notation is simply syntactical sugar for "x times (the dimension of the second index) plus y".
Jeremy
TheLinuxDuck
07-02-2001, 09:49 AM
siqe:
Would it be more logical to use [2][6], instead of [12]? Well, I suppose that totally depends on what you're wanting to do. Depending on the applicattion of the array, you could either one, and it could be logical in either case.
If you just had 12 numbers that you wanted to do some calculation with, it wouldn't really make sense to have to loop through two dimensions to deal with the numbers, since it's more expensive (in terms of CPU cycles):
// with array[12]
//
int j,total=0;
for(j=0;j<12)++j) {
total+=array[j];
}
// with array[2][6]
//
int j,k,total=0;
for(j=0;j<2;++j) {
for(k=0;k<6;++k) {
total+=array[j][k];
}
}
I guess the best way to determine which way is better, is to see if the items of the array should be grouped, or if they are all of one type.
It doesn't make sense to unecesarily group items of an array into multi-dimensions if there is no distinct difference of category.
Just MHO, howver.. (^= it will work either way. (^=
for 1 degrees i need to have sin(1), sin(-1), -sin(1), -sin(-1), cos(1), and cos(-1)... then for 2 degrees i need sin(2), sin(-2), -sin(2), -sin(-2), cos(2), and cos(-2)... on and on for an aribitrary number of degrees (the user decides). So array[x][6] would be nice.
Energon
07-02-2001, 04:41 PM
Originally posted by jemfinch:
<STRONG>Actually, according to "The C Programming Language", array[2][6] is one contiguous block of memory, and the [x][y] notation is simply syntactical sugar for "x times (the dimension of the second index) plus y".
Jeremy</STRONG>
Really? hmm... I wonder if Java does it the same... it uses a "pointer" method, but I don't know if it just does it in a block or not...
float *array;
array = (float *)malloc(sizeof(float) *100));
free(array);
is it part of standard C that array will equall NULL after the call to free, or is this compiler specific?
TheLinuxDuck
07-02-2001, 05:57 PM
I wouldn't count on the pointer being NULL after a call to free. I don't know the answer to the question you're asking, but, if it were me, I'd prolly double up by doing:
float *fptr=NULL;
// init
//
fptr=(float *)malloc(sizeof(float)*100);
if(fptr==NULL) {
perror("Cannot alloc memory");
return;
}
//
// use variable
// insert code here
//
// free it
//
free(fptr);
fptr==NULL;
That way, you're always guaranteed to know if it's going to be NULL or not.
Just my perspective.. (^=
Energon
07-02-2001, 07:32 PM
Well, all I have here is the MSDN, so bear with me... this is what it says:
The free function deallocates a memory block (memblock) that was previously allocated by a call to calloc, malloc, or realloc. The number of freed bytes is equivalent to the number of bytes requested when the block was allocated (or reallocated, in the case of realloc). If memblock is NULL, the pointer is ignored and free immediately returns. Attempting to free an invalid pointer (a pointer to a memory block that was not allocated by calloc, malloc, or realloc) may affect subsequent allocation requests and cause errors.
So, in theory, all it does is deallocate the memory without actually editing the space that the point points to... and in a way, that makes sense since if you reallocate the memory, you've already got a contiguous block that it's pointing to and it doesn't have to go find another...
Though the best way to find out is is:
free(array);
if(array == NULL)
printf("pointer is NULL");
tada... :)
[ 02 July 2001: Message edited by: Energon ]
sans-hubris
07-02-2001, 09:57 PM
free is implementation specific. Borland just deallocates the memory (I think.) MSVC++ and GCC point the pointer back to NULL.