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

  • Home
  • SEARCH
  • 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 3677422
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:13:11+00:00 2026-05-19T03:13:11+00:00

in this example, even though i will never use the variables WNDCLASSEX, x, y,

  • 0

in this example, even though i will never use the variables WNDCLASSEX, x, y, cx, cy, they will still use memory when i’m in the message loop:

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpArgs, int iCmdShow)
    {
     WNDCLASSEX wc;
     ...
     RegisterClassEx(&wc);

     const int cx = 640;
     const int cy = 480; 
     // center of the screen
     int x = (GetSystemMetrics(SM_CXSCREEN) - cx) / 2;
     int y = (GetSystemMetrics(SM_CXSCREEN) - cy) / 2;

     CreateWindow(..., x, y, cx, cy, ...);

     MSG msg;

     while (GetMessage(&msg, NULL, 0, 0) > 0)
     {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
     }
     return 0;
    }

But i’m wondering, if i put them in a scope, would they still use memory during the message loop? e.g.

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpArgs, int iCmdShow)
{
 {
  WNDCLASSEX wc;
  ...
  RegisterClassEx(&wc);

  const int cx = 640;
  const int cy = 480; 
  // center of the screen
  int x = (GetSystemMetrics(SM_CXSCREEN) - cx) / 2;
  int y = (GetSystemMetrics(SM_CXSCREEN) - cy) / 2;

  CreateWindow(..., x, y, cx, cy, ...);
 }

 MSG msg;

 while (GetMessage(&msg, NULL, 0, 0) > 0)
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
 return 0;
}

or maybe if i put them into two functions and called them in winmain e.g.

wnd_register(hInst);
wnd_create(hInst);

would that prevent them from using the memory?

  • 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-19T03:13:12+00:00Added an answer on May 19, 2026 at 3:13 am

    The compiler has a lot of leeway for handling simple locals, like you have in your examples. They may live on the stack, they may only exist as immediate values in the machine code, or they may just live in registers. Stack space is usually allocated on entry to a function. The compiler will subtract some value from the stack pointer to make space for all the locals. On return of the function, the stack pointer is restored back to its original value. This is not usually done on exit of different scope blocks. Most compilers will try to aggressively reuse stack space as soon as variables are no longer used. In your example, it’d be perfectly legal for x and msg to have the exact same address on the stack, since their usage is non-overlapped.

    My answer to this question goes into more detail on how local variables are allocated on the stack.

    In your examples, the constants, cx and cy, most likely will have no memory backing them at runtime, and just be immediate values in the generated code. x and y will most likely live in registers until they need to be pushed on the stack for the call to CreateWindow. wc and msg will almost definitely be on the stack.

    You shouldn’t worry about micro-optimizations at this level – let the compiler allocate space for local variables as it sees fit. You have a 1 MB stack by default, the amount of data consumed by these variables wouldn’t even register as noise. Spend your time worrying about more interesting problems instead.

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

Sidebar

Related Questions

I've got a very simple program written in C#, but the loop never exits
I have a little web app that uses the Facebook like widget. This page
I'm trying to use a case statement to make some code more readable. It
This is my code, which is supposed to accept input from the user and
I'm trying to make sure that a Rapidshare URL is valid when a user
I am working on a site that has a database schema like a tree.
I am trying to write a SQL query to fetch rows whose fields contain
What I want to achieve is having a ListFragment which displays data loaded from
I have a small application developed in ASP.NET 2.0 WebForms. For learning purpose, I
I have a working process, behind a web application, that is generating some records

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.