Solved

A memory allocation question

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 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 learn how to clear a vector as well as how to detect empty vectors in C++.

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now