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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

636 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