The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find Total and Free Physical Memory and Logical Memory in SOLARIS 9 0ktalmagik Filesystems, Disks and Memory 2 07-21-2008 04:38 AM
How can I get memory usage or anything that show memory used from sar file? panithat HP-UX 1 03-31-2008 09:26 AM
Memory Alignment Problem on Sun Sparcs nj302 SUN Solaris 1 09-21-2005 04:07 PM
Shared memory shortage but lots of unused memory cjcamaro UNIX for Advanced & Expert Users 1 10-13-2004 02:10 PM
Total Memory/Swap Memory ghe1 Filesystems, Disks and Memory 4 04-01-2002 07:33 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 09-18-2005
Registered User
 

Join Date: May 2005
Posts: 17
Question how to round up a memory address(memory alignment problem)

Hi, I try to marshal a unsigned int and a char * into a buffer, and then unmarshal them later to get them out. I need to put the char * in the front and unsigned int at the end of the buffer. However, my system always give me "BUS ERROR". I am using Sun Sparcs Sloris 2.10.

My code to marshal the data:

unsigned dev = 111;
unsigned inode = 222;

int s_docname = strlen(docname) + 1;
int s_key = s_docname + sizeof(unsigned) * 2;

keybuf = malloc(s_key);
memset(keybuf, 0, s_key);
memcpy(keybuf, docname, s_docname);
memcpy(keybuf + s_docname, &(dev), sizeof(unsigned));
memcpy(keybuf + s_docname + sizeof(unsigned), &(inode),
sizeof(unsigned));

My code to unmarshal the data:
char * docname = (char *)key.data;
int s_docname = strlen(docname) + 1;
dev =*((unsigned *)((char *)key.data + s_docname));
inode = *((unsigned *)((char *)key.data + s_docname +
sizeof(unsigned)));

I can print the dev and inode out in dbx debugger by typing the same
code, but running the program always have the BUS error.

I realize this is a problems of memory alignement. s_docname has byte granularity, somehow I need to "round up" my s_docname by some suitable calculation. But I really don't know how to? Help is appreciated.
Reply With Quote
Forum Sponsor
  #2  
Old 09-18-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
Here is a possible alternate solution (I really haven't understood the problem very well - I may be completely off here)
Code:
#include<stdlib.h>
#include<string.h>

int main(argc,argv)
int argc;
char *argv[];
{
        unsigned int dev=111,inode=222;
        char docname[]="/tmp/test";
        char *keybuf;
        int s_docname,s_key;

        s_docname=strlen(docname);
        s_key=s_docname+(sizeof(unsigned)*2)+1;
        keybuf=(char*)malloc(s_key);
        if(!keybuf) {
                perror("error in malloc!");
                exit(-1);
        }
        sprintf(keybuf,"%s %u %u",docname,dev,inode);
        fprintf(stdout,"%s\n",keybuf);
        docname[0]=0;
        dev=0;
        inode=0;
        fprintf(stdout,"doc: %s, dev: %d, inode: %d\n",docname,dev,inode);
        sscanf(keybuf,"%s %u %u",docname,&dev,&inode);
        fprintf(stdout,"post sscanf\n");
        fprintf(stdout,"doc: %s, dev: %d, inode: %d\n",docname,dev,inode);
        exit(0);
}
Reply With Quote
  #3  
Old 09-21-2005
Registered User
 

Join Date: May 2005
Posts: 17
Still I got same bus error. The problem is that the dev is not at the address where keybuff + s_docname is

Some processors are not able to load an integer, unsigned etc, from an address which is not a multiple of the size of that integer. For 4-byte integers, the address it is loaded from, looking at the lowest two bit only, must be 00. It is called 4-byte alignment.

I need to "round up" my s_docname by some suitable calculation, leaving what is called 'padding' between docname and dev.

I need some advice on how to round this up. Thanks a lot.
Reply With Quote
  #4  
Old 09-21-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,615
Don't dereference the pointer in place. Since you memcpy'ed it in, memcpy it back out. Then deference it.
Reply With Quote
  #5  
Old 09-21-2005
Registered User
 

Join Date: May 2005
Posts: 17
Sorry, but I don't understand what exactly you mean?
Reply With Quote
  #6  
Old 09-21-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,615
Replace:
dev =*((unsigned *)((char *)key.data + s_docname));
with:
memcpy(dev, keybuf + s_docname, sizeof(unsigned));
and make a similar for inode.
Reply With Quote
  #7  
Old 09-21-2005
Registered User
 

Join Date: May 2005
Posts: 17
Problem solved. Thank you very much, Perderabo!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0