pointers to structures

S

Sam

Hi everyone

I'm confused by these declarations:

struct hostent *he;
server *s = (server*)malloc(sizeof(server));

/* in the code *
he = gethostbyname(name);

(server is defined in the include as

typedef struct {
int fdin, fdout;
FILE *fin, *fout;
} server;

)

Am I right in thinking that both *he and *s are pointers to structures? In
that case, how come memory is only allocated for *s and not *he? When the
members of *he are accessed later on in the code, where does the memory
come from? What rules govern when you need to malloc and when you don't?

Many thanks for advice
Sam
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Sam said:
Hi everyone

I'm confused by these declarations:

struct hostent *he;
server *s = (server*)malloc(sizeof(server));
Don't cast the return of malloc.
/* in the code *
he = gethostbyname(name);

(server is defined in the include as

typedef struct {
int fdin, fdout;
FILE *fin, *fout;
} server;

)

Am I right in thinking that both *he and *s are pointers to structures? In
Well he and s are the pointers, not *he/*s.
that case, how come memory is only allocated for *s and not *he? When the
members of *he are accessed later on in the code, where does the memory
come from?
"somewhere". In this case gethostbyname returns a pointer to a static
buffer.

(Thus the next time you call gethostbyname the same buffer gets reused,
so your original pointer doesn't point to the data you originally had..)
What rules govern when you need to malloc and when you don't?
Documenation of the functions you use. If it's your own code,
(no 3.party functions/apis involved), it's ofcourse up to you how to
allocate required storage.
 
R

Richard Tobin

struct hostent *he;
server *s = (server*)malloc(sizeof(server));
he = gethostbyname(name);
Am I right in thinking that both *he and *s are pointers to structures?
Yes.

In
that case, how come memory is only allocated for *s and not *he?

Either gethostbyname() allocates the memory (by calling malloc()), or
the pointer it returns is to a static structure.

-- Richard
 
S

Sam

Don't cast the return of malloc.
OK

Well he and s are the pointers, not *he/*s.
OK

"somewhere". In this case gethostbyname returns a pointer to a static
buffer.

(Thus the next time you call gethostbyname the same buffer gets reused,
so your original pointer doesn't point to the data you originally had..)

Ah, I see!

One further question, for small structures, is it better to do this:

server *s = malloc(sizeof(server));
s->member = etc.

or this:

server s;
s.member = etc.

?
 
R

Richard Bos

Sam said:
One further question, for small structures, is it better to do this:

server *s = malloc(sizeof(server));
s->member = etc.

or this:

server s;
s.member = etc.

The size of the struct hardly matters, unless your structs types are
many kilobytes large - rare, and rarely a good idea.
For normal structs, small, middling and large, the former has the
disadvantage that you must remember to free() the memory, but the
advantage that it's more flexible; you can, for example pass s back to
the function that called this one. This is often used in constructor
functions. Of course, that calling function does then have the duty to
call free(). You can also use malloc() to allocate memory for more than
one struct at a time, for example, without having to know at compile
time how many structs you want. You can't do that with the second kind
of code, unless you have a C99 compiler and can use variable-length
arrays.
The latter has the advantage that it's simple, and the disadvantage that
it's less flexible. You can't do all these complicated things, but if
you only need the one struct, it's definitely less hassle. The only
other disadvantage is that implementations are only required to support
automatic objects below a certain size, but that doesn't apply to small
or large structs, only to gargantuan ones.

Richard
 
C

CBFalconer

Sam said:
I'm confused by these declarations:

struct hostent *he;
server *s = (server*)malloc(sizeof(server));

/* in the code *
he = gethostbyname(name);

(server is defined in the include as

typedef struct {
int fdin, fdout;
FILE *fin, *fout;
} server;

So are we (confused). struct hostent is never defined, neither is
gethostbyname. Potential errors are masked by the foolish cast of
malloc. This smells of off-topic non-standard lack of portability,
and probably should be asked on a group dealing with your
particular system and OS. Possibly comp.unix.programmer
 
R

Richard Tobin

This smells of off-topic non-standard lack of portability,
and probably should be asked on a group dealing with your
particular system and OS. Possibly comp.unix.programmer

Oh come on. The code uses non-standard libraries, but his problem is
obviously - as he states explicitly - that he doesn't know when he has
to use malloc() to allocate space.

Try to find an interpretation that makes someone's article reasonable,
rather than one that makes it unreasonable.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top