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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:22:52+00:00 2026-05-25T16:22:52+00:00

Please read the following C++ code and the results. According to some wiki pages,

  • 0

Please read the following C++ code and the results. According to some wiki pages, static, automatic and dynamically-allocated variables are allocated in different address spaces, namely, data segment, stack and heap. However, it seems to me that the address of static and dynamic variables are at about the same place. Why is that so? How do I know the static variable is really in data segment, not in heap?

A more broad question is whether it is possible in C++ to know the range (or available size) of each address space?

Another question I have is why the address of volatile variable is 1?

#include <iostream>
using namespace std;
static int i;
int main() {
    cout << sizeof(int*) << endl;
    int j;
    int* k = new int[10];
    volatile int l;

    cout << &i << endl;
    cout << &j << endl;
    cout << k << endl;
    cout << &l << endl;

    delete[] k;
}

Results:

8
0x1000010e4
0x7fff5fbff66c
0x100100080
1
  • 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-25T16:22:53+00:00Added an answer on May 25, 2026 at 4:22 pm

    Only the OS can tell you which sections are in which parts of the address space. If you’re using Linux, From within the program, output the contents of /proc/self/maps:

    I’ve added

    std::ifstream maps("/proc/self/maps");
    std::cout << maps.rdbuf();
    

    at the end of your program, and it prints:

    8
    0x6021c0
    0x7fffe07f60bc
    0x603010
    1
    ...
    00601000-00602000 r--p 00001000 09:01 9175691   /home/cubbi/test
                      ^ -- read-only static data
    00602000-00603000 rw-p 00002000 09:01 9175691  /home/cubbi/test
                      ^^ -- writeable static data
    00603000-00624000 rw-p 00000000 00:00 0         [heap]
    ...
    7fffe07d7000-7fffe07f9000 rw-p 00000000 00:00 0  [stack]
    

    As for printing the address of a volatile int, there is no standard operator<< that takes a pointer-to-volatile-T, but there is one that takes bool, and any pointer can be implicitly converted to void*, which then can be converted to bool. To print the address you want, change that line to

    cout << const_cast<int*>(&l) << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume the following code (Please read my question in the code comments in the
I'm wondering if this code ... StringBuilder sb = new StringBuilder(Please read the following
Could someone please explain the correct usage for boost::upgrade_lock. The following code results in
Here is what I got so far. Please read the comment in the code.
I am curious to know why this is happening. Please read the code example
I read my teacher's code, but I don't understand. Could anyone please tell me
Assume that the following code is being executed by 10 threads. pthread_mutex_lock(&lock) Some trivial
Please forgive my attempts at necromancy, but I actually need to write some code
I use the following code (from Bluetooth Chat sample app) to read the incoming
please consider the following code: typedef struct Person* PersonRef; struct Person { int age;

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.