c - Storage duration when calling constructor from another function -


i have struct looks this:

struct matrix {     size_t nrow;     size_t ncol;     double *data; }; 

and 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 want have function calls constructor , returns pointer 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);      /* stuff */      return dostuff; } 

is well-defined behaviour aka dostuff guaranteed exist after function returns?

yes, not undefined behaviour.

in code, dostuff holding pointer returned malloc(). it's lifetime spanned until deallocated manually , using free(). so, returning dostuff , using later good.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -