A memory allocation question

Posted on 2002-03-21
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 50 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

790 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