Unable to calc the 'sizeof' of a member of struct (pointers problem?)
I have this 2 functions
myfunction1 (myclass &value)
myfunction2 (int &value)
I want to pass the values by ref to avoid copies.
Ok, I have a object created like this
a_struc * my_struc= new a_struc;
(a_struc is a simple struct with 3 fields, the third is an int field called a3. )
myfunction1(*mystruc) works
myfunction2(*mystruc->a3) does not compile
myfunction2(mystruc->a3) compile, but inside myfunction2 I have :
int si = sizeof value; and in this last case I have an error ....(programs crash)
Any help? Thanks
Re: Unable to calc the 'sizeof' of a member of struct (pointers problem?)
Quote:
I want to pass the values by ref to avoid copies.
Passing an int by reference is not going to give you some incredible increase in app performance :P
btw if arguments are meant to be read-only, pass them by const reference ( for classes and structs ) or value (ints, doubles etc )
Quote:
myfunction2(*mystruc->a3) does not compile
Correct syntax would be myfunction2(mystruc->a3) or myfunction2((*mystruc).a3), but I think you've already figured it out.
Quote:
but inside myfunction2 I have :
int si = sizeof value; and in this last case I have an error ....(programs crash)
Make sure pointer is not deleted before function call, this works ok:
Code:
#include <iostream>
struct my_struct{
int x, y, z;
};
void method( int x ){ // or by reference int &x, works as well
std::cout << sizeof x << "\n";
}
void method( const my_struct& s ){
std::cout << sizeof(s) << "\n";
}
int main( int argc, char ** argv ){
my_struct * s = new my_struct;
method((*s).x);
method(s->x);
method(*s);
delete s;
return 0;
}
Re: Unable to calc the 'sizeof' of a member of struct (pointers problem?)
In myfunction1 and 2 value is a reference to myclass and integer so sizeof returns 4 for 32bit or 8 for 64bit
This code
Code:
#include <QCoreApplication>
struct MyStruct
{
int a3;
};
void myfunction1 (MyStruct &value)
{
int si = sizeof (value);
qDebug ("[%s]: Sizeof = %d", Q_FUNC_INFO, si);
}
void myfunction2 (int &value)
{
int si = sizeof (value);
qDebug ("[%s]: Sizeof = %d", Q_FUNC_INFO, si);
}
int main (int argc, char* argv[])
{
qDebug ("Hello World");
MyStruct *ms = new MyStruct;
myfunction1 (*ms);
myfunction2 (ms->a3);
delete ms;
}
executes without crash with output
Code:
Hello World
[void myfunction1(MyStruct&)]: Sizeof = 4
[void myfunction2(int&)]: Sizeof = 4
Re: Unable to calc the 'sizeof' of a member of struct (pointers problem?)
I'm going to stop ....
I hope to solve this tomorrow ...
There is something strange, I dont know where is the problem but code that works fine (as the examples you have showed) does not work into my classes ...
Thanks in any way.
Re: Unable to calc the 'sizeof' of a member of struct (pointers problem?)
sizeof() is often implemented as a macro, and requires parentheses around the argument. Your psuedo-code doesn't do this; if your code doesn't, that's likely the problem.
Re: Unable to calc the 'sizeof' of a member of struct (pointers problem?)