473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Convert Int Value To Pointer

I've got a strange problem here. I need to take a value that I have
written down on a sticky-note here, and find what's at that address in
current memory. I've tried the following:

void *value = 0x00323c68;

but I get a typecasting error. If I do the following:

void *value = static_cast<void *>(0x00323c68);

will that get me what I desire?

--Guillaume C.L.--

Jul 14 '06 #1
18 20539

ReaperUnreal wrote:
I've got a strange problem here. I need to take a value that I have
written down on a sticky-note here, and find what's at that address in
current memory. I've tried the following:

void *value = 0x00323c68;

but I get a typecasting error. If I do the following:

void *value = static_cast<void *>(0x00323c68);

will that get me what I desire?

--Guillaume C.L.--
Come to think of it, I'd need a reinterpret_cast not a static_cast
wouldn't I?

--Guillaume C.L.--

Jul 14 '06 #2
ReaperUnreal said:
I've got a strange problem here. I need to take a value that I have
written down on a sticky-note here, and find what's at that address in
current memory. I've tried the following:

void *value = 0x00323c68;

but I get a typecasting error.
No, you don't, since there is no cast.
If I do the following:

void *value = static_cast<void *>(0x00323c68);

will that get me what I desire?
No, because that's a syntax error.

If you do this:

void *value = (void *)0x00323c68;

it'll compile all right, but it won't actually mean anything very much on
most systems. On a protected mode OS, if you try to read through (or write
through!) that pointer, the best you're likely to get is a segfault.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 14 '06 #3
"ReaperUnreal" <re**********@gmail.comwrites:
ReaperUnreal wrote:
I've got a strange problem here. I need to take a value that I have
written down on a sticky-note here, and find what's at that address in
current memory. I've tried the following:

void *value = 0x00323c68;

but I get a typecasting error. If I do the following:

void *value = static_cast<void *>(0x00323c68);

will that get me what I desire?

--Guillaume C.L.--

Come to think of it, I'd need a reinterpret_cast not a static_cast
wouldn't I?
The casting you're using is specific to C++.
Depending on the OS the value of that number can or cannot have
meaning.
The standard (C99) also says that an integer may be converted to any
pointer type but the result is implementation-defined.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jul 14 '06 #4


ReaperUnreal wrote On 07/14/06 12:09,:
I've got a strange problem here. I need to take a value that I have
written down on a sticky-note here, and find what's at that address in
current memory. I've tried the following:

void *value = 0x00323c68;

but I get a typecasting error. If I do the following:

void *value = static_cast<void *>(0x00323c68);

will that get me what I desire?
Only you know your desires. However, you're in the
wrong newsgroup. In comp.lang.c we discuss Australia;
you should instead go to comp.lang.c++ if you want to
learn about Austria. Despite the superficial similarity
of names, the two have very little to do with each other.

--
Er*********@sun.com

Jul 14 '06 #5
ReaperUnreal posted:
I've got a strange problem here. I need to take a value that I have
written down on a sticky-note here, and find what's at that address in
current memory. I've tried the following:

void *value = 0x00323c68;

but I get a typecasting error. If I do the following:

void *value = static_cast<void *>(0x00323c68);

will that get me what I desire?

How about something like the following?

#include <stddef.h>
#include <limits.h>
#include <stdio.h>

void PrintBits(void const * const mem, size_t amount_bytes)
{
char static str[CHAR_BIT + 1]; /* Auto null teminator */

unsigned char const *p = (unsigned char const *)mem;

do
{
unsigned char const byte_val = *p++;

char *pos = str;

unsigned char i = 1U << CHAR_BIT - 1;

do *pos++ = byte_val & i ? '1' : '0';
while(i >>= 1U);

printf("%s",str);

} while (--amount_bytes);
}

int main(void)
{
void * const p = (void*)0x00323c68;

PrintBits( p, 1 );
}

--

Frederick Gotham
Jul 14 '06 #6
On Fri, 14 Jul 2006 19:37:20 GMT, Frederick Gotham
<fg*******@SPAM.comwrote in comp.lang.c:
ReaperUnreal posted:
I've got a strange problem here. I need to take a value that I have
written down on a sticky-note here, and find what's at that address in
current memory. I've tried the following:

void *value = 0x00323c68;

but I get a typecasting error. If I do the following:

void *value = static_cast<void *>(0x00323c68);

will that get me what I desire?


How about something like the following?

#include <stddef.h>
#include <limits.h>
#include <stdio.h>

void PrintBits(void const * const mem, size_t amount_bytes)
{
char static str[CHAR_BIT + 1]; /* Auto null teminator */

unsigned char const *p = (unsigned char const *)mem;

do
{
unsigned char const byte_val = *p++;

char *pos = str;

unsigned char i = 1U << CHAR_BIT - 1;

do *pos++ = byte_val & i ? '1' : '0';
while(i >>= 1U);
Do you realize that there is absolutely no gain to adding the 'U'
suffix here?
>
printf("%s",str);

} while (--amount_bytes);
}

int main(void)
{
void * const p = (void*)0x00323c68;

PrintBits( p, 1 );
}
Do you actually this makes the behavior any less undefined? Do you do
realize that without a terminating '\n', there is no guarantee that
any output will appear?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 15 '06 #7

"ReaperUnreal" <re**********@gmail.comwrote in message
news:11**********************@35g2000cwc.googlegro ups.com...
I've got a strange problem here. I need to take a value that I have
written down on a sticky-note here, and find what's at that address in
current memory. I've tried the following:

void *value = 0x00323c68;

but I get a typecasting error. If I do the following:

void *value = static_cast<void *>(0x00323c68);

will that get me what I desire?
I don't mean to sound difficult, but maybe, or maybe not. Your ability to
access memory outside of your application space is environment _and_
compiler specific. I get to this in a bit.

First, let's learn how to cast in C.
void *value = 0x00323c68;
First problem: there is no cast there. If there was a cast, it'd look like
this:

void *value = (void *)0x00323c68;

Second problem: you can't do pointer arithmetic on void's and you can't
obtain a value using a void pointer. So, the cast must be of some other
type (char, long, int,...):

unsigned char *value = (unsigned char *)0x00323c68;

or:

unsigned long *value = (unsigned long *)0x00323c68;

As a practical matter, the first will return a 8-bit "byte" on most modern
systems (C defines a byte as the smallest addressable grouping of bits, not
as 8-bits...). The second will return a 32-bit value on most modern
systems, but may return other sizes (64-bits) depending on the compiler
and/or environment.

Finally, if you are attempting to read memory outside your application
space. You may have to jump through a number of other hurdles:
1) mapping the memory through the MMU or paging mechanism
2) gaining CPU privilege to access the memory
3) adjusting the pointer by an offset (linear vs. physical addressing)
4) etc...
Rod Pemberton


Jul 15 '06 #8
Jack Klein posted:

> while(i >>= 1U);

Do you realize that there is absolutely no gain to adding the 'U'
suffix here?

Option (1)

i >>= 1

The thing on the left is very likely to be promoted to a "signed int". It's
shifted once to the right, then the result is converted to an "unsigned
char".

Option (2)

i >>= 1U

The thing on the left is promoted to "unsigned int". It's shifted once to
the right, then the result is converted to an "unsigned char".
Option (2) sounds more natural to me.

Do you actually this makes the behavior any less undefined?

Obviously, this person knows what's at that memory address.

Do you do realize that without a terminating '\n', there is no guarantee
that
any output will appear?

I recall hearing something along those lines before. Would I have to flush?
--

Frederick Gotham
Jul 15 '06 #9

Frederick Gotham wrote:
Jack Klein posted:

while(i >>= 1U);
Do you realize that there is absolutely no gain to adding the 'U'
suffix here?


Option (1)

i >>= 1

The thing on the left is very likely to be promoted to a "signed int". It's
shifted once to the right, then the result is converted to an "unsigned
char".

Option (2)

i >>= 1U

The thing on the left is promoted to "unsigned int". It's shifted once to
the right, then the result is converted to an "unsigned char".
Option (2) sounds more natural to me.

Do you actually this makes the behavior any less undefined?


Obviously, this person knows what's at that memory address.

Do you do realize that without a terminating '\n', there is no guarantee
that
any output will appear?


I recall hearing something along those lines before. Would I have to flush?
--

Frederick Gotham
Ok, so first off, I got it to work, it was indeed that I either had to
use reinterpret_cast or a C-style cast. I needed to cast it as a void
pointer because that's what the function takes. This is perfectly
defined for the function ReadProcessMemory which I'm using to write a
trainer of sorts. Please stop telling me that it won't work, when I've
tested it, and it does. I do in fact know exactly what's at that memory
address, a double containing my randomly generated number, a proof of
concept basically.

--Guillaume C.L.--

Jul 15 '06 #10
On 15 Jul 2006 08:04:36 -0700, "ReaperUnreal" <re**********@gmail.com>
wrote:

snip
>Ok, so first off, I got it to work, it was indeed that I either had to
use reinterpret_cast or a C-style cast. I needed to cast it as a void
Are you working in C or C++? If the latter, this is the wrong group.
If the former, there is no reinterpret_cast.
>pointer because that's what the function takes. This is perfectly
What language are you in? In C, you can pass any pointer to a
function that expects a void pointer as long as a prototype for that
function is in scope. C permits implicit conversion to and from void
pointers.
Remove del for email
Jul 15 '06 #11
Frederick Gotham <fg*******@SPAM.comwrites:
Jack Klein posted:
[...]n
>Do you do realize that without a terminating '\n', there is no
guarantee that any output will appear?


I recall hearing something along those lines before. Would I have to
flush?
It depends on the circumstances. For something like:

printf("Prompt: ");
fflush(stdout);
fgets(buf, sizeof buf, stdin);

the fflush() call ensures that the prompt will appear. (This assumes
that the system supports this kind of interactive i/o in the first
place; not all systems do.) On the other hand, for the last output a
program produces before terminating, a missing newline means the
output might not appear at all, with or without a call to fflush().
C99 7.19.2p2:

A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.

(stdout is, of course, associated with a text stream.) Note that the
standard doesn't say what happens if a new-line character is required
and the program doesn't provide one; this implies that it's undefined
behavior.

More commonly, even if a final line without a trailing new-line is
written properly to stdout, it may not appear properly. For example,
a shell prompt written after the program terminates may be confusingly
merged with the program's output, or may even overwrite it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 15 '06 #12
Frederick Gotham wrote:
>
Jack Klein posted:
while(i >>= 1U);
Do you realize that there is absolutely no gain to adding the 'U'
suffix here?

Option (1)

i >>= 1

The thing on the left is very likely
to be promoted to a "signed int".
Option (2)

i >>= 1U

The thing on the left is promoted to "unsigned int".
Wrong.
The types of the right and left operands of a shift operator,
have nothing to do with each other.

(i >>= 1) and (i >>= 1U),
are both expressions of whatever type i is promtoted to,
according to the rules for "integer promotions".

--
pete
Jul 15 '06 #13
pete wrote:
>
Frederick Gotham wrote:

Jack Klein posted:
> while(i >>= 1U);
>
Do you realize that there is absolutely no gain to adding the 'U'
suffix here?
Option (1)

i >>= 1

The thing on the left is very likely
to be promoted to a "signed int".
Option (2)

i >>= 1U

The thing on the left is promoted to "unsigned int".

Wrong.
The types of the right and left operands of a shift operator,
have nothing to do with each other.

(i >>= 1) and (i >>= 1U),
are both expressions of whatever type i is promtoted to,
according to the rules for "integer promotions".
I meant to say that (i >1) and (i >1U),
are both expressions of whatever type i is promoted to,
according to the rules for "integer promotions".

(i >>= 1) and (i >>= 1U) are both expressions of
whatever type i is of.

--
pete
Jul 15 '06 #14
Keith Thompson posted:

printf("Prompt: ");
fflush(stdout);
fgets(buf, sizeof buf, stdin);

Is it a requirement of C that every time you print something, there must be a
'\n' at the end?
--

Frederick Gotham
Jul 16 '06 #15

Frederick Gotham wrote:
Keith Thompson posted:

printf("Prompt: ");
fflush(stdout);
fgets(buf, sizeof buf, stdin);


Is it a requirement of C that every time you print something, there must be a
'\n' at the end?
No. Why do you thing that?

Jul 16 '06 #16
Ed Prochak posted:
>
Frederick Gotham wrote:
>Keith Thompson posted:

printf("Prompt: ");
fflush(stdout);
fgets(buf, sizeof buf, stdin);


Is it a requirement of C that every time you print something, there
must be a '\n' at the end?

No. Why do you thing that?

I think I understand...

The following program might not print anything at all:

#include <stdio.h>

int main(void)
{
printf("Hello");

fflush(stdout);
}

While the following *must* print something:
#include <stdio.h>

int main(void)
{
printf("Hello\n");
}

Correct... ?

What are all the ways of "flushing"? I know of:

(1) Print a '\n'.
(2) fflush()
--

Frederick Gotham
Jul 16 '06 #17
Frederick Gotham <fg*******@SPAM.comwrites:
Keith Thompson posted:
> printf("Prompt: ");
fflush(stdout);
fgets(buf, sizeof buf, stdin);

Is it a requirement of C that every time you print something, there
must be a '\n' at the end?
No, of course not. (It's odd that you ask that after quoting an
example where there *isn't* a '\n' at the end of the output.)

For a full explanation, see the remainder of my previous followup,
which you snipped.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 16 '06 #18
Frederick Gotham <fg*******@SPAM.comwrites:
Ed Prochak posted:
>>
Frederick Gotham wrote:
>>Keith Thompson posted:
printf("Prompt: ");
fflush(stdout);
fgets(buf, sizeof buf, stdin);
Is it a requirement of C that every time you print something, there
must be a '\n' at the end?

No. Why do you thing that?


I think I understand...

The following program might not print anything at all:

#include <stdio.h>

int main(void)
{
printf("Hello");

fflush(stdout);
}

While the following *must* print something:
#include <stdio.h>

int main(void)
{
printf("Hello\n");
}

Correct... ?
Yes (though an output error is always a possibility, the first program
will *probably* print "Hello" without a newline, or possibly with one,
on most implementations, and you should really have a "return 0;"
unless you're assuming a C99 implementation).
What are all the ways of "flushing"? I know of:

(1) Print a '\n'.
(2) fflush()
Printing a '\n' doesn't *necessary* flush the stream; it depends on
how the stream is buffered.

C99 7.19.3p3:

When a stream is _unbuffered_, characters are intended to appear
from the source or at the destination as soon as
possible. Otherwise characters may be accumulated and transmitted
to or from the host environment as a block. When a stream is
_fully buffered_, characters are intended to be transmitted to or
from the host environment as a block when a buffer is filled. When
a stream is _line buffered_, characters are intended to be
transmitted to or from the host environment as a block when a
new-line character is encountered. Furthermore, characters are
intended to be transmitted as a block to the host environment when
a buffer is filled, when input is requested on an unbuffered
stream, or when input is requested on a line buffered stream that
requires the transmission of characters from the host
environment. Support for these characteristics is
implementation-defined, and may be affected via the setbuf and
setvbuf functions.

C99 7.19.3p7:

At program startup, three text streams are predefined and need not
be opened explicitly -- _standard input_ (for reading conventional
input), _standard output_ (for writing conventional output), and
_standard error_ (for writing diagnostic output). As initially
opened, the standard error stream is not fully buffered; the
standard input and standard output streams are fully buffered if
and only if the stream can be determined not to refer to an
interactive device.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 16 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sorisio, Chris | last post by:
Ladies and gentlemen, I've imported some data from a MySQL database into a Python dictionary. I'm attempting to tidy up the date fields, but I'm receiving a 'mx.DateTime.Error: cannot convert...
41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
1
by: irvin.hwang | last post by:
Hi, I'm having a problem passing a pointer of a struct. Here is the code that's giving me a trouble. 1. shape *s; 2. s = load_shape(); 3. shape2grammar(s); test.cpp:53: error: no...
10
by: dorkrawk | last post by:
I am having an issue with some VC++ I am writing. I have a struct and I'm trying to call a function from it from a function in another object. here is the struct.... struct JNI_Interface {...
3
by: mishink7 | last post by:
i am getting this error error C2662: 'std::vector<_Ty>::push_back' : cannot convert 'this' pointer from 'const std::vector<_Ty>' to 'std::vector<_Ty> &' with and ...
6
by: Kate77 | last post by:
Hi, Im trying to build simple encryption, I use char d="a" int intASC = System.Convert.ToInt32(d); to convert it, and play with it, now I want to change it (d) back for the original ascii...
1
by: simply123 | last post by:
I doing doing C btw... i have been trying to convert array elements into their respective addresses but Im faced with many problems. eg. int x (array with 7 elements) im trying to set the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.