How access a specific memory portion through printf() function????


 
Thread Tools Search this Thread
Top Forums Programming How access a specific memory portion through printf() function????
# 1  
Old 03-29-2012
How access a specific memory portion through printf() function????

Hi friends,
Hope everyone is doing well. Please have a look at this simple program, you will figure out what I want.


Code:
 
#include <stdio.h>
 
int main()
{
printf("Enter an integer!\n");
scanf( "%d", 134511890 ); // Valid address on my computer
 
printf( "%d\n", ???? );
return 0;
}

What should I write instead of ??? to access the data that I just stored in that particular memory location???
Here is another program which works, but not the way I would want.

Code:
 
#include <stdio.h>
 
int main()
{
printf("Enter an integer!\n");
scanf( "%d", 134511890 );
 
int *p;
 
p = (int *)134511890;
 
printf( "%d\n", *p );
return 0;
}

Is it possible to read data from a specific memory address directly, without creating any pointers???
Looking forward to your helpful replies!
Thanks in advance!
# 2  
Old 03-29-2012
printf ("%d\n", (int *)134511890) might work.

Assigning a pointer variable to the address seems neater, though. Why do you need to access a specific address anyway?
# 3  
Old 03-29-2012
Quote:
Originally Posted by gabam
Is it possible to read data from a specific memory address directly, without creating any pointers???
Looking forward to your helpful replies!
Thanks in advance!
Yes it is possible on a system that doesnt implement any kind of memory management...so that there is no translation from logical to physical memory..."what you want is what you get". It is highly unlikely to find a MPU that doesnt implement memory mgmt nowadays even in the world of embedded systems...so looks like "you cant always get what you want..." Smilie
# 4  
Old 03-29-2012
How do you know that 134511890 is a valid memory address? Unless you mapped it in yourself, it likely isn't.

Most modern architectures and operating systems use virtual memory. You don't get physical addresses inside a user-mode process anymore, you get virtual ones, which the kernel can assign to whatever physical address it pleases. Most or all of the processes you run probably have their stacks in the exact same virtual location and still don't overlap because the physical addresses assigned to these processes are different.

To make this scheme still work quickly, the processor translates virtual addresses into physical ones in hardware at runtime.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

printf function

#include<stdio.h> int counter; int fibonacci(int n) { counter += 1; if ( n <= 2 ) return 1; else return fibonacci(n-1) + fibonacci(n-2); } int main(void) { int i; int sum ; for( i = 1 ; i<= 10; i++) { counter = 0; sum... (1 Reply)
Discussion started by: vincent__tse
1 Replies

2. Shell Programming and Scripting

Help with reading a specific portion of a file using PERL

Hello, I am trying to read from a file using PERL:confused, however i need to read specific portions of the file the file goes like this <Name 1 Hono <Name 2 Jack and so on anyways i need to be able to write a program that ONLY opens the lines beginning with "<"? so it would... (2 Replies)
Discussion started by: UNDstudent
2 Replies

3. Shell Programming and Scripting

Extract portion of log info based on specific word

Hi Gurus, I'm using HP-UX B.11.23 operating system. I've been trying to extract a specific wording for example: "A tool used by tp produced warnings" from my below log data, but could not find a way to solve it. My intention is, if the log contain the word: "A tool used by tp produced... (9 Replies)
Discussion started by: superHonda123
9 Replies

4. Programming

Internals of the printf function?

hey all, im a new programmer. i was wondering how you would go about writing the printf function yourself? it is my understanding that when you call printf you are calling an already written function and just providing an argument? if this is the case, is it possible to write that function... (8 Replies)
Discussion started by: Christian.B
8 Replies

5. Shell Programming and Scripting

Cutting out text from specific portion on filename

Hi, how do I go about cutting out the first numeric characters after the word "access"? access1005101228.merged-00.15.17.86.d8.b8.log.gz (16 Replies)
Discussion started by: GermanJulian
16 Replies

6. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

7. Solaris

File cache portion of memory on Solaris

I'm looking to get the file cache portion of physical (real) memory on a Solaris workstation (Similar to the Cache: line in /proc/meminfo on some Linux systems): # swap -s; swap -l; vmstat 2 2; echo "::memstat" | mdb -k total: 309376k bytes allocated + 41428k reserved = 350804k used,... (5 Replies)
Discussion started by: Devyn
5 Replies

8. Shell Programming and Scripting

Collecting specific portion from a file

I have a file which contains data like a b x col1:data1 formula:data3 this is for 2 a c col1:@bkw formula:dontad ad asd as per a \ i want the data from col1 and formula to keep the col1 data in left side of excel and col2 data in right side of it (1 Reply)
Discussion started by: bmrout007
1 Replies

9. Programming

why printf() function don't go work?

I use FreeBSD,and use signal,like follows: signal(SIGHUP,sig_hup); signal(SIGIO,sig_io); when I run call following code,it can run,but I find a puzzled question,it should print some information,such as printf("execute main()") will print execute main(),but in fact,printf fuction print... (2 Replies)
Discussion started by: konvalo
2 Replies

10. Shell Programming and Scripting

How to print a % within a printf() function using awk

Here is the code I'm using { printf("%11d %4.2f\% %4.2f\%\n", $1,$2,$3); } I want the output to look something like 1235415234 12.24% 52.46% Instead it looks something like 319203842 42.27\%4.2f\% How do I just print a "%" without awk or printf thinking I'm trying to do... (1 Reply)
Discussion started by: Awanka
1 Replies
Login or Register to Ask a Question