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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:51:54+00:00 2026-05-27T15:51:54+00:00

int* pi; { int ar[1000000]; int a =3,b=4; ar[3]=a*b pi=ar; }//ar is destroyed int

  • 0
int* pi;
{
    int ar[1000000];
    int a =3,b=4;
    ar[3]=a*b
    pi=ar;
}//ar is destroyed
int ar2[]={5,6,7,8,9};
char* f="zcxzsdaaaaaaaaa";
std::cout<<pi[3]<<std::endl;// prints 12

I have 2 questions:

  1. I heard that the stack contains only pointers to data. And if so where is the data stored? For example char* a="bbbb"; a – placed on stack, “bbbb” – somewhere else. Where?

  2. Doesn’t the above code working correctly mean a memory leak of 1000000 bytes? Variable ar is destroyed but the data that it pointed to still exists. And we can’t use delete here since ar is not dynamically allocated.

  • 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-27T15:51:55+00:00Added an answer on May 27, 2026 at 3:51 pm

    I heard that stack contain only pointers to data

    You heard wrong. The stack contains the actual data. However, if that data is a pointer then that is what is stored.

    And if so where is data stored? For example char* a=”bbbb”; a – placed on stack, “bbbb” – somewhere else. Where?

    Yes, a (the pointer) is stored on the stack. The actual string "bbbb" is stored in a fixed part of the executable.

    Doesnt code above working correctly mean memory leak of 1000000 bytes? Variable ar is destroyed but data that it pointed to still exists. And we cant use delete here since ar is not dynamically allocated.

    No, there is a difference between arrays and pointers to arrays. ar (the whole 1000000 bytes) will be stored on the stack. This is different from char const* ar = "... 1000000 chars ...";. As ar is on the stack, it will be “freed” automatically.

    char const* a = "abcde"; // a is on the stack, pointing to "abcde" somewhere else.
    char const b[6] = "abcde"; // *all* of b is on the stack, all 6 bytes
    

    The problem in your code is that pi is pointing to something on the stack which is no longer there. It may well be there when you run the code because “freeing” data on the stack doesn’t do anything to the data in non-debug builds. It’s not a memory leak, you just have an invalid pointer.

    Final note: Although essentially all modern computer architectures make use of a call stack, the C++ standard makes no mention of it. Note that people will often say that a variable is “on the stack”, but it may actually just live in a register. For example, if you compiled your code, the variable pi probably wouldn’t ever touch the stack, it would likely just stay in a register for the duration of the function because going to the stack is relatively expensive (compared to registers).

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

Sidebar

Related Questions

int main(void) { std::string foo(foo); } My understanding is that the above code uses
(int)(33.46639 * 1000000) returns 33466389 Why does this happen?
int main(void) { char tmp, arr[100]; int i, k; printf(Enter a string: ); scanf_s(%s,
I have two ways to program the same functionality. Method 1: doTheWork(int action) {
I have a program that has to declare a huge integer array of the
Let's say I have a sequence. IEnumerable<int> sequence = GetSequenceFromExpensiveSource(); // sequence now contains:
I have this problem to solve that I have no idea how to do
I have the following C++ code that tried to generate a random number. The
So, my doctor asking me to implement treeSort() and then test it on int[1000000]
I have a class that does some time-consuming calculations. I'm trying to performance test

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.