Size of SDL_Surfaces?

This is a discussion on Size of SDL_Surfaces? within the C++ Programming forums, part of the General Programming Boards category; I was curious how much memory SDL_Surfaces use, so I tried the following: Code: int Background = sizeof(*Surface_Background); int Cards ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    61

    Size of SDL_Surfaces?

    I was curious how much memory SDL_Surfaces use, so I tried the following:

    Code:
    int Background = sizeof(*Surface_Background);
    int Cards = sizeof(*Surface_Cards);
    int Title = sizeof(*Surface_Title);
    all three of them resulted in 60 bytes, even though the images are of different dimensions. How are SDL Surfaces stored in memory and do they all use 60 bytes regardless of size??? (In my opinion hard to believe, but I'm a novice programmer, so...)

    Another question: The way I see it a boolean variable should only use one bit, but I read somewhere that the smallest amount of memory which can be allocated is a byte, therefore a bool variable uses a byte. I tried this:

    Code:
    bool Test1;
    bool Test2[8];
    
    cout << sizeof(Test1) << endl;
    cout << sizeof(Test2) << endl;
    and it displayed 1 and 8 respectively. My question is, since a boolean technically only uses a bit, shouldn't an array of 8 bools still only allocate 8 bits or 1 byte?
    Last edited by LyTning94; 07-28-2011 at 01:46 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    It's basically like this
    Code:
    char *p = malloc(10);
    char *q = malloc(10000);
    You can do sizeof(p), sizeof(*p), and the same for q,*q and get the respective same answer for both.
    sizeof() cannot tell you about how much additional memory has been allocated.

    A surface by it's nature is going to have a variable component (the image data), so it will be the case that the variably sized aspects of this data will be allocated at run-time, and all that will be in the struct is a pointer to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by LyTning94 View Post
    Another question: The way I see it a boolean variable should only use one bit
    Good for you but you are wrong. See the rest of your sentence below.

    Quote Originally Posted by LyTning94 View Post
    I read somewhere that the smallest amount of memory which can be allocated is a byte, therefore a bool variable uses a byte.
    The default packet for information in a computer is a byte, there were many decisions leading up to this which you can read about by googling around for computer history.

    I believe the actual size of a bool is left up to the implementation, the only requirement per standard is that it is capable of holding 1 or 0.

    Quote Originally Posted by LyTning94 View Post
    My question is, since a boolean technically only uses a bit, shouldn't an array of 8 bools still only allocate 8 bits or 1 byte?
    The answer to this should be self evident now.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of array - why function gives size ONE only
    By noob123 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2009, 04:20 PM
  2. Size of fixed size object?
    By shwetha_siddu in forum C Programming
    Replies: 2
    Last Post: 04-08-2009, 02:39 AM
  3. Replies: 3
    Last Post: 08-13-2006, 05:58 AM
  4. Finding Words in a array[size][size]
    By ^DJ_Link^ in forum C Programming
    Replies: 8
    Last Post: 03-08-2006, 02:51 PM
  5. window size VS. pixel size
    By dug in forum Windows Programming
    Replies: 2
    Last Post: 08-26-2003, 06:30 AM

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