2

I try to initialize a structure pointer with another structure pointer inside by C programming, otherwise i get the segmentation fault. The structure is defined as below:

`struct gfcontext_t{
    char *fileContent;
    size_t fileLength;
    char *response;
    int socket_hd;
};

struct gfserver_t{
    char *serverName;
    int serverPort;
    int maxConnection;
    ssize_t (*handler)(struct gfcontext_t *, char *, void * );
    struct gfcontext_t *ctx;
    int status;
};

The initialization is give inside a function:

gfserver_t * gfserver_create(){
    struct gfserver_t *gfs;
    gfs=(gfserver_t*) malloc(sizeof(struct gfserver_t));
    ......//how to do the initialization?
    return gfs;
}`
os_coder
  • 21
  • 2
  • 1
    Standard Warning : Please [do not cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()` and family in `C`. – Sourav Ghosh Jun 04 '15 at 13:07
  • 3
    `//how to do the initialization?`...show existing efforts please – Sourav Ghosh Jun 04 '15 at 13:08
  • Standard Warning #2: Please do not use a C++ compiler to compile C code. If you need to use some C code in your C++ project, compile the C code (using a C compiler) as a static or dynamic object file, and then link that object file into your C++ project when you're compiling your C++ code (using a C++ compiler). – autistic Jun 04 '15 at 13:40
  • @undefinedbehaviour isn't that a bit overkill, if the code is in the C/C++ subset ? – Quentin Jun 04 '15 at 13:44
  • @undefinedbehaviour Sorry but how did you know OP uses a `C++` compiler? – Sourav Ghosh Jun 04 '15 at 13:48
  • @SouravGhosh Probably because of the typecast on the malloc result which would otherwise produce a warning in C++ – JeremyP Jun 04 '15 at 13:52
  • @JeremyP I see, but there are cases (tutorials, professors) which/who _tell you_ to cast the result , even in C. :-( – Sourav Ghosh Jun 04 '15 at 13:55
  • @SouravGhosh Current received wisdom is not to cast the result since you catch more errors that way. Unsurprisingly [the question has been asked before](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – JeremyP Jun 04 '15 at 14:05
  • @JeremyP did you miss my very first comment? :-) – Sourav Ghosh Jun 04 '15 at 14:07
  • @SouravGhosh I missed the link in it :) You are right that sometimes people are instructed to put the cast in, but I don't see it very often in real World programs anymore. If you leave it out, statements like `gfs->ctx = malloc(sizeof *gfs->ctx);` are possible which are still correct if you change the type of `gfs->ctx`. – JeremyP Jun 04 '15 at 14:14
  • @JeremyP Right you are, in professional environment, it is rare, but still, in educational environment, believe me, I personally know, _not_casting `malloc()` result will trigger a "fail" in your exam. Not to mention, the widespread use of `turbo c++` as the primary choice of `c` compiler. Really feel sad. :-( – Sourav Ghosh Jun 04 '15 at 14:17
  • @SouravGhosh The return type of `gfserver_create` is invalid in C. – autistic Jun 04 '15 at 14:39
  • @undefinedbehaviour that is a typo. see the pointer declaration in the immediate next line. If you were true, that `struct` would have been missing there also. – Sourav Ghosh Jun 04 '15 at 14:40
  • @SouravGhosh So either the code isn't real or it's C++? – autistic Jun 04 '15 at 14:46

1 Answers1

1

Use:
gfs->ctx = malloc(sizeof(struct gfcontext_t));
or if you also want to initialize the gfcontext_t members to null
gfs->ctx = calloc(1, sizeof(struct gfcontext_t));

axxis
  • 954
  • 1
  • 10
  • 18