In article <2ecf9822-1a6c-4a86-bd8d-c56ea4946194@k3 7g2000hsf.googl egroups.com>,
Jrdman <ahmed.bou23@gm ail.comwrote:
>is it possible to use a pointer to see the data stored in a given
>address in the memory ?
Yes, you can convert any pointer to a character pointer type and
examine the bytes it points to. The consequences of following an
invalid pointer, or looking beyond the end of the object it points to,
are undefined.
E.g.
int a = 42;
unsigned char *p = (unsigned char *)&a;
int i;
On Jul 3, 9:50 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
In article <2ecf9822-1a6c-4a86-bd8d-c56ea4946...@k3 7g2000hsf.googl egroups..com>,
>
Jrdman <ahmed.bo...@gm ail.comwrote:
is it possible to use a pointer to see the data stored in a given
address in the memory ?
>
Yes, you can convert any pointer to a character pointer type and
examine the bytes it points to. The consequences of following an
invalid pointer, or looking beyond the end of the object it points to,
are undefined.
>
E.g.
>
int a = 42;
unsigned char *p = (unsigned char *)&a;
int i;
>
for(i=0; i<sizeof(a); i++)
printf("%02x ", p[i]);
printf("\n");
>
-- Richard
--
Please remember to mention me / in tapes you leave behind.
On Jul 3, 9:50 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
In article <2ecf9822-1a6c-4a86-bd8d-c56ea4946...@k3 7g2000hsf.googl egroups..com>,
>
Jrdman <ahmed.bo...@gm ail.comwrote:
is it possible to use a pointer to see the data stored in a given
address in the memory ?
>
Yes, you can convert any pointer to a character pointer type and
examine the bytes it points to. The consequences of following an
invalid pointer, or looking beyond the end of the object it points to,
are undefined.
>
E.g.
>
int a = 42;
unsigned char *p = (unsigned char *)&a;
int i;
>
for(i=0; i<sizeof(a); i++)
printf("%02x ", p[i]);
printf("\n");
>
-- Richard
--
Please remember to mention me / in tapes you leave behind.
but what if we repalce &a with an arbitrary address(number) cuz i wanna
access a specific place?
In article <a7e128ad-72f4-464c-ba95-dc2f2a5c5464@k1 3g2000hse.googl egroups.com>,
Jrdman <ahmed.bou23@gm ail.comwrote:
>but what if we repalce &a with an arbitrary address(number) cuz i wanna
>access a specific place?
Please use proper English.
If the number corresponds to some accessible memory, then it will
work, but you need to be familiar with the details of your operating
system. In a general purpose operating system with virtual memory,
there's not likely to be anything much useful at fixed addresses.
-- Richard
--
Please remember to mention me / in tapes you leave behind.
In article <a7e128ad-72f4-464c-ba95-dc2f2a5c5464@k1 3g2000hse.googl egroups.com>,
Jrdman <ahmed.bou23@gm ail.comwrote:
>
>but what if we repalce &a with an arbitrary address(number) cuz i wanna
>access a specific place?
>
Please use proper English.
>
If the number corresponds to some accessible memory, then it will
work, but you need to be familiar with the details of your operating
system. In a general purpose operating system with virtual memory,
there's not likely to be anything much useful at fixed addresses.
>is it possible to use a pointer to see the data stored in a given
>address in the memory ?
>>
>Yes, you can convert any pointer to a character pointer type and
>examine the bytes it points to. The consequences of following an
>invalid pointer, or looking beyond the end of the object it points to,
>are undefined.
>>
>E.g.
>>
> int a = 42;
> unsigned char *p = (unsigned char *)&a;
> int i;
>>
> for(i=0; i<sizeof(a); i++)
> printf("%02x ", p[i]);
> printf("\n");
>
but what if we repalce &a with an arbitrary address(number) cuz i wanna
access a specific place?
Addresses are not numbers. They may be represented as numbers on some
systems, probably most of them, but as far as the C language is
concerned they're two very different things.
It's possible to convert an integer value to a pointer type, and vice
versa. The results of any such conversion are implementation-defined,
and any attempt to use an address (equivalently a pointer value)
obtained by conversion from some arbitrary integer value is likely to
be dangerous. (Conversion of a constant 0 is a special case, yielding
a null pointer.)
Here's a program that illustrates how you might do this:
=============== =============== ==========
#include <stdio.h>
int main(void)
{
unsigned long num = 0xdeadbeef;
unsigned char data;
printf("The byte at 0x%lx is: ", num);
/*
* Flush stdout to make sure the above line appears
* even if the program crashes.
*/
fflush(stdout);
data = *(unsigned char*)num;
printf("0x%x\n" , (unsigned int)data);
And here's the output I got when I ran it:
=============== =============== ==========
The byte at 0xdeadbeef is: Segmentation fault (core dumped)
=============== =============== ==========
This is close to the best result I could have gotten; the system was
kind enough to tell me I had done something stupid, rather than just
giving me meaningless data.
Now I'll ask you the question I probably should have asked in
the first place.
*Why* do you want to do this? What problem are you trying to solve?
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
On Jul 3, 10:37 pm, Keith Thompson <ks...@mib.orgw rote:
Jrdman <ahmed.bo...@gm ail.comwrites:
On Jul 3, 9:50 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
In article
<2ecf9822-1a6c-4a86-bd8d-c56ea4946...@k3 7g2000hsf.googl egroups.com>,
>
Jrdman <ahmed.bo...@gm ail.comwrote:
is it possible to use a pointer to see the data stored in a given
address in the memory ?
>
Yes, you can convert any pointer to a character pointer type and
examine the bytes it points to. The consequences of following an
invalid pointer, or looking beyond the end of the object it points to,
are undefined.
>
E.g.
>
int a = 42;
unsigned char *p = (unsigned char *)&a;
int i;
but what if we repalce &a with an arbitrary address(number) cuz i wanna
access a specific place?
>
Addresses are not numbers. They may be represented as numbers on some
systems, probably most of them, but as far as the C language is
concerned they're two very different things.
>
It's possible to convert an integer value to a pointer type, and vice
versa. The results of any such conversion are implementation-defined,
and any attempt to use an address (equivalently a pointer value)
obtained by conversion from some arbitrary integer value is likely to
be dangerous. (Conversion of a constant 0 is a special case, yielding
a null pointer.)
>
Here's a program that illustrates how you might do this:
=============== =============== ==========
#include <stdio.h>
>
int main(void)
{
unsigned long num = 0xdeadbeef;
unsigned char data;
>
printf("The byte at 0x%lx is: ", num);
/*
* Flush stdout to make sure the above line appears
* even if the program crashes.
*/
fflush(stdout);
>
data = *(unsigned char*)num;
printf("0x%x\n" , (unsigned int)data);
>
return 0;}
>
=============== =============== ==========
>
And here's the output I got when I ran it:
=============== =============== ==========
The byte at 0xdeadbeef is: Segmentation fault (core dumped)
=============== =============== ==========
>
This is close to the best result I could have gotten; the system was
kind enough to tell me I had done something stupid, rather than just
giving me meaningless data.
>
Now I'll ask you the question I probably should have asked in
the first place.
>
*Why* do you want to do this? What problem are you trying to solve?
>
--
Keith Thompson (The_Other_Keit h) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"- Hide quoted text-
>
- Show quoted text -
i wanna access the memory address at 0x00(first segment) and then
printf a range of memory from the first segment to a specific address
something is wrong with this idea ?if this ,is possible please help
me?
In article <8bba9c05-7c35-4f4f-a135-1add08363c2e@8g 2000hse.googleg roups.com>,
Jrdman <ahmed.bou23@gm ail.comwrote:
>i wanna access the memory address at 0x00(first segment) and then
>printf a range of memory from the first segment to a specific address
>something is wrong with this idea ?if this ,is possible please help
>me?
What you are asking to do is system-specific.
Most of the systems I use don't have segments at all; the other systems
I use that do have segments pretty much ignore the existance of segments
and use linear virtual address ranges in practice. So what you
are asking for is impossible on many systems and irrelevant on a bunch
more.
As you are wanting to do something that isn't possible at all on
many systems that run C, if you are able to implement it at all
on the systems of interest to you, you have to use non-portable methods
to accomplish it.
On C systems that use a linear virtual address space, you can
often (but it isn't promised to work) look at offsets in your virtual
address space by converting the offset from an integral type to a pointer.
C doesn't define what this means: C says that the implementation
can assign any meaning it wants to the conversion shown in the first line.
Actually attempting to access the memory you've constructed a pointer
to might do nearly anything, including possibly hang your system. (And
I don't mean that lightly: just *reading* from memory addresses can
trigger nasty hardware behaviour.)
Because C doesn't define the conversion, the memory address actually
examined might have no obvious relationship at all to the constant
0x000003fc -- you may have to dig deep into your system documentation
to find out which address constant you need to use to access particular
memory locations.
Even on systems that have segments at all, there is no common
idiomatic way of storing particular values in segment base registers.
You might have to drop in some inline assembler, if your C happens
to have an inline assembler extension (inline assembler is not
part of C.)
--
"To burn always with this hard, gem-like flame, to maintain this
ecstasy, is success in life." -- Walter Pater
i wanna access the memory address at 0x00(first segment) and then
printf a range of memory from the first segment to a specific address
something is wrong with this idea ?if this ,is possible please help
me?
You can "legally" only look at memory that belongs to your
program. "Legally" includes memory that are variables you
defined and memory you allocated. With everything else you
are at the mercy of your system. There may be parts of the
address space that you can look at and others that you can't.
And to some of the addresses you may even write to. For the
address NULL you always will get a crash if you try to read
from it or write to it (but mind, NULL is not always necessary
0 in this context).
And then you should be aware that the addresses you use on
most modern systems are virtual addresses and not physical
addresses. So if you try to do something like e.g. reading
from or writing to the video buffer since you read something
about the address range where it's mapped in memory you are
out of luck on modern systems - they won't let you touch this
memory at all (unless you're getting at the level the kernel
is operating on). If you want to do things like that you have
to go back to something like DOS, on (reasonable) multi-tasking
and, even more, multi-user systems completely different rules
apply. All low level things are protected from being peeked or
even poked at by normal users. And then you really would have
to ask in a group dealing with the low level specifics of your
operating system.
So, again, the question: what do you want to achieve? Just
saying "wanna access the memory address at 0x00" isn't any-
thing to go by. What's the purpose of accessing that address?
Mere curiousity is absolutely fine, but it's unclear if your
looking in a completely wrong direction where there are just
impenetrable walls.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
On Jul 3, 11:51 pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
Jrdman <ahmed.bo...@gm ail.comwrote:
i wanna access the memory address at 0x00(first segment) and then
printf a range of memory from the first segment to a specific address
something is wrong with this idea ?if this ,is possible please help
me?
>
You can "legally" only look at memory that belongs to your
program. "Legally" includes memory that are variables you
defined and memory you allocated. With everything else you
are at the mercy of your system. There may be parts of the
address space that you can look at and others that you can't.
And to some of the addresses you may even write to. For the
address NULL you always will get a crash if you try to read
from it or write to it (but mind, NULL is not always necessary
0 in this context).
>
And then you should be aware that the addresses you use on
most modern systems are virtual addresses and not physical
addresses. So if you try to do something like e.g. reading
from or writing to the video buffer since you read something
about the address range where it's mapped in memory you are
out of luck on modern systems - they won't let you touch this
memory at all (unless you're getting at the level the kernel
is operating on). If you want to do things like that you have
to go back to something like DOS, on (reasonable) multi-tasking
and, even more, multi-user systems completely different rules
apply. All low level things are protected from being peeked or
even poked at by normal users. And then you really would have
to ask in a group dealing with the low level specifics of your
operating system.
>
So, again, the question: what do you want to achieve? Just
saying "wanna access the memory address at 0x00" isn't any-
thing to go by. What's the purpose of accessing that address?
Mere curiousity is absolutely fine, but it's unclear if your
looking in a completely wrong direction where there are just
impenetrable walls.
Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.d e
\______________ ____________ http://toerring.de
actually i wanna make a small memory dumper and it seems that it's not
the correct way.so what should i know to be able to write a basic
memory dumper?
thanks.
In article <ec4b23fd-b4d0-45c6-aa51-5e8c2662fcfc@79 g2000hsk.google groups.com>,
Jrdman <ahmed.bou23@gm ail.comwrote:
>actually i wanna make a small memory dumper
Of physical or virtual memory?
We can deduce from your earlier questions that you are using
a system that has segment registers, but we do not know whether
you are trying to write a routine to dump a block of memory
from the current program, or if you are trying to dump a block
of memory from a different program that is running under your
username, or if you are trying to dump a block of memory from a
different program that is running under a different username,
or if you are trying to examine physical memory on an embedded
system, or if you are trying to examine physical memory on
a "hosted" multiprocessing system (such as MS Windows) ??
--
"When we all think alike no one is thinking very much."
-- Walter Lippmann
actually i wanna make a small memory dumper and it seems that it's not
the correct way.so what should i know to be able to write a basic
memory dumper?
thanks.
What kind of memory you want to "dump"? On basically all modern
operating systems your program is running in a kind of memory
"sandbox", i.e. the addresses your program sees have nothing
at all to do with "real" addresses. All you could dump is the
mostky rather uninteresting address space of your program. You
could do such memory dumps twenty years ago with CP/M, DOS,
TOS and similar rather primitive operating systems that exposed
everything to a user program. Back then that was possible - there
was just a single task running because you did something stupid
you cursed and toggled the power button. But with systems running
several programs at once (or serving several users at once!) that
is not an option anymore.
So, if you want to do a "real" memory dump, where "real" means
not just what the system your program is running under allows
it to see but what's really stored in physical memory addresses,
then you have to resort to extremely system-specific methods (or
use a system like DOS that lets you mess around with all the
internals). But comp.lang.c isn't the right group for asking
questions about that since that's not anything related to the C
language but all about something extremely system specific. It
will be rather different if you try to do it under Windows,
Linux or MacOSX etc. So you will have to pick a newsgroup that
deals with the low level innards of the system you're using,
that's where the experts for that can be found and that are
prepared to answer such questions.
>>is it possible to use a pointer to see the data stored in a
>>given address in the memory ?
>>
>Yes, you can convert any pointer to a character pointer type
>and examine the bytes it points to. The consequences of
>following an invalid pointer, or looking beyond the end of the
>object it points to, are undefined.
>
.... snip ...
>
but what if we repalce &a with an arbitrary address(number) cuz
i wanna access a specific place?
There is no portable (standard C) way to do that. For something
specific to your system, read the system documentation.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
actually i wanna make a small memory dumper and it seems that it's not
the correct way.so what should i know to be able to write a basic
memory dumper?
One thing you should know is that you don't need to quote an entire
article when you post a followup. Just quote enough so your followup
makes reasonable sense to someone who didn't see the parent article.
In particular, don't quote signatures unless you're actually
commenting on them.
There is no portable way to write a memory dumper in C, or in any
other language as far as I know. You're delving into some very
system-specific stuff.
The system(s) you're probably using almost certainly use virtual
memory, which means that any addresses are specific to the current
execution of your program.
*If* it's possible to write a memory dumper, then converting from an
integer representation of an address to an unsigned char* and then
dereferencing it is likely to be as good a way as any. That is,
unless your system provides something better. <OT>On Unix-like
systems, consider something like /dev/mem, /dev/kmem, /proc/*.</OT>
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Comment