I had next code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <memory>
#include <string.h>
int main()
{
char a = 'A';
unsigned int b = 1000;
char* arr = (char*)malloc(sizeof(a));
memcpy(arr, &a, sizeof(a));
arr = (char*)realloc(arr, sizeof(arr) + sizeof(b));
memcpy(arr + sizeof(a), &b, sizeof(b));
const char* pData = arr;
assert(*pData == a);
pData += sizeof(a);
assert(*(unsigned int*)pData == b);
printf("finished\n");
}
This code work fine on system wirth x86 preocesors, but failed on Blackfin device. On Blackfin device error occured. From error message first line is:
Data access misaligned address violation
I read that Blackfin processor don't can work without data alignment to word size. I tryied next code then:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <memory>
#include <string.h>
int main()
{
char a[] = "AAA";
unsigned int b = 1000;
char* arr = (char*)malloc(sizeof(a));
memcpy(arr, &a, sizeof(a));
arr = (char*)realloc(arr, sizeof(arr) + sizeof(b));
memcpy(arr + sizeof(a), &b, sizeof(b));
const char* pData = arr + sizeof(a);
assert(*(unsigned int*)pData == b);
printf("finished\n");
}
This code work on x86 and Blackfin hardware. Now I have question: Is exist ways to align data not manually from code, but via, for example, compilers option?
Thank you for attention!