Create New C++ Object at Specific Memory Address

Create new C++ object at specific memory address?

You want placement new(). It basically calls the constructor using a block of existing memory instead of allocating new memory from the heap.

Edit: make sure that you understand the note about being responsible for calling the destructor explicitly for objects created using placement new() before you use it!

c# creating object in specific memory address

Workaround:

p/invoke the method: memcpy from msvrct.dll:

[DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl,
SetLastError = false)]
public static extern IntPtr memcpy(IntPtr dest, IntPtr src, UIntPtr count);

you need the size of your object you want to copy:

var size = (uint) Marshal.SizeOf(obj);

you need to pin it:

GCHandle handle = GCHandle.Alloc(obj, GCHandleType.Pinned);

finally call the method memcpy:

 var _adress = NativeMethods.memcpy(new IntPtr(1115911111), handle.AddrOfPinnedObject(), new UIntPtr(size));

How to store a variable or object on desired memory location?

You wrote

 class newarray 
{
int arr[]; //size not declared

This is not allowed. Unfortunately, your compiler did not warn you when compilinhg. You only discovered that this was a problem when your code crashed.

You then wonder about "some allocated memory location" and picking one manually. That's not the problem. int arr[] without explicitly specifying bounds is allowed only in a few contexts, such as a function declaration. The function declaration doesn't need a size because the size in that case is determined by the caller.

Since this is C++, just use std::vector<int> for your dynamic arrays.

how to change value in a specific memory address?

Given a valid pointer to an object in a memory address, indirect through the pointer and assign a value:

int health_points = 1337;  // an object in some memory address
int* ptr = &a; // pointer to the value in that memory address
*ptr = 0; // the value is modified

Is there a way that I can forcedly compile and run this code?

Your program is ill-formed. There may be a way to let a compiler successfully compile it if the compiler provides a relevant language extension, but it is typically a bad idea to rely on such extensions because it would trap you into using that one compiler.

Instead of trying to force a compiler accept a broken program, I recommend fixing the program.

how to change value in a specific memory address?

Firstly, the memory in that address must be allocated. The language implementation takes care of allocating all memory and there is no standard way to specify what address is allocated.

However, some language implementations may specify certain addresses as accessible. This is typical on embedded systems. If this is the case, then you can convert an integer value to a pointer using a cast:

std::uintptr_t address = 0x111; // see documentation whether this is a valid address
int* ptr = reinterpret_cast<int*>(address);

If your language implementation doesn't specify that the address is accessible, then there are no guarantees about accessing it. On modern operating systems which manage virtual memory, the best case scenario is a segfault.

How to assign a specific memory address to a pointer?

Assuming this is a valid writable on your process memory address and by "an integer" you meant an int:

*reinterpret_cast<int*>(0x8f820dae) = 2;

Note that this will write the value 2 (0x00000002) to the address 0x8f820dae (considering x86). Change the <int> type-parameter if you want to write a different numbers of bytes (i.e. sizeof(int) bytes will be written at the memory address).

What is the correct way to create a struct at a specific address in C?

This is valid because no memory usage constraints are enforced in C.

void *ptr = mmap(0, sizeof(A), prot, flags, fd, offset);

A *ptrA = (A*) ptr;

Change the memory address of an object in C#

Only to mark this question as answered to be useful, I pasted my comment here again.

It won't be easy to do so.
Because in .Net, GC must track the address of all of the variables to de-allocate the pointer, after releasing the last anchor of the variable.
Also, it's illegal because each application has it's own Application Domain and by default, no application is allowed to access to out of its boundary.

Please consider marking the question as answered.
Thanks a lot.

Unwanted creation of object at same memory address

while (...)
{
Pair pair(...); // calling constructor, creating object type of Pair.
pairs[i] = &pair;
i++;
}

pair is allocated with automatic storage duration and goes out of scope upon each iteration of the loop. You are saving a pointer to an invalid object (a bunch of them).

You should use a collection of smart pointers if you really need pointers (this is true when objects cannot be copied or copies are expensive), which may not be necessary at all. Try this instead:

vector<Pair> pairs;
// and if you need pointers...
vector<unique_ptr<Pair>> pairs;

while(whatever) {
pairs.push_back(Pair(...));
// or...
pairs.push_back(unique_ptr<Pair>(new Pair(...)));
}


Related Topics



Leave a reply



Submit