Double pointers question
-
Hi, I have this problem I can't find much information about. Suppose I have the following function:
void test(float arg[][3]) { //do nothing } ... float ** vertices = Allocate2DArray(10,3); //assume that's correct test(vertices); //doesn't work! ... float vertices2[10][3]; test(vertices2); //works
So basically in the first case it can't convert ** to [][3]. What's the difference? How can I cast it? I can't change the function because the real one is already written that way. Finally a question about freeing the memory. Suppose I haveBYTE * temp = new BYTE[TotalSize] float ** result = (float **) temp; ..followed by a loop to assign result[i] = (float *) to a location in temp[i*cols*sizeof(float)];
Now if I type delete[] result. Will that delete all the memory I allocated? After all I allocated a BYTE[], but then just did a bunch of assignments. Thanks. -
Hi, I have this problem I can't find much information about. Suppose I have the following function:
void test(float arg[][3]) { //do nothing } ... float ** vertices = Allocate2DArray(10,3); //assume that's correct test(vertices); //doesn't work! ... float vertices2[10][3]; test(vertices2); //works
So basically in the first case it can't convert ** to [][3]. What's the difference? How can I cast it? I can't change the function because the real one is already written that way. Finally a question about freeing the memory. Suppose I haveBYTE * temp = new BYTE[TotalSize] float ** result = (float **) temp; ..followed by a loop to assign result[i] = (float *) to a location in temp[i*cols*sizeof(float)];
Now if I type delete[] result. Will that delete all the memory I allocated? After all I allocated a BYTE[], but then just did a bunch of assignments. Thanks.Budric B. wrote: What's the difference? Even though they can sometimes be used interchangeably, pointers and arrays are not the same thing. One way to achieve what you want is like:
double (*vertices)[3] = (double (*)[3]) malloc(10 * sizeof(*vertices));
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 3; y++)
vertices[x][y] = 0.0; // initialize 'em
}test(vertices);
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
Hi, I have this problem I can't find much information about. Suppose I have the following function:
void test(float arg[][3]) { //do nothing } ... float ** vertices = Allocate2DArray(10,3); //assume that's correct test(vertices); //doesn't work! ... float vertices2[10][3]; test(vertices2); //works
So basically in the first case it can't convert ** to [][3]. What's the difference? How can I cast it? I can't change the function because the real one is already written that way. Finally a question about freeing the memory. Suppose I haveBYTE * temp = new BYTE[TotalSize] float ** result = (float **) temp; ..followed by a loop to assign result[i] = (float *) to a location in temp[i*cols*sizeof(float)];
Now if I type delete[] result. Will that delete all the memory I allocated? After all I allocated a BYTE[], but then just did a bunch of assignments. Thanks.FIRST PART: If test(float* in){...} vertices2 // is a pointer test(vertices2) // ok test(&vertices2) // error vertices // is a pointer to a pointer test(*vertices) // ok test(vertices) // error In this case a pointer to array of pointers, it gets a bit confusing. float* pr = vertices[r]; float f = pr[c]; Therefore, float f = (vertices[r])[c]; SECOND PART: (*result)[i] == temp[i] result[i] == &temp[i] No the == are not typos. I hope that helps. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
-
Hi, I have this problem I can't find much information about. Suppose I have the following function:
void test(float arg[][3]) { //do nothing } ... float ** vertices = Allocate2DArray(10,3); //assume that's correct test(vertices); //doesn't work! ... float vertices2[10][3]; test(vertices2); //works
So basically in the first case it can't convert ** to [][3]. What's the difference? How can I cast it? I can't change the function because the real one is already written that way. Finally a question about freeing the memory. Suppose I haveBYTE * temp = new BYTE[TotalSize] float ** result = (float **) temp; ..followed by a loop to assign result[i] = (float *) to a location in temp[i*cols*sizeof(float)];
Now if I type delete[] result. Will that delete all the memory I allocated? After all I allocated a BYTE[], but then just did a bunch of assignments. Thanks.So, what is the difference between ** and [x][y]? Well, one is a static memory location and the other are pointers.
float **x;
float y[10][3];x is a pointer to a pointer. So, what the compiler does it it goes to the address of the variable X. It then reads this address and then goes to the address stored at that location. It then uses this address as the place to store the float! If you understand single pointers you can see how the double pointer works:
First Address = *x;
Second Address = *First Address;
The Actual Float Value = *Second Address;At any level you can actually allocate a "block" of pointers that you can index into at that time. So what's the difference with an array?
y\[1\]\[2\] = 12;
Is actually
y\[3 + 2\] = 12;
It's a single block of memory! There are no pointers! In the case of even a single float *, the address of the variable stores the address that then stores the actual value! In the case of an array, the address of the variable stores the actual value! So, a multi-dimentional pointer is actually a single block of memory that is then index by the compiler as one block. So if you have: rows by columns: float x[row][column] each row is maxcolumns size. So if you want to go to rowx, coly, you could simply do rowx*maxcolumns + coly. This would index that block of memory. The compiler knows how to generate the correct code if it's an array or pointer. Remember, an array's address stores the actual data. The pointer's address, stores another address. That address then stores the data (in a single indirection case). 8bc7c0ec02c0e404c0cc0680f7018827ebee