how to round up a memory address(memory alignment problem)


 
Thread Tools Search this Thread
Top Forums Programming how to round up a memory address(memory alignment problem)
# 1  
Old 09-19-2005
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.
# 2  
Old 09-19-2005
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);
}

# 3  
Old 09-21-2005
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.
# 4  
Old 09-21-2005
Don't dereference the pointer in place. Since you memcpy'ed it in, memcpy it back out. Then deference it.
# 5  
Old 09-21-2005
Sorry, but I don't understand what exactly you mean?
# 6  
Old 09-21-2005
Replace:
dev =*((unsigned *)((char *)key.data + s_docname));
with:
memcpy(dev, keybuf + s_docname, sizeof(unsigned));
and make a similar for inode.
# 7  
Old 09-21-2005
Problem solved. Thank you very much, Perderabo!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a value to a physical memory address in bash script?

How would I write a value to a physical memory address? I was able to read a physical memory address (for example, 0x400) using this line: dd if=/dev/mem count=4 bs=1 skip=$(( 0x400 )) But I get an error: dd: 'standard input': cannot skip to specified offset when I try to write using... (1 Reply)
Discussion started by: rabrandt
1 Replies

2. Solaris

[DOUBT] Memory high in idle process on Solaris 10 (Memory Utilization > 90%)

Hi Experts, Our servers running Solaris 10 with SAP Application. The memory utilization always >90%, but the process on SAP is too less even nothing. Why memory utilization on solaris always looks high? I have statement about memory on solaris, is this true: Memory in solaris is used for... (4 Replies)
Discussion started by: edydsuranta
4 Replies

3. UNIX for Advanced & Expert Users

What is the function to get address of the virtual memory block in linux??

I want address of current virtual memory block? i am using fedora10:wall::wall: (1 Reply)
Discussion started by: powyama
1 Replies

4. Linux

change the memory address of ld.linux-so in a dynamically linked process

hi, For some special reason , I'd like to control the memory address for the shared libraries in my dynamically linked process. And it is the "ld" which interpret the dynamically linked library, and in my system, the "ld-linux.so.2" is put at 0x00812000. Then I use "prelink -r" command to... (3 Replies)
Discussion started by: zerocool_08
3 Replies

5. UNIX for Dummies Questions & Answers

change the memory address of ld.linux-so in a dynamically linked process

hi, For some special reason , I'd like to control the memory address for the shared libraries in my dynamically linked process. And it is the "ld" which interpret the dynamically linked library, and in my system, the "ld-linux.so.2" is put at 0x00812000. Then I use "prelink -r" command to change... (0 Replies)
Discussion started by: zerocool_08
0 Replies

6. Solaris

restrcit physical memory with zone.max-locked-memory

Is it possible to restrict physical memory in solaris zone with zone.max-locked-memory just like we can do with rcapd ? I do not want to used rcapd (1 Reply)
Discussion started by: fugitive
1 Replies

7. Solaris

How to find Total and Free Physical Memory and Logical Memory in SOLARIS 9

Hi, Im working on Solaris 9 on SPARC-32 bit running on an Ultra-80, and I have to find out the following:- 1. Total Physical Memory in the system(total RAM). 2. Available Physical Memory(i.e. RAM Usage) 3. Total (Logical) Memory in the system 4. Available (Logical) Memory. I know... (4 Replies)
Discussion started by: 0ktalmagik
4 Replies

8. Solaris

Error: Memory Address Not aligned

Hi, The following error message occured when I was trying to reboot my SUN machine: Memory address not aligned Its a Sun 280 R , Ultra SPARC III What should I do. Varma (3 Replies)
Discussion started by: gunnervarma
3 Replies

9. Programming

Cannot access memory at address 0x8

Hi All I have a structure pointer and setting that pointer as NULL. When i tried to access the elements in the structure i am getting the error message. "Cannot access memory at address 0x8". This i tried in LINUX. When the same program is tried thro UNIX (HP-UX), i am not getting the message... (8 Replies)
Discussion started by: rkraj
8 Replies

10. Solaris

Memory Alignment Problem on Sun Sparcs

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... (1 Reply)
Discussion started by: nj302
1 Replies
Login or Register to Ask a Question