F
frank
I'm not quite done with my latest dabbling with malloc. Assume that bar
and baz are constructors; that is, they allocate memory and do not free
it, but pass a pointer and the responsibility to caller to free it.
I seemed to have unlearned things about referencing and dereferencing
since I first read K&R (that was two concussions ago).
My first question is how do I see the return values of malloc in these
constructors?
Thanks for your comment and cheers,
dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra malloc2.c -o out
malloc2.c: In function ‘bar’:
malloc2.c:9: warning: initialization makes integer from pointer without a
cast
malloc2.c:10: warning: format ‘%d’ expects type ‘int’, but argument 2 has
type ‘char **’
malloc2.c:9: warning: unused variable ‘r’
dan@dan-desktop:~/source$ ./out
-1075468176
qwerty
0
dan@dan-desktop:~/source$ cat malloc2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void
bar (char **s)
{
*s = malloc (13);
int r = &s;
printf ("%d\n", s);
}
char *
baz (void)
{
char *s = malloc (17);
return s;
}
int
main (void)
{
void bar (char **);
char *baz (void);
char *b;
int c;
bar (&b);
b = baz ();
#if 1
c = *b;
strcpy (b, "qwerty");
printf ("%s\n", b);
printf ("%d\n", c);
#endif
return 0;
}
// gcc -std=c99 -Wall -Wextra malloc2.c -o out
dan@dan-desktop:~/source$
and baz are constructors; that is, they allocate memory and do not free
it, but pass a pointer and the responsibility to caller to free it.
I seemed to have unlearned things about referencing and dereferencing
since I first read K&R (that was two concussions ago).
My first question is how do I see the return values of malloc in these
constructors?
Thanks for your comment and cheers,
dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra malloc2.c -o out
malloc2.c: In function ‘bar’:
malloc2.c:9: warning: initialization makes integer from pointer without a
cast
malloc2.c:10: warning: format ‘%d’ expects type ‘int’, but argument 2 has
type ‘char **’
malloc2.c:9: warning: unused variable ‘r’
dan@dan-desktop:~/source$ ./out
-1075468176
qwerty
0
dan@dan-desktop:~/source$ cat malloc2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void
bar (char **s)
{
*s = malloc (13);
int r = &s;
printf ("%d\n", s);
}
char *
baz (void)
{
char *s = malloc (17);
return s;
}
int
main (void)
{
void bar (char **);
char *baz (void);
char *b;
int c;
bar (&b);
b = baz ();
#if 1
c = *b;
strcpy (b, "qwerty");
printf ("%s\n", b);
printf ("%d\n", c);
#endif
return 0;
}
// gcc -std=c99 -Wall -Wextra malloc2.c -o out
dan@dan-desktop:~/source$