Skip to content

Commit ea25f4b

Browse files
committed
Add reb_simulation_try_create() and clarify reb_simulation_create() allocation failure behavior.
1 parent d867fcc commit ea25f4b

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/rebound.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,18 @@ int reb_simulation_reset_function_pointers(struct reb_simulation* const r){
380380
}
381381

382382
struct reb_simulation* reb_simulation_create(){
383+
struct reb_simulation* r = reb_simulation_try_create();
384+
if (r == NULL) reb_exit("Memory allocation failed.");
385+
return r;
386+
}
387+
388+
struct reb_simulation* reb_simulation_try_create(){
383389
struct reb_simulation* r = calloc(1,sizeof(struct reb_simulation));
390+
if (r == NULL) return NULL;
384391
reb_simulation_init(r);
385392
return r;
386393
}
387394

388-
389395
void reb_simulation_copy_with_messages(struct reb_simulation* r_copy, struct reb_simulation* r, enum reb_simulation_binary_error_codes* warnings){
390396
char* bufp;
391397
size_t sizep;

src/rebound.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,10 @@ struct reb_simulation {
701701

702702
// Simulation life cycle
703703

704-
// Allocates memory for reb_simulation and initializes it.
704+
// Allocates memory for reb_simulation and initializes it. Fatal on allocation failure.
705705
DLLEXPORT struct reb_simulation* reb_simulation_create(void);
706+
// Attempts to allocate memory for reb_simulation and initializes it. Returns NULL on allocation failure.
707+
DLLEXPORT struct reb_simulation* reb_simulation_try_create(void);
706708
// Create a simulation object from a file. Set snapshot=-1 to load last snapshot.
707709
DLLEXPORT struct reb_simulation* reb_simulation_create_from_file(char* filename, int64_t snapshot);
708710
// Create a simulation object from a simulationarchive. Set snapshot=-1 to load last snapshot.

0 commit comments

Comments
 (0)