Pointer question

C

Chad Cartwright

I was coding a very basic C++ application, and all I wanted to do was
read a space of memory at 0x005CE000 over and over again. There is
another program running that is constantly writing to this address in
memory, and I wanted to make a basic loop funtion to read that address
in memory over and over until I closed the other program or my
program.

I don't know what I am doing wrong. Will C++ not let you do this?

int* var;
var = 0x005CE000

do{
cout << *var;
}while(1);

I can't remember if my syntax is correct (it's probably not)....all I
want to know is...is it possible for me to do something like this? I
just want to point a pointer to a specific location in memory. I am
not changing it...just reading it. I know there are access rights
issues if I wanted to change it, but I would assume that all memory
could just be read.

Thanks a lot.

~Chad
 
K

Karl Heinz Buchegger

Chad said:
I was coding a very basic C++ application, and all I wanted to do was
read a space of memory at 0x005CE000 over and over again. There is
another program running that is constantly writing to this address in
memory, and I wanted to make a basic loop funtion to read that address
in memory over and over until I closed the other program or my
program.

I don't know what I am doing wrong. Will C++ not let you do this?

The question is not: will C++ let you do this. With some casts you
press the compiler to do anything (even if the behaviour is undefined).

The question is: will your operating system let you do this.
int* var;
var = 0x005CE000

do{
cout << *var;
}while(1);

I can't remember if my syntax is correct (it's probably not)....all I
want to know is...is it possible for me to do something like this? I
just want to point a pointer to a specific location in memory. I am
not changing it...just reading it. I know there are access rights
issues if I wanted to change it, but I would assume that all memory
could just be read.

Ah so. You think it is ok for any program to just read the memory where
the operating system is storing passwords :)
 
J

JKop

Chad Cartwright posted:

int* var;
var = 0x005CE000

do{
cout << *var;
}while(1);


unsigned long cat = 72;

double dog = 54.6;

dog = cat;


The above will compile. Why? That's just how it is, that's
the rules as defined by the C++ Standard, an unsigned long
can be "implicitly casted" to a double.

As for:

unsigned long address = 0x005CE000;

int* p_var = address;

The C++ Standard dictates here that an unsigned long CANNOT
be "implicitly casted" to an int*. But... it does supply an
"explicit cast":


int* p_var = reinterpret_cast<int*>(0x005CE000);


-JKop
 
R

Rolf Magnus

Chad said:
I was coding a very basic C++ application, and all I wanted to do was
read a space of memory at 0x005CE000 over and over again. There is
another program running that is constantly writing to this address in
memory, and I wanted to make a basic loop funtion to read that address
in memory over and over until I closed the other program or my
program.

I don't know what I am doing wrong. Will C++ not let you do this?

int* var;
var = 0x005CE000

do{
cout << *var;
}while(1);

I can't remember if my syntax is correct (it's probably not)....all I
want to know is...is it possible for me to do something like this? I
just want to point a pointer to a specific location in memory. I am
not changing it...just reading it. I know there are access rights
issues if I wanted to change it, but I would assume that all memory
could just be read.

That heavily depends on the operating system you're using. Many (like
e.g. Linux, Windows, FreeBSD, MacOS, ...) don't even give your programs
real memory addresses. On such systems, your program is working on a
virtual address, and the address 0x005CE000 can point to completely
different physical memory locations in different processes, or they
might not even be valid at all. Further, processes cannot access the
memory of other processes on those systems. That memory is just not
part of their virtual address space.
If you need to transfer data between processes, you'd need to use some
form of interprocess communication, but this is again higly systems
specific (From the standard C++ point of view, there is only your
program, nothing else), so you should ask in a newsgroup about the OS
you're using.
 
R

Rolf Magnus

JKop said:
As for:

unsigned long address = 0x005CE000;

int* p_var = address;

The C++ Standard dictates here that an unsigned long CANNOT
be "implicitly casted" to an int*.

Actually, there is no such thing as an "implicit cast". A cast is what
you write down to explicitly request a conversion.
 
I

Ioannis Vranos

Chad said:
I was coding a very basic C++ application, and all I wanted to do was
read a space of memory at 0x005CE000 over and over again. There is
another program running that is constantly writing to this address in
memory, and I wanted to make a basic loop funtion to read that address
in memory over and over until I closed the other program or my
program.

I don't know what I am doing wrong. Will C++ not let you do this?

int* var;
var = 0x005CE000

do{
cout << *var;
}while(1);

I can't remember if my syntax is correct (it's probably not)....all I
want to know is...is it possible for me to do something like this? I
just want to point a pointer to a specific location in memory. I am
not changing it...just reading it. I know there are access rights
issues if I wanted to change it, but I would assume that all memory
could just be read.



Such thing is not guaranteed to work unless that specific address had a
specific meaning in a specific platform. In most platforms you can't
read memory in this way. Furthermore the above needs a reinterpret_cast.


What is guaranteed by the standard, is to read objects as sequences of
unsigned chars and chars for POD types and as unsigned char sequences
only, for non-POD types. And this for objects created inside the program
itself.






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
R

Richard Herring

Ioannis Vranos said:
Such thing is not guaranteed to work unless that specific address had a
specific meaning in a specific platform. In most platforms you can't
read memory in this way. Furthermore the above needs a reinterpret_cast.

And also (assuming you satisfy those requirements) probably a volatile
qualifier.

Oh, and a semicolon ;-)
 
O

Old Wolf

I was coding a very basic C++ application, and all I wanted to do was
read a space of memory at 0x005CE000 over and over again. There is
another program running that is constantly writing to this address in
memory, and I wanted to make a basic loop funtion to read that address
in memory over and over until I closed the other program or my
program.

I don't know what I am doing wrong. Will C++ not let you do this?

Not portably, of course. Most systems don't allow you to read
arbitrary memory addresses.
int* var;

int volatile *var;
var = 0x005CE000

do{
cout << *var;
}while(1);

the keyword 'volatile' signifies that the int may change as a result
of things not under your program's control (without it, the compiler
would probably optimise your code to:

int x = *(int *)0x5CE000;
do cout << x; while(1);

If that doesn't work then what you want isn't possible like this.
(There could be other system-specific means to read memory, eg.
using files under /proc, or OS API calls)
 
A

Anil Mamede

If you have the code of the other program you can use shared memory. But
that's off-topic, ask it on the OS newsgroup that you're using

Anil Mamede
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top