I have a struct that looks like this:
struct matrix {
size_t nrow;
size_t ncol;
double *data;
};
and a corresponding constructor:
struct matrix *matrix_create(const size_t nrow, const size_t ncol)
{
struct matrix *m;
m = malloc(sizeof(struct matrix));
if (!m) {
fprintf(stderr, "Memory allocation failed\n");
return NULL;
}
m->data = malloc(sizeof(double) * nrow * ncol);
if (!m->data) {
fprintf(stderr, "Memory allocation failed\n");
return NULL;
}
m->nrow = nrow;
m->ncol = ncol;
return m;
}
Now say I want to have another function that calls the constructor and returns a pointer to the struct
:
struct matrix *matrix_dostuff(const struct matrix *m1, const struct matrix *m2)
{
struct matrix *dostuff =
matrix_create(m1->nrow * m2->nrow, m1->ncol * m2->ncol);
/* do stuff */
return dostuff;
}
Is this well-defined behaviour aka is dostuff
guaranteed to exist after the function returns?