int *p = new(0x100100080) int;
gives me a compiler error: Cannot cast *void to int. void* new (1st arg, *void).
Any of the above code that compiles does not work right.
However, how do you know this address?
I did a "new int" and assigned it to a pointer. Dereferenced the pointer and gave it a value. Took the address to where the pointer was pointing.
Now, assume, for some reason, my pointer does not exist, BUT the 'new' object created is there at the same address(I know it is there due to something else, but I cannot use that "something else", to get it back).
How do I get another pointer to it.
The object is like a memory leak right now, but I need it.
you cannot directly assign an address to a pointer.
I disagree.
This code creates a pointer named p and sets its value (i.e. the memory address it points to) to some value occupied by the object x, and then to zero, and then to some number I typed in.
This is very common in embedded coding where physical components are hard-wired to specifically identified memory addresses.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
int main()
{
int x = 7;
int* p = &x; // Set p to address of x
std::cout << (unsignedint)p << std::endl;
p = 0; // NULL POINTER
std::cout << (unsignedint)p << std::endl;
p = (int*) 123456; // Set p to my choice of 123456
std::cout << (unsignedint)p;
return 0;
}
This compiles, but the output is not the value of the int. The Output is '0'.
Yes, it is an embedded system.
EDIT:
Just figured it out. The addresses are on two different memory units (RAM's).
That is the reason why Moschops code was not changing my value in the address (it was changing something somewhere else with the same address, 0x100100080.)
This compiles, but the output is not the value of the int. The Output is '0'.
If the output is zero, and you're reading it as an int from the correct memory location, and displaying it as an int, then the value of the int is zero.
If you think it isn't zero, you have either made a mistake in your code, or you are denying the clear evidence in front of you.
I note that the hex value 0x100100080 is the binary value
1000 0000 0000 1000 0000 0000 0100 0000 0
which takes 33 bits to represent. Can your system handle a pointer size of 33 bits? A 32 bit system, as a general rule of thumb, can't.
Maybe the 2 memory chips he's got use 32-bit addresses so the most-significant bit tells which of the 2 chips to read/write to. And if he's storing it on the first one but is reading it from the second one (because of int overflow) he could be getting different results. Imagine you write to address of 33-bit-long "long int" but read from an address of 32-bit-long "int" so the 33-rd bit is 0 by default. For me that's the most logical case according to what dams said so far.