Hello,

I have observed a data access misaligned error in my code. I am trying to isolate the issue and find reason for its occurrence.

float *mul;

int main(void)
{
   int i;
    mul=(float *)malloc(36000);
    *mul=0;
    for(i=1; i<9000; i++)
    {
        *(mul+i)=*(mul+(i-1))+(1/(float)(72000/2));
        printf("%f\t", *(mul+i));
    }
    free(mul);
    return 0;
}

Please point errors in my code and if possible some rectifications that I could try.

Thank you.

Dani AI

Generated

A bus-error / “data access misaligned” usually means a float is being accessed at an address the CPU requires to be aligned. In this kind of code the single biggest practical cause is a bad pointer value rather than a mysterious float size: if the malloc prototype is missing (no #include <stdlib.h>), an old C compiler will assume malloc returns int and the returned pointer can be truncated on 64‑bit systems. Casting the result of malloc in C can hide that mistake, producing a misaligned pointer at runtime. was right to flag the allocation and casting issues; fix the headers and allocation first.

A minimal, safer pattern (zeroed memory, check success, clear float constants and readable indexing):

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    size_t n = 9000;
    float *mul = calloc(n, sizeof *mul);
    if (!mul) { perror("calloc"); return 1; }

    mul[0] = 0.0f;
    for (size_t i = 1; i < n; ++i)
        mul[i] = mul[i-1] + 1.0f / 36000.0f;

    /* print or use values... */
    free(mul);
    return 0;
}

Troubleshooting checklist: compile with warnings enabled (e.g. -Wall -Wextra) and treat warnings seriously; try AddressSanitizer (-fsanitize=address,undefined) or valgrind to catch bad pointers/out‑of‑bounds writes; print sizeof(float) and the pointer value (printf("%p\n", (void*)mul)) to confirm alignment; always check allocation return. If problems persist after fixing headers and allocation, look for off‑by‑one writes or other code that could overwrite the pointer. Mentioning : once headers and allocation are corrected the misaligned accesses should disappear.

Recommended Answers

All 2 Replies

> mul=(float *)malloc(36000);
a) why are you casting malloc in a C program? Did you forget to include stdlib.h?
b) who said sizeof(float) was 4 bytes on your machine?
If you want 9000 floats, then say mul = malloc( 9000 * sizeof *mul ); then it doesn't matter how big floats really are.
c) check for success before trying to dereference the memory.

> *(mul+i)=*(mul+(i-1))+(1/(float)(72000/2));
Consider using subscript notation. It means the same to the compiler, but it's easier on the eye mul[i] = mul[i-1]+(1/(float)(72000/2));

Thank you for the comments. It helped me a lot in optimising my code.

> mul=(float *)malloc(36000);
a) why are you casting malloc in a C program? Did you forget to include stdlib.h?
b) who said sizeof(float) was 4 bytes on your machine?
If you want 9000 floats, then say mul = malloc( 9000 * sizeof *mul ); then it doesn't matter how big floats really are.
c) check for success before trying to dereference the memory.

> *(mul+i)=*(mul+(i-1))+(1/(float)(72000/2));
Consider using subscript notation. It means the same to the compiler, but it's easier on the eye mul[i] = mul[i-1]+(1/(float)(72000/2));

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.