GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-May-2006, 10:40
realnapster realnapster is offline
Junior Member
 
Join Date: Feb 2006
Posts: 51
realnapster will become famous soon enough

How to read particular memory location ?


Hi members,
Now a days i was working on a program and i made a big move in it. Now the last stage is that i came up with a memory address and i wanna read the content at that particular address.I have previously asked about reading RAM yes this thread is just the end of my program. I have got the memory location and i simply made a "long int" variable and tried to read that memory location but error in program. Here is my source code.
CPP / C++ / C Code:
#include<stdio.h>
int main()
{
     long int *a;
     a=58112;
     printf("%ld",*(58112));
     getchar();
     return 0;
}

I think that we can't assign a constant to a pointer. Am I right ? If yes then please tell me how can i read this particular memory location.


With love
REAL NAPSTER
__________________
*Labor omnia consent*** Hardwork conquers all*
  #2  
Old 02-May-2006, 10:58
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,031
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: How to read particular memory location ?


I am afraid you can not access specific memory addresses, as your Operating system probably does not allow direct access to this resource. See for reference Direct memory access thread.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 02-May-2006, 11:54
realnapster realnapster is offline
Junior Member
 
Join Date: Feb 2006
Posts: 51
realnapster will become famous soon enough

Re: How to read particular memory location ?


Oh my God it's a shock to me. I have been working on this project from a long time and when i reached my destiny, The OS sucks.
It's really bad to hear this news.Any more suggestions are welcomed.I really wanna do this at any cost.Please suggest something.

with sadness
REALNAPSTER
__________________
*Labor omnia consent*** Hardwork conquers all*
  #4  
Old 02-May-2006, 16:54
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: How to read particular memory location ?


You can use DOS
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #5  
Old 03-May-2006, 11:05
realnapster realnapster is offline
Junior Member
 
Join Date: Feb 2006
Posts: 51
realnapster will become famous soon enough

Re: How to read particular memory location ?


hi guys,
I used this code to read the memory addresses and i can read only a specific range.
CPP / C++ / C Code:
#include<stdio.h>
int main()
{
    printf("%ld",*(long int *)2281471);
    getchar();
    return 0;
}
The address range is
minimum = 2281472
maximum = 2297852
Now i wanna know if i can read this region why i can't read the region that i want ? Actually i wanna read "address : 1613758136". Please tell me what the hell is happening beyond this specific region ?

WALTP how can i use DOS to make it possible? please explain...........

With happiness
REALNAPSTER
__________________
*Labor omnia consent*** Hardwork conquers all*
  #6  
Old 03-May-2006, 11:28
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: How to read particular memory location ?


Today's operating systems protect memory from being read by programs that don't 'own' the memory. Think virus. You write a program that plays around with memory from another program. What happens to that extremely important Word doc you haven't saved yet when a different program starts messing around with Word's memory? So the new O/S won't let you access any memory but your own.

As for DOS, search for a copy of it. You can load it on a system and there is no protection -- you can look at memory to your heart's content. But be sure to read up on installation and limitations. DOS cannot handle the huge memory and disks we have today.

Quote:
Originally Posted by realnapster
Actually i wanna read "address : 1613758136".
Is that where the computer fairies have stashed their pot of hacker bits?
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #7  
Old 03-May-2006, 13:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to read particular memory location ?


Quote:
Originally Posted by realnapster
The address range is
minimum = 2281472
maximum = 2297852
Now i wanna know if i can read this region
I am assuming here that you are using a current operating system (Linux, Mac OSX, Windows XP for examples). I am assuming that you are writing a "user space" C program that isn't using some special device driver or other system program that may give you certain extra privileges. If you are writing a program for an embedded system that doesn't use a memory management system to separate and protect the memory regions (user space versus kernel space), then some or all of the conditions that I am going to describe may be different. (Your Mileage May Vary.)

First of all: when your "normal" C (or C++) program is loaded, the operating system gives it a certain region of memory to use for its variables and certain other stuff. If your program asks for memory (malloc(), for example), the operating system can give you some more. (Outside of the original region, probably). If you try to access some memory location outside of your allocated region(s), the program will bomb. That is, memory outside your program space is protected from your program. Can't read; can't write; can't do "squat".

If you try to access some arbitrary address that "just happens" to be inside your allocated region (but was not given to explicitly by a variable declaration or other allocation process) the program may do something bad, or not. That's called "undefined behavior" and can give different results for different program runs, depending on what is actually happening with that memory location.

Secondly: When the program gives you some memory (whether it is through a variable declaration or from malloc() or whatever), the value of the address that your program sees is not actually a physical address in real memory. If you think of connecting a logic analyzer to the physical address bus in such a way that you could actually track the physical memory accesses as your program runs, you would not see the same addresses as the program is using for its memory operations. Since I'm not sure why you want to access some memory location other than what your are given, I can't say whether your hopes and dreams can lead to something practical; I'm just saying that you can't do it with a "normal" C program by simply giving a pointer a value and then dereferencing it.

You can try something like the following program to see what address values your program is using as it runs. I repeat: for "normal" users with Linux or Windows XP or other such systems, the addresses that this program tells you about are not actual physical memory addresses. Period.

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
int z; /* a "global" variable */
int main()
{

    void f(void); /* a function: program space */
    int x;        /* a local "automatic" variable in user data space       */
    char *y = "Hi";      /* a pointer to a string literal in program space */
    unsigned long *huh = (unsigned long *)2281471; /* arbitrary integer value
                                                    * used as a pointer
                                                    */


    printf("&x   = 0x%08x (%u decimal)\n", (unsigned int)&x,(unsigned int)&x);
    printf("&z   = 0x%08x (%u decimal)\n", (unsigned int)&z,(unsigned int)&z);
    printf(" f   = 0x%08x (%u decimal)\n", (unsigned int)f, (unsigned int)f);
    printf(" y   = 0x%08x (%u decimal)\n", (unsigned int)y, (unsigned int)y);
    printf("Now, set y equal to the address of a block from malloc()\n");
    y = malloc(10);
    printf(" y   = 0x%08x (%u decimal)\n", (unsigned int)y, (unsigned int)y);
    free(y);
    printf(" huh = 0x%08x (%u decimal)\n", huh, huh);
    printf("*huh = 0x%08x (%u decimal)\n", *huh, *huh); /* does it bomb? */

    return 0;
}
/* just to see where "program" space is */
void f()
{
}

For GNU gcc on my Windows XP machine:
Code:
&x = 0x0022eef4 (2289396 decimal) &z = 0x00403020 (4206624 decimal) f = 0x00401173 (4198771 decimal) y = 0x00402000 (4202496 decimal) Now, set y equal to the address of a block from malloc() y = 0x006625b0 (6694320 decimal) huh = 0x0022cfff (2281471 decimal) *huh = 0x00000000 (0 decimal)

Since the value that I gave to the pointer just happened to fall close to the addresses of local variables (x, for example), the program apparently wasn't outside of its allowed region. Of course the value in that memory space is not meaningful, and if I tried to write to it, something bad could happen (or, maybe, not). The behavior is undefined, but, apparently not pathological (at least for reading).

For Borland bcc32 on the same machine:
Code:
&x = 0x0012ff88 (1245064 decimal) &z = 0x0040c558 (4244824 decimal) f = 0x00401209 (4198921 decimal) y = 0x0040a128 (4235560 decimal) Now, set y equal to the address of a block from malloc() y = 0x00842e6c (8662636 decimal) huh = 0x0022cfff (2281471 decimal) ****Then the program bombed ****

In this case the arbitrary address that I gave to the pointer is not close to the (virtual) addresses that my program is allowed to use, and the program bombs.


Regards,

Dave
  #8  
Old 10-May-2006, 00:53
Golmal Golmal is offline
Junior Member
 
Join Date: May 2006
Posts: 45
Golmal has a little shameless behaviour in the past

Re: How to read particular memory location ?


i was wondering why not use assembly language with in c program , Because with assemble language the memory can explicatily altered. However i am not sure about how virtual memory would or memory map will look at this ..
  #9  
Old 10-May-2006, 02:15
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: How to read particular memory location ?


Quote:
Originally Posted by Golmal
i was wondering why not use assembly language with in c program , Because with assemble language the memory can explicatily altered. However i am not sure about how virtual memory would or memory map will look at this ..

No it can't. Assembly has the same memory limitations as any other language.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #10  
Old 10-May-2006, 06:54
realnapster realnapster is offline
Junior Member
 
Join Date: Feb 2006
Posts: 51
realnapster will become famous soon enough

Re: How to read particular memory location ?


Thanks for your comments on my problem. But i am still working on it to get something out from the OS.

I am using Windows XP and DAVE C++.

Guys my exams are gonna start i will not be visiting the forum for some days.
Anyways thanks for you help.

REALNAPSTER
__________________
*Labor omnia consent*** Hardwork conquers all*
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Memory cannot be read? dlare9 C Programming Language 3 16-Nov-2005 07:03
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 09:25
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:19.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.