Solved

A memory allocation question

Posted on 2002-03-21
5
185 Views
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?



0
Comment
Question by:alex1234
  • 3
5 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 50 total points
ID: 6885564
Yes
0
 
LVL 1

Author Comment

by:alex1234
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?
 
0
 
LVL 1

Author Comment

by:alex1234
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?"


0
 
LVL 1

Expert Comment

by:Robalitoru
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.
0
 
LVL 1

Author Comment

by:alex1234
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.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

810 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