LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-01-2005, 02:03 PM   #1
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Rep: Reputation: 31
Reading specific memory blocks with C++


Hello, Ive been reading this tutorial : http://www.cplusplus.com/doc/tutorial/files.html (just to prove i have done some research).

It demonstrates a way of copying a file to a specific memory block. I'd like to know if theres a way of retrieving the contents of a specific memory location, in my case the contents of the memory block is an integer which id like to be returned to stdout. Any ideas?

Thanks
 
Old 12-01-2005, 03:25 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Well..yes. If you don't care about values beeing big or little-endian you can simply cast the pointer to int or long or whatever type and read it.
Code:
char memblock[something];
int *myval_ptr;
int myval;
myval_ptr = (int *)(memblock+someoffset);
myval = *myval_ptr;
 
Old 12-01-2005, 06:06 PM   #3
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Thats brilliant, exactly what I'm after! Thanks, i presume its the same for arrays?

Thanks for your quick reply, I really appreciate it.
 
Old 12-03-2005, 02:55 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Yep..Array is just a block of memory starting from pointer arr when you have something like char arr[something].

Note that you should be careful when using this method, because one small mistake and you get a segmentation fault. Nothing will stop you from accessing memory your program doesn't own (well.. the system will stop you...).
 
Old 12-23-2007, 05:24 PM   #5
bamdiedwergie
LQ Newbie
 
Registered: Nov 2007
Posts: 1

Rep: Reputation: 0
reading memory blocks

I have tried the example of reading the memory blocks in the binary file reading,using pointers as in the example . i used a loop just to seeeach value , the strang thing is that all the numbers are the same.I have tried with all data types possible .

the code is
....
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();

cout << "the complete file content is in memory " <<"\n";

unsigned char *myval_ptr;
unsigned char myval;
for (int i = 0;i < size;i++)
{
myval_ptr = (unsigned char *)(memblock+i);
cout << myval <<"\t";
};


The file is an image file
there is a header with name , surname , date , and aquisition parameters
followed by the image data .
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Want to limit Memory to a specific Process tstaples Linux - General 3 01-26-2005 01:59 PM
Reading Memory Value from ELF Binary Damaged Soul Programming 4 11-24-2004 11:52 PM
reading data from a specific hd hard locks system thesammy Linux - Hardware 0 07-17-2004 02:17 AM
CPU & Memory specific drivers robhargreaves Linux - Hardware 2 01-24-2004 02:13 PM
Linux Redhat 7.2 is not reading all my memory? ForumKid Linux - Hardware 3 08-06-2003 10:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration