473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Memory Alignment Problem

Hi,
Good day! Anyone knows how to extract shorts, long, chars from
a char array considering the memory alignment? It's like this,
there's a char buffer, which is cast into a struct, then cast back to
a char pointer. The first two chars should be a short; when displayed
as char the number (value 0x8600, equal to 134) is correct but when a
short pointer is pointed to this char and the short is displayed, it
becomes a negative number (-31232).

TIA,
Roy

Mar 16 '07 #1
3 2477
iduniq wrote:
Hi,
Good day! Anyone knows how to extract shorts, long, chars from
a char array considering the memory alignment? It's like this,
there's a char buffer, which is cast into a struct, then cast back to
a char pointer. The first two chars should be a short; when displayed
as char the number (value 0x8600, equal to 134) is correct but when a
short pointer is pointed to this char and the short is displayed, it
becomes a negative number (-31232).
The bit pattern 0x8600 is -31232 when cast to a (16 bit) short. Are you
muddling up signed and unsigned types?

--
Ian Collins.
Mar 16 '07 #2
Ian Collins wrote:
>
iduniq wrote:
Hi,
Good day! Anyone knows how to extract shorts, long, chars from
a char array considering the memory alignment? It's like this,
there's a char buffer, which is cast into a struct, then cast back to
a char pointer. The first two chars should be a short; when displayed
as char the number (value 0x8600, equal to 134) is correct but when a
short pointer is pointed to this char and the short is displayed, it
becomes a negative number (-31232).
The bit pattern 0x8600 is -31232 when cast to a (16 bit) short. Are you
muddling up signed and unsigned types?
I think he's mixing up endianness. If you have:

char foo[] = { 0x86, 0x00 };

and read *(short *)foo on a little-endian, 16-bit-short, properly-
aligned system, you will probably (though it's probably UB according
to the standard) read 134 (0x0086). However, on a big-endian,
everything-else-the-same-as-before system, you will probably read
-31232 (0x8600).

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Mar 16 '07 #3
Hi guys,

I'm very sorry I only realise my fault after I sent SOS. Ian is
correct, there was a prior ntosh before this code.

Thanks,
Roy
On Mar 16, 9:49 pm, Kenneth Brody <kenbr...@spamcop.netwrote:
Ian Collins wrote:
iduniq wrote:
Hi,
Good day! Anyone knows how to extract shorts, long, chars from
a char array considering the memory alignment? It's like this,
there's a char buffer, which is cast into a struct, then cast back to
a char pointer. The first two chars should be a short; when displayed
as char the number (value 0x8600, equal to 134) is correct but when a
short pointer is pointed to this char and the short is displayed, it
becomes a negative number (-31232).
The bit pattern 0x8600 is -31232 when cast to a (16 bit) short. Are you
muddling up signed and unsigned types?

I think he's mixing up endianness. If you have:

char foo[] = { 0x86, 0x00 };

and read *(short *)foo on a little-endian, 16-bit-short, properly-
aligned system, you will probably (though it's probably UB according
to the standard) read 134 (0x0086). However, on a big-endian,
everything-else-the-same-as-before system, you will probably read
-31232 (0x8600).

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody |www.hvcomputer.com| #include |
| kenbrody/at\spamcop.net |www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamT...@gmail.com>

Mar 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: J. Campbell | last post by:
I posted a question some time back about accessing a char array as an array of words. In order not to overrun the char array, I padded it with enough 0x00 bytes to ensure that when accessed as...
18
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a...
13
by: Kutty Banerjee | last post by:
Hi, I ve got the following piece of code which does the role of allocating aligned memory (not sure what aligned memory allocation is either). void * _align_calloc(size_t bytes, unsigned long...
13
by: sachin_mzn | last post by:
Hi, What is the concept of memory alignment? Is memory alignment differs, If a data type is local to a function or if it is a member of structure or union? How 32 to 64 bit processor afftects the...
4
by: Romeo Colacitti | last post by:
I have a need to make a custom quasi-memory allocator, and I remembered a simple ons in K&R2. Looking at the code for it now, I think I notice a "fault" in the design, and I was wondering if...
4
by: Dharma | last post by:
hi, struct a { byte d; byte buf; }text; text.buf = 'A' text.buf = '\0'
11
by: simonp | last post by:
I'm taking an intro course on C++, and our teacher is not being clear on how stuct memory padding is determined. If the memory used by all components defined inside a struct falls between a...
13
by: Chris Thomasson | last post by:
Here is some info on a C++ allocator prototype I am working on: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/beeee1f61fdbb52c Any tried-and-true techniques for calculating the...
8
by: ramsatishv | last post by:
Hi Group, I have one question. If I am allocating memory of 38 bytes to an integer pointer, what will be the memory actually allocated? Will there be any memory alignment concept in malloc?...
66
by: Why Tea | last post by:
typedef struct some_struct { int i; short k, int m; char s; } some_struct_t; Assuming 16 bit or 32-bit alignment, can I assume that s always gets 4 or 8 bytes of allocation due to padding
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.