Go Premium for a chance to win a PS4. Enter to Win


A memory allocation question

Posted on 2002-03-21
Medium Priority
Last Modified: 2010-04-02
Let's say that after this

char *sz = new char[4];
strcpy(sz, "AAA");

sz is 0x100. Will sz always point to the "AAA" and still be 0x100 during the life of my application? What I mean is that there will be lots of memory allocations/movements/etc as my application runs and I want to be certain that sz is always 0x100 and is always point to the right place. In other words, I want to reference "AAA" through any other pointer that I assign to 0x100:

//In 10 days after I started my application
char *sz1 = 0x100;
//sz1 still points to "AAA"

Is this true?

Question by:alex1234
  • 3
LVL 30

Accepted Solution

SteveGTR earned 100 total points
ID: 6885564

Author Comment

ID: 6885613
How about all these memory allocations/reallocations/movements/etc that occur in the application, don't they ever touch my 4 bytes island? What about memory optimizations that Windows does on its own behind my back? Is it because my application sees only a virtual memory the 'sz' is still at address 0x100?

Author Comment

ID: 6885974
"Is it because my application sees only a virtual memory the 'sz' is still at address 0x100?"

is supposed to be read as

"Is it because my application sees only a virtual memory the "AAA" is still at address 0x100?"


Expert Comment

ID: 6885986
From within the C++ application... there is little need to worry about "memory optimizations that Windows does on its own behind our back", as we never get the physical address.

Now about "all these memory allocations/ reallocations/ movements/ etc that occur in the application", as long you don't delete sz or a pointer == to sz, sz is still there pointing to the value.

The physical location may be changed, but that's transparent for the application.

Author Comment

ID: 6886007
Robalitoru, thanks. I knew all this stuff, but I just wanted to confirm with sombody else.

I guess that I'll have to give points to SteveGTR because he was first, despite he was a little short.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question