Results 1 to 6 of 6

Thread: Unable to calc the 'sizeof' of a member of struct (pointers problem?)

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Unable to calc the 'sizeof' of a member of struct (pointers problem?)

    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 )
    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.
    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:
    Qt Code:
    1. #include <iostream>
    2.  
    3. struct my_struct{
    4. int x, y, z;
    5. };
    6. void method( int x ){ // or by reference int &x, works as well
    7. std::cout << sizeof x << "\n";
    8. }
    9. void method( const my_struct& s ){
    10. std::cout << sizeof(s) << "\n";
    11. }
    12. int main( int argc, char ** argv ){
    13. my_struct * s = new my_struct;
    14. method((*s).x);
    15. method(s->x);
    16. method(*s);
    17. delete s;
    18. return 0;
    19. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. #include <QCoreApplication>
    2.  
    3. struct MyStruct
    4. {
    5. int a3;
    6. };
    7.  
    8. void myfunction1 (MyStruct &value)
    9. {
    10. int si = sizeof (value);
    11.  
    12. qDebug ("[%s]: Sizeof = %d", Q_FUNC_INFO, si);
    13. }
    14.  
    15. void myfunction2 (int &value)
    16. {
    17. int si = sizeof (value);
    18.  
    19. qDebug ("[%s]: Sizeof = %d", Q_FUNC_INFO, si);
    20. }
    21.  
    22. int main (int argc, char* argv[])
    23. {
    24. QCoreApplication a (argc, argv);
    25.  
    26. qDebug ("Hello World");
    27.  
    28. MyStruct *ms = new MyStruct;
    29.  
    30. myfunction1 (*ms);
    31.  
    32. myfunction2 (ms->a3);
    33.  
    34. delete ms;
    35. }
    To copy to clipboard, switch view to plain text mode 

    executes without crash with output

    Qt Code:
    1. Hello World
    2. [void myfunction1(MyStruct&)]: Sizeof = 4
    3. [void myfunction2(int&)]: Sizeof = 4
    To copy to clipboard, switch view to plain text mode 
    Last edited by mcosta; 22nd June 2011 at 18:24. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

  4. #4
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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.

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unable to calc the 'sizeof' of a member of struct (pointers problem?)

    post you code
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. Replies: 4
    Last Post: 7th June 2011, 00:46
  2. Replies: 8
    Last Post: 15th July 2010, 21:42
  3. Pointers to Member Functions
    By Doug Broadwell in forum General Programming
    Replies: 8
    Last Post: 15th May 2007, 23:08
  4. struct problem...
    By hiuao in forum General Programming
    Replies: 3
    Last Post: 5th April 2007, 07:48
  5. which one is constant,sizeof(int) or sizeof(long)?
    By coralbird in forum Qt Programming
    Replies: 4
    Last Post: 20th March 2006, 11:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.