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
    23,616
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    23,616
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    23,616
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    23,616
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 05:55 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

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