ishaangupte
ishaangupte

Reputation: 313

Why am I getting bad file descriptor?

uint8_t *out = mmap(NULL, OUT_SIZE, MAP_ANON, PROT_READ | PROT_WRITE, -1, 0);

I am running this line on an ARM64 Macbook and I keep getting Bad file descriptor. I am not trying to map a file, so how do I fix this?

Upvotes: 0

Views: 466

Answers (1)

John Bollinger
John Bollinger

Reputation: 180048

You appear to have reversed the prot argument with the flags argument. It is plausible that as a result, mmap() thinks you are trying to map a real file, and therefore expects the given file descriptor to be valid. Of course, -1 is not a valid file descriptor.

It looks like the call you wanted would be:

uint8_t *out = mmap(NULL, OUT_SIZE, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);

Note: it occurs to me that detecting this kind of error is a good reason for the convention (or requirement, in some implementations) that the file descriptor be specified as -1 for an anonymous mapping.

Upvotes: 1

Related Questions