i want to access to a specific memory unit

V

Vincent SHAO

Well, i have a question here. i want to write 50000 into a specific
memory unit (like # 4321). So i wrote these:

int number=5000;
memcpy((char *)50000,(char *)&number,sizeof(char));
char * p=(char *)50000;
cout<<"This is p"<<endl;
cout<<p<<endl;

But it doesn't work, how can i access to a specific memory unit.
Thank you.
 
I

Ian Collins

Vincent said:
Well, i have a question here. i want to write 50000 into a specific
memory unit (like # 4321). So i wrote these:

int number=5000;
memcpy((char *)50000,(char *)&number,sizeof(char));
char * p=(char *)50000;
cout<<"This is p"<<endl;
cout<<p<<endl;

But it doesn't work, how can i access to a specific memory unit.

The answer lies in the implementation specific realm, you had better ask
on a group dedicated to your tools and or platform.
 
J

Juha Nieminen

Vincent said:
Well, i have a question here. i want to write 50000 into a specific
memory unit (like # 4321). So i wrote these:

int number=5000;
memcpy((char *)50000,(char *)&number,sizeof(char));
char * p=(char *)50000;
cout<<"This is p"<<endl;
cout<<p<<endl;

But it doesn't work, how can i access to a specific memory unit.
Thank you.

"It doesn't work" tells nothing to us. What are the *symptoms* you are
seeing? Is it crashing? Is it returning the wrong value? What?

Besides, the above code is horribly non-portable, and thus not really
on-topic in this group.
 
K

keith

Well, i have a question here. i want to write 50000 into a specific
memory unit (like # 4321). So i wrote these:

memcpy((char *)50000,(char *)&number,sizeof(char));

First, you should ask someone about the difference between 'source'
and 'destination'.

Then, ask them to explain why it is unlikely that the value 50000
would fit into something that was 'sizeof(char)' bytes long.

Then after all that, perhaps you yourself could explain why you think
that '(char*)&number' might have anything to do with 'a specific
memory unit (like # 4321)'.

HTH.
 
J

James Kanze

The answer lies in the implementation specific realm, you had
better ask on a group dedicated to your tools and or platform.

Mostly. The C++ part of the answer is reinterpret_cast.

As it is, his comments don't seem to correspond to his code, and
his code seems to be trying to write 5000 into a char, which
isn't going to work on a lot of systems, for obvious reasons.
If I understand what he's trying to do, however, something like:
*reinterpret_cast< int* >( 50000 ) = 5000 ;
should do the trick, although what that will actually do is, of
course, very, very implementation dependent. (It will generate
a core dump on the implementations I use, unless the executable
is very, very small.)
 
V

Vincent SHAO

"It doesn't work" tells nothing to us. What are the *symptoms* you are
seeing? Is it crashing? Is it returning the wrong value? What?

Besides, the above code is horribly non-portable, and thus not really
on-topic in this group.

Thanks for comments. The original problem is "put a byte(like 34) into
a specific memory unit (like #4321)". Here is my code
//define a byte
char number=34;
//copy the number to the memory unit #4321
memcpy((char *)4321,(char *)&number,sizeof(char));
char * p=(char *)4321;
cout<<(int)*p<<endl;

i run the program and there is nothing out put.

And why the former code is non-portable, how judge?
Thank you
 
J

Jeff Schwab

Vincent said:
Well, i have a question here. i want to write 50000 into a specific
memory unit (like # 4321). So i wrote these:

int number=5000;
memcpy((char *)50000,(char *)&number,sizeof(char));
char * p=(char *)50000;
cout<<"This is p"<<endl;
cout<<p<<endl;

You're outputting the pointer value, not the memory content.
Dereference the pointer (cout << *p).
 
V

Victor Bazarov

Jeff said:
You're outputting the pointer value, not the memory content.
Dereference the pointer (cout << *p).

I also failed to see '4321' in the code in any way. The usual
(implementation-defined) method is to use the integral value and
reinterpret_cast it to the pointer. Perhaps this is what the
intention of declaring 'p' was...

char *p = reinterpret_cast<char*>(4321);

V
 
J

Jeff Schwab

Victor said:
I also failed to see '4321' in the code in any way. The usual
(implementation-defined) method is to use the integral value and
reinterpret_cast it to the pointer. Perhaps this is what the
intention of declaring 'p' was...

char *p = reinterpret_cast<char*>(4321);

Right you are!

I don't really understand what the OP is doing with the value 5000. It
looks like one byte of the value is being written to the memory at
address 50000, but whether the high-order or low-order byte, I do not
know. I supposed either *p == 136 or *p == 0.
 
I

Ian Collins

Vincent said:
Thanks for comments. The original problem is "put a byte(like 34) into
a specific memory unit (like #4321)". Here is my code
//define a byte
char number=34;
//copy the number to the memory unit #4321
memcpy((char *)4321,(char *)&number,sizeof(char));
char * p=(char *)4321;
cout<<(int)*p<<endl;

Why make things so complex? sizeof(char) is 1 by definition, so you
could simply write

i run the program and there is nothing out put.

And why the former code is non-portable, how judge?

What are you running on and how do you know 4321 is a valid, writable
address?
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top