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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:22:01+00:00 2026-05-27T07:22:01+00:00

bool example1() { long a; a = 0; cout << a; a = 1;

  • 0
bool example1()
{
    long a;
    a = 0;
    cout << a;
    a = 1;
    cout << a;
    a = 2;
    cout << a;
    //and again...again until
    a = 1000000;
    cout << a+1;
    return true;
}
bool example2()
{
    long* a = new long;//sorry for the misstake
    *a = 0;
    cout << *a;
    *a = 1;
    cout << *a;
    *a = 2;
    cout << *a;
    //and again...again until
    *a = 1000000;
    cout << *a + 1;
    return true;
}

Note that I do not delete a in example2(), just a newbie’s questions:
1. When the two functions are executing, which one use more memories?
2. After the function return, which one make the whole program use more memories?
Thanks for your help!

UPATE: just repace long* a; with long* a = new long;
UPDATE 2: to avoid the case that we are not doing anything with a, I cout the value each time.

  • 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-27T07:22:02+00:00Added an answer on May 27, 2026 at 7:22 am

    Original answer

    It depends and there will be no difference, at the same time.

    The first program is going to consume sizeof(long) bytes on the stack, and the second is going to consume sizeof(long*). Typically long* will be at least as big as a long, so you could say that the second program might use more memory (depends on the compiler and architecture).

    On the other hand, stack memory is allocated with OS memory page granularity (4KB would be a good estimate), so both programs are almost guaranteed to use the same number of memory pages for the stack. In this sense, from the viewpoint of someone observing the system, memory usage is going to be identical.

    But it gets better: the compiler is free to decide (depending on settings) that you are not really doing anything with these local variables, so it might decide to simply not allocate any memory at all in both cases.

    And finally you have to answer the “what does the pointer point to” question (as others have said, the way the program is currently written it will almost surely crash due to accessing invalid memory when it runs).

    Assuming that it does not (let’s say the pointer is initialized to a valid memory address), would you count that memory as being “used”?

    Update (long* a = new long edit):

    Now we know that the pointer will be valid, and heap memory will be allocated for a long (but not released!). Stack allocation is the same as before, but now example2 will also use at least sizeof(long) bytes on the heap as well (in all likelihood it will use even more, but you can’t tell how much because that depends on the heap allocator in use, which in turn depends on compiler settings etc).

    Now from the viewpoint of someone observing the system, it is still unlikely that the two programs will exhibit different memory footprints (because the heap allocator will most likely satisfy the request for the new long in example2 from memory in a page that it has already received from the OS), but there will certainly be less free memory available in the address space of the process. So in this sense, example2 would use more memory. How much more? Depends on the overhead of the allocation which is unknown as discussed previously.

    Finally, since example2 does not release the heap memory before it exits (i.e. there is a memory leak), it will continue using heap memory even after it returns while example1 will not.

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

Sidebar

Related Questions

bool operator()(Iterator it1, Iterator it2) const { return (*it1 < *it2); } Can someone
Whew, that was a long title. Here's my problem. I've got a template class
My question (which will follow after this, sorry about the long intro, the question
I've got the object User with the following attributes: Id(long), Name(string), e-mail(string), activated(bool). I
For example: - (BOOL)compare:(NSDecimal)leftOperand greaterThan:(NSDecimal)rightOperand { NSComparisonResult result = NSDecimalCompare(&leftOperand, &rightOperand); // rest not
Let us pick the following Win API call as an example: BOOL MessageBeep(UINT uType);
Example: public bool Save(MyObj instance) { if (instance.IsNew) { this.repository.Create(instance); } else { this.repository.Update(instance);
For example, do I need to lock a bool value when multithreading?
For example, I try to do something like this: - (BOOL)compare:(NSDecimal)leftOperand greaterThan:(NSDecimal)rightOperand { BOOL
In C#, for example, when I compare two nullable booleans ( bool? ) I

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.