Solved

Reading integers from disk

Posted on 2006-11-09
3
237 Views
Last Modified: 2010-04-15
When I read from disk I usually read into a void* type memory block, and then sort out the types using pointers.

But, I find strange discrepencies sometimes when dealing with integers.  For example, if I have a 64-bit integer stored in a void memory block, I access it by dereferencing the region in memory, and casting it to the appropriate type.

uint64_t number = (uint64_t) *( (byte*) membuffer + offset);

I also have to cast the void* pointer itself to a char*, or my byte* typedef, in order to add the offset distance.

I can also access the integer like this:

memcpy(&number, (byte*) membuffer + offset, sizeof(number));

The first method seems more direct, but it sometimes results in garbage numbers, whereas the second method always works properly.  Is there some reason that accessing the data via dereferencing the pointer and casting might not work properly?
0
Comment
Question by:chsalvia
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 17909726
does
uint64_t number = *( (uint64_t*) ( (byte*) membuffer + offset));
work any beter?
If not, there may be alignment issues for pointers to different types
0
 

Author Comment

by:chsalvia
ID: 17910014
That worked correctly, thanks.  But I'm confused.  Why would casting to a pointer-to-integer type, and then dereferencing that, result in something different than dereferncing a pointer and casting to an integer type?  It's the same memory region.
0
 
LVL 84

Expert Comment

by:ozo
ID: 17910088
when you dereference a byte pointer, you get a byte.  when you dereference a uint64_t pointer, you get a uint64_t
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question