Niek de Klein
Niek de Klein

Reputation: 8834

Segmentation fault in C on Mac, direct copy from a tutorial

I've started learning C and pointers and I've been working on tutorials on the internet. I assume that the code should work as it is in a tutorial, and it seems right to me, but I get a segmentation error. The code is:

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

/*
 * 
 */
int main(int argc, char** argv) {
    float fl = 3.14;
    unsigned int addr = (unsigned int) &fl;
    printf("fl's address=%u\n", addr);
    printf("addr's contents = %.2f\n", * (float*) addr);
    return (EXIT_SUCCESS);
}

The error that I get is

/Applications/NetBeans/NetBeans       
6.9.1.app/Contents/Resources/NetBeans/ide/bin/nativeexecution/dorun.sh: line 33:  1626     
Segmentation fault      sh "${SHFILE}"

Does this have to do with me using a Mac or is there something wrong with the code?

Thanks a lot, Niek

Upvotes: 2

Views: 1201

Answers (6)

swegi
swegi

Reputation: 4112

The problem is the size of int. As @paxdiablo already said, there is no guarantee that an int is big enough to hold the same value as a pointer. Try long longinstead, but be warned, the code stays in the undefined behavior corner.

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882028

That may be undefined behaviour. There's no guarantee that an int and a pointer can hold the same value, and you should not be casting between them. Use a float* instead.

C99 6.3.2.3/5 and /6 state:

5: An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.

6: Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

Upvotes: 4

yolo
yolo

Reputation: 2795

yes under win7 + mingw32, the code compiles\executes corrrectly

fl's address=2293572
addr's contents = 3.14

i think it's problem with MacOS only

Upvotes: 1

nedblorf
nedblorf

Reputation: 5365

Yes, the address space of pointer is not guaranteed to be the same as an int. In fact, this code gives me the following compilation warnings using gcc on a Mac Pro:

test.c:9: warning: cast from pointer to integer of different size test.c:11: warning: cast to pointer from integer of different size

and a seg fault as well. I would consider samplebias' code.

Upvotes: 2

samplebias
samplebias

Reputation: 37919

Try this instead:

int main(int argc, char** argv) {
    float fl = 3.14;
    float *addr = &fl;
    printf("fl's address=%p\n", addr);
    printf("addr's contents = %.2f\n", *addr);
    return (EXIT_SUCCESS);
}

Upvotes: 6

schnaader
schnaader

Reputation: 49729

The code seems to be correct, compiling with MinGW GCC and running gives the following output:

fl's address=2293528
addr's contents = 3.14

Upvotes: 1

Related Questions