LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-11-2007, 07:23 AM   #1
nesta
Member
 
Registered: Aug 2006
Posts: 100

Rep: Reputation: 15
how to access unaligned address of memory?


hi all,
i have a kit with arm processor and i have simulated my actual problem with the following code:
Code:
#include <stdio.h>
#include<stdlib.h>

int main ()
{
	char* temp = malloc(100);
	*temp = 'a';
	temp++;
	*(int*)temp = 13;
	temp +=4;
	return 0;
}
as u can see from the previous code:i want to store an int variable into a memmory address that is starting with odd number i.e. here is the layout of the data stored in my memory:
suppose that temp start address is 0x08 for instance,
then i saved the ascii value of a in this address (0x08) afterwards i increased the temp to be 0x09 and now wanna write an integer value in this odd address (0x09)but this does not happen as i found the processor start saving this value from the even address (0x08)i.e. it overrides the ascii value of a.

i want to force the processor to store the int value from address (0x09)?
how can i do that, note that i m using arm toolchain not gcc
and the previous code is just a simulation to my problem.
thanks in advance.
 
Old 05-11-2007, 09:28 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Do you have the equivalent of memcpy()?
Code:
  int i=13;
.........
  memcpy(temp,&i, sizeof(int));
 
Old 05-12-2007, 11:22 AM   #3
nesta
Member
 
Registered: Aug 2006
Posts: 100

Original Poster
Rep: Reputation: 15
i have manged from accessing the unaligned memory address by using __packed before the pointer as follows:
Code:
	
        int main ()
     {
        char* temp;
	int x = 13;
	temp = malloc(100);
	*temp = 'a';
	temp++;
	*(__packed int*)temp = x;
	temp +=4;
	*temp = 'c';
	return 0;}
so i want to ask 2 questions:
1. does __attribute__ ((packed)) have the same effect of __packed ? i.e.
Code:
packed int* x;
is equivalent to
Code:
int* x __attribute__ ((packed));
or not?

2. when i tried to compile the following simple code :
Code:
int main ()
{
	__packed char* temp;

	return 0;
}
when i tried to compile it using arm-elf-gcc i got the following error:
Code:
main.c: In function `main':
main.c:28: `__packed' undeclared (first use in this function)
main.c:28: (Each undeclared identifier is reported only once
main.c:28: for each function it appears in.)
main.c:28: parse error before "char"
make: *** [main.o] Error 1
while when i tried to compile it using armcc ,
it was compiled suucessfully?

so should i search for the equivalence of __packed in arm-elf-gcc which is the toolchain i m wroking on, or there is something that should i include before using __packed.

thanks in advance
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
gdb : Cannot access memory at address 0x99f825d4 johnpaulodonnell Linux - Newbie 1 03-23-2007 08:14 PM
Access address out of main memory tsik Programming 3 01-17-2007 06:07 AM
Memory Mapping (same physical address = virtual address) skd.tech Linux - Kernel 7 07-24-2006 11:13 PM
36 bit memory address Schmidt Programming 2 03-03-2005 01:45 AM
RH 8.0 Mapping Virtual Memory to get access to VMIC Reflective Memory PCI card. Merlin53 Linux - Hardware 0 05-05-2003 12:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration