Free structures allocated in GNU function

K

k.epost

Hello list,
Many GNU C library functions take a pointer to a possibly NULL pointer
structure of some defined type. E.g. the uname function in header
sys/utsname.h takes a pointer to a struct utsname as argument.

When given the function a NULL pointer, the structure gets allocated
some space. E.g. this works:

#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>

int main(void){

struct utsname* info;
// info = malloc(sizeof(struct utsname*));
if(uname(info) != -1){
printf("GOOD\n");
printf("info -> sysname: %s\n",info -> sysname);
printf("info -> release: %s\n",info -> release);
printf("info -> version: %s\n",info -> version);
printf("info -> machine: %s\n",info -> machine);
printf("info -> nodename: %s\n",info -> nodename);
}
else
printf("BAD");


return 0;
}


Who has the responsibility of deallocating the structure which gets
allocated space by the uname function, and is it even possibly to give
that function a pointer to unitialized memory and is this behavior
portable?

Please post a link to where in the GNU C manual or elsewhere your
answer appears on the net. Thank you.
 
J

Jens.Toerring

Many GNU C library functions take a pointer to a possibly NULL pointer
structure of some defined type. E.g. the uname function in header
sys/utsname.h takes a pointer to a struct utsname as argument.
When given the function a NULL pointer, the structure gets allocated
some space. E.g. this works:
#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>
int main(void){
struct utsname* info;
// info = malloc(sizeof(struct utsname*));

Without the malloc() you will pass an unitialized pointer to the
function which is definitely a bad idea - the function can not
determine if the random memory location the pointer points to
is memory which belongs to you or if its garbage and it has to
trust you. However, you picked a function that doesn't (at least
in the implementation I have) allow you to pass it a NULL pointer.
if(uname(info) != -1){
printf("GOOD\n");
printf("info -> sysname: %s\n",info -> sysname);
printf("info -> release: %s\n",info -> release);
printf("info -> version: %s\n",info -> version);
printf("info -> machine: %s\n",info -> machine);
printf("info -> nodename: %s\n",info -> nodename);
}
else
printf("BAD");

return 0;
}
Who has the responsibility of deallocating the structure which gets
allocated space by the uname function

It depends and you must read the documentation for the function
carefully. There are two possibilities the function could deal
with the situation when you pass it a NULL pointer

a) it allocates memory which you are supposed to free() when you're
done with it

b) it gives you back a pointer to some static memory belonging to the
function and you may not free() it (in that case the memory typi-
cally will be overwritten on further calls of the function with
a NULL pointer). An example is tmpnam().

Since both are legal possibilities only the documentation for the
function can tell you.
Regards, Jens
 
M

Mark

Hello list,
Many GNU C library functions take a pointer to a possibly NULL pointer
structure of some defined type. E.g. the uname function in header
sys/utsname.h takes a pointer to a struct utsname as argument.
Or maybe it's supposed to take the address of properly defined structure...
When given the function a NULL pointer, the structure gets allocated
some space. E.g. this works:
#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>

int main(void){

struct utsname* info;
Note: that is not a NULL pointer... it's uninitialized... who knows where
it's pointing...
// info = malloc(sizeof(struct utsname*));
if(uname(info) != -1){
printf("GOOD\n");
printf("info -> sysname: %s\n",info -> sysname);
printf("info -> release: %s\n",info -> release);
printf("info -> version: %s\n",info -> version);
printf("info -> machine: %s\n",info -> machine);
printf("info -> nodename: %s\n",info -> nodename);
}
else
printf("BAD");


return 0;
}


Who has the responsibility of deallocating the structure which gets
allocated space by the uname function,
There was no allocation, you merely wrote somewhere you probably shouldn't
have.
and is it even possibly to give that function a pointer to unitialized
memory
Is it possible, yes... you just did it!
and is this behavior portable?
Yes... I'm sure it will invoke 'Undefined' behaviour on any system!
Please post a link to where in the GNU C manual or elsewhere your
answer appears on the net. Thank you.
Do your own homework.
 
J

Joe Wright

Hello list,
Many GNU C library functions take a pointer to a possibly NULL pointer
structure of some defined type. E.g. the uname function in header
sys/utsname.h takes a pointer to a struct utsname as argument.

When given the function a NULL pointer, the structure gets allocated
some space. E.g. this works:

#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>

int main(void){

struct utsname* info;
// info = malloc(sizeof(struct utsname*));
if(uname(info) != -1){
printf("GOOD\n");
printf("info -> sysname: %s\n",info -> sysname);
printf("info -> release: %s\n",info -> release);
printf("info -> version: %s\n",info -> version);
printf("info -> machine: %s\n",info -> machine);
printf("info -> nodename: %s\n",info -> nodename);
}
else
printf("BAD");


return 0;
}


Who has the responsibility of deallocating the structure which gets
allocated space by the uname function, and is it even possibly to give
that function a pointer to unitialized memory and is this behavior
portable?

Please post a link to where in the GNU C manual or elsewhere your
answer appears on the net. Thank you.

Try this..

struct utsname info; /* info is the structure */
if(uname(&info) != -1){ /* pass its address to uname */
printf("GOOD\n");
printf("info.sysname: %s\n",info.sysname);
printf("info.release: %s\n",info.release);
printf("info.version: %s\n",info.version);
printf("info.machine: %s\n",info.machine);
printf("info.nodename: %s\n",info.nodename);
}
else
printf("BAD");


return 0;
}
 

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