0

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!

synacker
  • 1,722
  • 13
  • 32
  • 1
    Sidenote: `arr = (char*)realloc(arr, sizeof(arr) + sizeof(b));` doesn't do what you expect. `sizeof(arr)` is size of a pointer and not same as `sizeof(a)`. – user694733 Feb 12 '14 at 12:18
  • 2
    Even before thinking of alignment you should definitively revise your skills about dynamic allocation in C. That your code works correctly is hard to believe. – Jens Gustedt Feb 12 '14 at 12:26
  • @Milovidov It seems you want to group several variables together. You could do that with `struct`. Compiler would sort out alignment and you wouldn't need to do pointer arithmetric. – user694733 Feb 12 '14 at 12:30
  • @JensGustedt where problem in my code exist? The comment about sizeof not a correct becuase sizeof return array size if array was created with [] keyword - such as in my code char a[] = "AAA" – synacker Feb 12 '14 at 12:31
  • @user694733 Thanks for comment, but I need to storage many count of data with different types. This example is small and not real for demonstartion of problem. – synacker Feb 12 '14 at 12:32
  • @Milovidov Using `struct` puts no limitiation on how you allocate memory for it. Or did you mean saving data to extenal destination (like disk/network/flash chip/...)? In that case you should use proper serialization techniques. – user694733 Feb 12 '14 at 12:40
  • @user694733 I just want to know how to allocate memory without manual data alignment. I know about structures, but, as I said before, I need to storage big count of data with different types exclusively in dynamic data arrays. – synacker Feb 12 '14 at 12:45
  • @Milovidov, your main problem has already been noted by user694733, `sizeof(arr)` isn't at all what you expect, `arr` is a pointer not an array. You'd have to keep track of the size of the allocated block. Then don't cast the return of `malloc` or `realloc`. This is superfluous in C and only hides bugs. Also don't think of `char` as all purpose data storage, that should be `unsigned char*`, or if you clearly don't want interpretation on the allocated block `void*` – Jens Gustedt Feb 12 '14 at 12:48

0 Answers0