pointers - What non-NULL memory addresses are free to be used as error values in C? -


i need write own memory allocation functions gmp library, since default functions call abort() , leave no way can think of restore program flow after occurs (i have calls mpz_init on place, , how handle failure changes based upon happened around call). however, documentation requires value function returns not null.

is there @ least 1 range of addresses can guaranteed invalid? useful know them all, use different addresses different error codes, or possibly different ranges different families of errors.

no, there isn't portable range of invalid pointer values.

you use platform-specific definitions, or use addresses of global objects:

const void *const error_out_of_bounds = &error_out_of_bounds; const void *const error_no_sprockets = &error_no_sprockets; 

[edit: sorry, missed hoping return these values library. bdonlan says, can't that. if find "invalid" values, library won't expecting them. requirement function must return valid value, or abort.]

you in globals:

void (*error_handler)(void*); void *error_data; 

then in code:

error_handler = some_handler; error_data = &some_data; mpz_init(something); 

in allocator:

if (allocated_memory_ok) return the_memory; error_handler(error_data); abort(); 

setting error handler , data before calling mzp_init might tedious, depending how different behaviour in different cases, might able write function or macro deal it.

what can't do, though, recover , carry on running if gmp library isn't designed cope after allocation fails. you're @ mercy of tools in respect - if library call doesn't return on error, knows broken state internals left in.

but that's general view, whereas gmp open source. can find out happens in mpz_init, @ least particular release of gmp. there might way ensure in advance allocator has enough memory satisfy request(s), or there might way wriggle out without doing damage (like bdonlon says, longjmp).


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -