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 9181141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:12:58+00:00 2026-06-17T18:12:58+00:00

Could you help me understand where variables are stored (stack, heap, static memory). How

  • 0

Could you help me understand where variables are stored (stack, heap, static memory).
How can I determine it? I mean not intuitively but I would like to have some sign printed on the screan what is where. Is it possible?

So far I tried to have a look at where the variables are stored by printing their addresses.
But that didn’t give me much. Could you have a look and give me a piece of advice.
If I’ve made a mistake (see my comments to the program), please tell me about that.

#include "stdafx.h"
#include <iostream>
using namespace std;

int * p1 = new int [3]; // Static memory as it is a global array;
namespace P {int * p2 = new int[3];} // Static memory as it is a namespace; 
namespace {int * p3 = new int[3];} // Static memory as it is a namespace;
using namespace P;

int _tmain(int argc, _TCHAR* argv[])
{
    int * p4 = new int[3]; // Heap as there is a new operator.
    static int * p5 = new int [3]; // Static memory as the variable is static.

    cout << "p1: "<< p1 << endl;
    cout << "p2: "<< p2 << endl;
    cout << "p3: "<< p3 << endl;
    cout << "p4: "<< p4 << endl;
    cout << "p5: "<< p5 << endl;
    cout << endl; 
    cout << "p5 - p4: " << p5 - p4 << endl;
    cout << "p4 - p3: " << p5 - p4 << endl;
    cout << "p3 - p2: " << p5 - p4 << endl;
    cout << "p2 - p1: " << p5 - p4 << endl;

    system("pause");
}
  • 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-06-17T18:13:00+00:00Added an answer on June 17, 2026 at 6:13 pm

    Only the pointer is static here. The stuff it points to is on “the heap”, meaning it is dynamically allocated and the caller needs to take care of de-allocating. Where the data actually is is another matter. It depends on the platform, and on what new is defined to do.

    static int * p5 = new int [3]; 
    

    Have a look at this GotW. Thanks to @AlokSave for posting it in the comments.

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

Sidebar

Related Questions

I wonder if anyone can help me to understand where I could be going
So I'm just learning Forth and was curious if anyone could help me understand
Could someone help me understand the primitive accessors with this example : i don't
Please could someone help me understand why the div.fl element shown in Developer Tools
Could anyone help me to understand following line of code: sol< ?=f((1<< n)-1,i,0)+abs(P[i])*price; I
Im starting to understand how Magento Event Observers work and hoping someone could help
I was wondering if anyone could help me understand if what I am doing
I do not understand why the main method has to be static. I understand
I was reading this link http://dec.bournemouth.ac.uk/staff/awatson/micro/articles/9907feat2.htm I could not understand this following statements from
Can someone please help me understand the following expression? printf(%3d - %s\n, counter, name)

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.