|
-
June 16th, 2015, 07:10 AM
#7
Re: Is memory allocated to a variable name or an address?
it isn't entirely clear what you are asking, your question is formulated in a weird manner, you may have to clarify what you mean.
'operator new' allocates a chunk of memory of the desired size (size is typically provided 'behind the scenes' by the compiler).
operator new returns a pointer, this pointer value is typically assigned to a (pointer)variable because you will need to delete the memory afterwards.
could we memory allocation to specific legal address (e.g: 0x7fff12345678) without any variable declaration?
do you mean... Can we allocate memory, and assign the pointer to the allocated memory to a specific address ?
then, yes:
Code:
*reinterpret_cast<int**>(0x7fff5678) = new int;
this will probably crash if you don't know what you're doing.
or do you mean, can you construct an object at a specific memory location (assumedly, previously allocated some way since otherwise the constructor will crash) ?
then, yes, this is called 'placement new'.
Code:
MyClass* p= new( reinterpret_cast<MyClass*>(0x7fff5678) ) MyClass(param1, param2, param3);
...
p->~MyClass(); // <- destroy previously placed object , there is no delete, since nothing got allocated.
If you mean something else, you'll need to be more clear.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|