|
Hi,
Yes I did. It crashes as I use the memory passed back. If I look at the memory it has f [0x0012fe30 -> 0x3f800000]. The memory address of 0x0012fe30 corresponds to the memory in the class.
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
Hmmm, it is difficult to guess what is happening. Post the code in which you use the array (where you put stuff inside it).
Off topic: I like your sig
|
|
|
|
|
Sure:
<br />
void main (void)<br />
{<br />
float **f;<br />
A id;<br />
f = id.GetData ();<br />
<br />
cout << f[0][0] << endl;<br />
}<br />
I have also just noticed that the error code is:
Unhandled exception at 0x004150e0 in agile_tests.exe: 0xC0000005: Access violation reading location 0x3f800000.
Sorry for the misleading info before.
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
looking in the debugger, is id well constructed so ?
TOXCCT >>> GEII power [toxcct][VisualCalc 2.20] | soon : [VisualCalc 3.0]
|
|
|
|
|
Yes, I checked and with the simple code he gave the program crashes. The array inside the class is correct (except that the data is not initialized but the array itself is valid). The problem appear because of the casting from float[4][4] to float** . At this point, the pointers becomes invalid (even if the it points to the same memory ). Probably because the two arrays are represented differently in memory (I didn't knew that)
|
|
|
|
|
Yes, id is constucted fine. If I use data[r][c] it is fine. Inside the class there are no problem with the data.
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
Ok, I created a new project to check your code and yes, I have the same problem. It comes from the casting of float[4][4] to float** (never had the problem before because I never use fixed sized double dimensions arrays). Maybe the best way to do it would be to use dynamic arrays:
class A<br />
{<br />
protected:<br />
float** data;<br />
}
Then, in the constructor of your class, create the array:
A::A()<br />
{<br />
data = new float[4];<br />
for(int i=0;i<4;i++)<br />
data[i] = new float[4];<br />
}
This will create a float[4][4] array (but still, the data in it is unitialized).
Don't forget to free the memory in the destructor of your class:
A::~A()<br />
{<br />
if (data)<br />
{<br />
for (int i=0;i<4;i++)<br />
{<br />
if (data[i])<br />
delete[] data[i];<br />
}<br />
<br />
delete[] data;<br />
}<br />
}
|
|
|
|
|
Ok, but how do you get double dimension arrays of constant size out of a class? It must be some how possible?
Thanks for the help btw
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
Hi, I have a solution, but I don't know if you'll like it.
#include <iostream>
class A
{
public:
A(){ for( int i = 0; i < 4; i++ ) for( int j = 0; j < 4; j++ ) data[ i ][ j ] = 0; }
float** GetData()
{
float **g = new float*[ 4 ];
for( int i = 0; i < 4; i++ )
g[ i ] = data[ i ];
return g;
}
protected:
float data[ 4 ][ 4 ];
};
using namespace std;
int main( int nargs, char * cargs[] )
{
A id;
float** f = id.GetData();
cout << f[ 0 ][ 0 ] << endl;
delete [] f;
return 0;
} As far as I can tell, when you cast data as a float**, it interprets the value of data[ 0 ][ 0 ] as f[ 0 ]. Since data[ 0 ][ 0 ] == 0, then f[ 0 ] becomes the null pointer for the first row of the array. In my solution, you create the pointer array of pointers and initialize the values of the row pointers to the correct memory locations of data.
Of course, you have to delete the returned double pointer's array.
Hope that helps.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
Hi,
Currently I have this way implemented but I was hoping that there could just get around it, as subsiquent posts have revealed.
Thanks anyway,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
No problem. Hope all turns out well. I, too, like that code below. <smile />
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
To access element x, y in the returned float** , you should do this :-
*((float*)f + x*4 + y)
e.g.
float **f;
A id;
f = id.GetData();
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
cout << *((float*)f + i*4 + j) << " ";
}
cout << endl;
}
|
|
|
|
|
Hi,
Is this not the same as f[0][0] or do float** work differently?
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
you need to return a pointer to the array like the following. Note that I use a typedef for the array type as it makes returning a pointer to an array easier to read.
#include <stdio.h>
typedef float FixedFloatArray[4][4];
class A
{
public:
A()
{
for(int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
data[i][j] = (float)(i * 4 * j);
}
}
}
FixedFloatArray* GetData() { return &data; }
protected:
FixedFloatArray data;
};
void main()
{
A foo;
FixedFloatArray* data = foo.GetData();
printf("%f\n", (*data)[1][1]);
}
The basic problem with the way you were trying to do it is that the compiler doesn't know how big the rows of the array are at the GetData callsite. So, while the code you wrote looks "correct", it isn't because the compiler doesn't how to write code that will correctly address the elements of the array. Remember this when thinking that pointers are equivalent to arrays. They aren't.
|
|
|
|
|
Yep, your way is the right one, but if you know the length of the dimensions of the array, you can get at ecah element (as I've shown in my post).
|
|
|
|
|
|
another way to do this is to overload the array operator "[]" in the class like the following:
class A
{
float* operator[](int row)
{
return data[row];
}
};
void main()
{
A foo;
printf("%f\n", foo[1][1]);
}
|
|
|
|
|
That is quite cool, never thought of doing that
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
Excellent,
Thank-you
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
TBD.
modified 28-Jan-14 19:18pm.
|
|
|
|
|
|
i am using a ddl to call other dll to show a form, that´s ok.But the problem is, i can not give the focus to the form, if there are more forms or programs opened the form that i call stay under the others , i want it to apears in front of all the others things opened,i have already used ShowModal but it does not work, can you help me, i am using C++ Builder(Borland) in windows 2000.
tks
|
|
|
|
|
This forum is for C++/CLI related questions. While none of the forums really handle C++ Builder queries, you could try the Visual C++ forum.
|
|
|
|
|
I want to develop a firewall software with C++ Builder.But i don't know how can i start.Must i use a driver or anything else (Ndis,filter hook ....)Please tell me how can i start? What i need ?
what is ndis and winpkfilter ?
|
|
|
|
|
I doubt that C++ Builder supports C++/CLI. (i.e. please don't flood the forums )
Christian Graus - Microsoft MVP - C++
|
|
|
|