Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 876167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:23:27+00:00 2026-05-15T11:23:27+00:00

SDL provides me this struct: typedef struct SDL_Rect { Sint16 x, y; Uint16 w,

  • 0

SDL provides me this struct:

typedef struct SDL_Rect {
    Sint16 x, y;
    Uint16 w, h;
} SDL_Rect;

I want to create a new SDL_Rect on the heap as class variable:

// Forward declaration
private:
    SDL_Rect *m_pcScreenRect;

And in the constructor I do this:

/* Screen Rectangle (for clearing) */
m_pcScreenRect = new SDL_Rect;
m_pcScreenRect->x = 0;
m_pcScreenRect->y = 0;
m_pcScreenRect->w = 800;
m_pcScreenRect->h = 600;
printf("Rect(x:%d, y:%d, w:%d, h:%d)\n", m_pcScreenRect->x, m_pcScreenRect->y, m_pcScreenRect->w, m_pcScreenRect->h);

Which prints Rect(x:0, y:0, w:800, h:600)
So this is correct.

Problem 1

But when I don’t initialize x and y, it prints rubbish numbers like:

 Rect(x:-11280, y:63, w:800, h:600)
 Rect(x:25584, y:167, w:800, h:600)
 Rect(x:-11280, y:40, w:800, h:600)
 // This is just, run, terminate, run, terminate, ....

And I thought the default value for an int is 0?

Problem 2

In my gameloop, I have the same line to check the values of the SDL_Rect. In the loop I get results like this:

Clear (x:0, y:40, w:0, h:560)
Clear (x:0, y:99, w:0, h:501)
Clear (x:0, y:55, w:0, h:545)
Clear (x:0, y:55, w:0, h:545)
// Again: run, terminate, run, terminate....

When my constructor looks like this:

/* Screen Rectangle (for clearing) */
m_pcScreenRect = new SDL_Rect;
//m_pcScreenRect->x = 0;
//m_pcScreenRect->y = 0;
m_pcScreenRect->w = 800;
m_pcScreenRect->h = 600;

And I get normal results when I uncomment the two lines:

/* Screen Rectangle (for clearing) */
m_pcScreenRect = new SDL_Rect;
m_pcScreenRect->x = 0;
m_pcScreenRect->y = 0;
m_pcScreenRect->w = 800;
m_pcScreenRect->h = 600;

Does this problem something have to do with new or with the datatypes (Uint16, and a normal int). If it is with the datatypes, how to solve it?

Thanks would be very appreciated! (Haha!)

Thanks,
Any help would be very appreciated!


Extra Question:

I have to define all my variables in C++.
But from where do the random numbers come?

I’m using g++ and gcc in Linux.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:23:28+00:00Added an answer on May 15, 2026 at 11:23 am

    In order to make use of the "default value for an int", you can use value initialization:

    m_pcScreenRect = new SDL_Rect(); // this will initialize to zeros
    

    Otherwise, the values are not defined.

    To give more details: adding a constructor to this structure would be the best approach, but since it’s in a third-party library which you (I suppose) cannot modify, you can either wrap it in your own class that provides a default constructor, or use value-initialization. To quote C++03 8.5/5,

    To value-initialize an object of type T means:

    • if T is a class type with a user-declared constructor, then the default constructor for T is called

    • if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;

    • if T is an array type, then each element is value-initialized;

    • otherwise, the object is zero-initialized

    In your case, SDL_Rect is a non-union class type without a user-declared constructor (second clause in this list), meaning value-initialization is performed for its non-static data memebers, and each of those data members falls under the "otherwise" clause, and is zero-initialized (which is defined as "if T is a scalar type, the object is set to the value of 0 (zero) converted to T;")

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SDL 1.3 I want to create fake fullscreen SDL_Window under linux. It is
I am using SDL tridion 2011 SP1. I want to get the list of
I'm using the SDL library to try to create a rendering context in a
This is an SDL problem, however I have the strong feeling that the problem
I am building a GUI extension using SDL Tridion 2011 SP1. I want to
I am using SDL audio to play sounds. SDL_LockAudio tells this : Do not
I'm trying to create an SDL drawing canvas inside of a simple QT4 window,
In SDL Tridion 2011 SP1, I want to allow custom XML tags (nodes) inside
How does Allegro or SDL create window for Windows and how to do it
I am new to SDL, and I am just curious why does sdl use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.