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.
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.