Pointer assignment.

How to I assign an address to a pointer.
I want to do something like this:
1
2
long int *p;
	p = 0x100100080;

0x100100080 - I know this address is in memory and has an int value(object)(on the heap, probably).
The only way to access it is through that address.

Can I use new(0x100100080). What will this actually do. Will it erase the data in the address.

1
2
int *p = new(0x100100088) int;
	p - 8;

This too doesn't work.

Is there any other way to access it.

EDIT:
Tried both these.
1
2
3
4
	std::stringstream s;
	s << "0x100100080";
	void* p;
	s >> std::hex >> p;


int *p = reinterpret_cast< int* > (0x100100080);

They compile, but do not give the desired result.
Last edited on
closed account (zwA4jE8b)
you cannot directly assign an address to a pointer.
Is there any other way to get the data in the address? Maybe reference, function....????

I only have the address and I know what is at that address (int, object...)
How do they do this in debuggers?
Last edited on
Placement new, your second example, is the way. It will not modify the contents of the memory since int's default constructor does nothing.

However, how do you know this address?
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.
Last edited on
Responding to the original post:

Could you clarify what you are trying to do?

If you want to access (and change) the variable, as suggested by your comment:

"I know this address is in memory and has an int value(object)(on the heap, probably).
The only way to access it is through that address."

Then you just have to dereference the pointer:

1
2
3
long int* p = 0x100100080;

*p = 123;


The "integer" at 0x100100080 will now contain 123

Of course, if it wasn't an integer something could go horribly wrong.

Also note that not all memory is readable. If you try and change memory in a const segment, you will cause a memory violation.



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 << (unsigned int)p << std::endl;
  p = 0; // NULL POINTER
  std::cout << (unsigned int)p << std::endl;
  p = (int*) 123456; // Set p to my choice of 123456
  std::cout << (unsigned int)p;

return 0;
}
Last edited on
long int* p = 0x100100080;
Compiler error: invalid conversion from 'long int' to 'long int*'
Does Not Work.

The compiler thinks 0x100100080 is a value(long int) and not an address.
Last edited on
It needs casting (see my code above).
Moschops wrote:
p = (int*) 0x100100080;


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.)

Thank you for all the Help.

Last edited on
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.
Last edited on
Hmmm...

Was the problem computer a 64-bit system?

The solution is a little unclear to me; what do you mean by "The addresses are on two different memory units (RAM's)."
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.
Last edited on
Topic archived. No new replies allowed.