0

I have a template function to get values to convert binary data to pod types written in C++

template<typename T>
T inline GetVal(const char * source)
{
    return *(T*)(source);
}

everything works fine on iOS Simulator... But if i run on real device it gives me EXC_BAD_ACCESS error when tries to get float or double types.

I had to change my code for device to use unions. And it fixes my problem. Here is the code:

#ifdef __IPHONE_3_2

    union U8f {
        char abc[8];
        float f;
        double d;
    };

    template<>
    float inline GetVal(const char* source){
        union U8f u8f;
        u8f.abc[0] = dest[0];
        u8f.abc[1] = dest[1];
        u8f.abc[2] = dest[2];
        u8f.abc[3] = dest[3];
        return u8f.f;
    }

    template<>
    double inline GetVal(const char* source){
        union U8f u8f;
        u8f.abc[0] = dest[0];
        u8f.abc[1] = dest[1];
        u8f.abc[2] = dest[2];
        u8f.abc[3] = dest[3];
        u8f.abc[4] = dest[4];
        u8f.abc[5] = dest[5];
        u8f.abc[6] = dest[6];
        u8f.abc[7] = dest[7];
        return u8f.d;
    }

#endif

But what i don't understand why it gives error on floats ? What did i do wrong ?

EDITED

i examined the sizeof and __alignof values on the device for misalignment issue and here is the results

bool           sizeof = 1 alignof = 1
wchar_t        sizeof = 4 alignof = 4 
short int      sizeof = 2 alignof = 2 
int            sizeof = 4 alignof = 4 
long int       sizeof = 4 alignof = 4 
long long int  sizeof = 8 alignof = 8 
float          sizeof = 4 alignof = 4 
double         sizeof = 8 alignof = 8 
long double    sizeof = 8 alignof = 4 
void*          sizeof = 4 alignof = 4

float seems to be good boy :D

1

2 Answers 2

3

you might want to check this out:

Why does this EXC_BAD_ACCESS happen with long long and not with int?

3
  • this link is really good question and answer but i dont believe this problem causes because of misalignment. code gives error on float type. not for long long, or long double.
    – Alkimake
    Feb 28, 2012 at 11:44
  • 2
    @Alkimake floats and doubles have larger alignment than the source. detay's answer's linked question's answer and comments walk you through it. good find detay +1. so, you will have to resort to viewing the assembly to debunk the float theory, rather than relying on alignof. however, you're still prone to failure in the future if you just look at what the compiler's generated. next time the compiler is updated, it may generate different assembly. for a safe fix, either use the char-by-char copy, memcpy, or write your own assembly.
    – justin
    Feb 28, 2012 at 12:02
  • 1
    @Justin @detay you 2 were right. I did use memcpy and it solved
    – Alkimake
    Feb 28, 2012 at 12:25
1

Is it possible that the source just isn't long enough?

Is the source ever 0?

How do you cast to const char* for source? and what is the source?

Consider ways to make it more type safe.

If that's all right, try creating a temporary T in GetVal, casting the temporary T to char* then writing to the temporary from source, then returning the temporary -- unnatural alignment, perhaps?

If that fails, perhaps a small program to repro this?

Note: Your fix relies on unspecified behavior.

2
  • source is just what i neeed is 4 bytes.
    – Alkimake
    Feb 28, 2012 at 11:49
  • i use NSInputStream to get source and its const uint8_t*. i simply cast it to const char*
    – Alkimake
    Feb 28, 2012 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.