sizeof a struct

This is a discussion on sizeof a struct within the C++ Programming forums, part of the General Programming Boards category; Given that I'm trying to acquire the information of the size of this struct: Code: struct Point2D {int x,y;}; Logically ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    sizeof a struct

    Given that I'm trying to acquire the information of the size of this struct:

    Code:
    struct  Point2D {int x,y;};
    Logically thinking, it should be the size of 2 ints. However, when I checked in the debugger, it gives me a value of 0 when checking its size using sizeof(). Is this normal, or is there something wrong with how I used the struct to get a sizeof = 0?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    So you are saying that this prints 0 for you?
    Code:
    #include <iostream>
    
    struct  Point2D {int x,y;};
    
    int main()
    {
        std::cout << sizeof(Point2D) << std::endl;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I checked with a debugger again by assigning its value to a variable. That variable isn't 0. But when I checked "sizeof(Point2D)" in the debugger, it gives me a value of 0. My confusion is with what the debugger is doing.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    Maybe the problem is that the debugger is (or you are, when using the debugger) a little confused by the fact that sizeof is a compile time operator rather than a function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    But how does that explain the case when "sizeof(int)" does not return 0 rather than a 4 when I evaluated that value in the debugger?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    Post the smallest and simplest compilable program that demonstrates this problem. It might also be a good idea to post about which debugger you used and how you went about using it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I was using Visual Studio debugger. I just typed in sizeof(whatever) in the watch window. Not sure how posting a simple compilable program would help.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    Quote Originally Posted by stanlvw
    I just typed in sizeof(whatever) in the watch window. Not sure how posting a simple compilable program would help.
    I have a feeling that this might be the problem: it just does not make sense to me to try and run the debugger when you do not have a compilable program to run. Presumably sizeof(int) works because that is hard coded into the debugger.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  3. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21