Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 19th, 2005, 2:39 AM   #1
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
Copying memory address values

Hi

I have a buffer whose structure goes as the following:

[1byes of byte][2byes of unsigned short][4byes of unsigned integer][4 bytes of unsigned integer][1byes of byte]

I am trying to store the second it of the buffer "[2byes of unsigned short]" into a vaiable named sss;
say x is the buffer variable name.
Currently Im reading:
unsigned short sss = *(unsigned short*)  (&x+ 1);
which doesnot quite give me the exact value.

just wondering if there was any way of doing this simply without going thru making structures.

Thanks
rsnd is offline   Reply With Quote
Old Jun 19th, 2005, 5:45 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Your question isn't terribly clear. So, I'll try to guess your actual question and then (assuming that is what you meant) answer it.

I'm assuming you mean you have a structure something like;
[PHP]
struct Name
{
char a_byte;
unsigned short a_ushort; // assuming sizeof (unsigned short) is 2
unsigned a_unsigned_1; // assuming sizeof(unsigned) is 4
unsigned a_unsigned_2;
char another_byte;
} x;
[/PHP]
I assume your question amounts to "How do I get the value of x.a_ushort without using standard structure notation?"

The short answer is that you can't, at least not in general. You might be able to with specific compilers, but your code won't be portable between compilers or operating systems.

The longer answer is that layout of struct members is "implementation defined" according to the standard. In theory, compilers are not prevented from insert padding (extra memory) between the data members, because the standard says nothing one way or the other. In practice, a lot of compilers do actually insert padding so that the address of every variable is aligned in some way. For example, a lot of 32 bit systems run a lot more efficiently if the start of (say) an integer is at an address that is a multiple of 4 bytes. Compilers on those systems will often add three bytes of padding between a_byte and a_ushort: the cost is that structures take more memory space, but the benefit is that operations on structure members execute faster.

Which means that the offset between &x and &(x.a_ushort) is compiler dependent.
grumpy is offline   Reply With Quote
Old Jun 19th, 2005, 6:28 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Another possibility is that if you're talking about an array that is packed in that fashion, you may be encountering an endian issue. Depends on who and how is packing it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 19th, 2005, 8:09 AM   #4
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
Quote:
Originally Posted by grumpy
Your question isn't terribly clear. So, I'll try to guess your actual question and then (assuming that is what you meant) answer it.

I'm assuming you mean you have a structure something like;
[PHP]
struct Name
{
char a_byte;
unsigned short a_ushort; // assuming sizeof (unsigned short) is 2
unsigned a_unsigned_1; // assuming sizeof(unsigned) is 4
unsigned a_unsigned_2;
char another_byte;
} x;
[/PHP]
I assume your question amounts to "How do I get the value of x.a_ushort without using standard structure notation?"

The short answer is that you can't, at least not in general. You might be able to with specific compilers, but your code won't be portable between compilers or operating systems.

The longer answer is that layout of struct members is "implementation defined" according to the standard. In theory, compilers are not prevented from insert padding (extra memory) between the data members, because the standard says nothing one way or the other. In practice, a lot of compilers do actually insert padding so that the address of every variable is aligned in some way. For example, a lot of 32 bit systems run a lot more efficiently if the start of (say) an integer is at an address that is a multiple of 4 bytes. Compilers on those systems will often add three bytes of padding between a_byte and a_ushort: the cost is that structures take more memory space, but the benefit is that operations on structure members execute faster.

Which means that the offset between &x and &(x.a_ushort) is compiler dependent.
Ita a big problem indeed! Well...what if I made an array of charz...does compilers stick bits inside array elements too?
rsnd is offline   Reply With Quote
Old Jun 19th, 2005, 8:41 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Arrays are not padded. If you store the array contents as two individual bytes and read them back as a short unsigned (two bytes), you may encounter endian issues, as mentioned in my previous post. This, too, is implementation dependent. If you store them as a short and fetch them as a short it will not be an issue. When programming sockets the problem is so common that there are utility functions to guarantee the correct order for the network or for your machine.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:20 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC