char *p; // 4 consecutive bytes in memory for the p variable
char *p = NULL;
strcpy(p, "Hello");
// Result
Segmentation Fault. Worse yet, the copy may actually succeed.
Trying to copy the string "Hello"
into location 0
(NULL
) results in a run-time Bus Error and a program crash.
Source Code: no-malloc.c
We can use malloc
or calloc
to request a pointer to a block of memory. Run man
on both functions for details.
char *q = NULL;
// Memory allocation
q = (char *)malloc(strlen("Goodbye")+1);
// Copying string to memory
strcpy(q, "Goodbye");
printf("%s\n", q);
Source Code: malloc.c
char *q = NULL;
q = (char *)malloc(strlen("Goodbye")+1);
// Proper allocate validation
if (!q) {
perror("Failed to allocate space because");
exit(1);
}
Source Code: malloc2.c
Local variables are destroyed when their enclosing function terminates. Run the following code and verify the output.
Open the source code link to verify the foo
function.
char *a = NULL;
char *b = NULL;
a = foo("Hi there, Chris");
b = foo("Goodbye");
printf("From main: %s %s\n", a, b);
Source Code: no-free.c
The address of q
is returned to main
, where there is an attempt to preserve and use the strings. The result can be disastrous.
Consider this foo
function version
char *foo(char *p) {
char *q = (char *)malloc(strlen(p)+1);
strcpy(q, p);
printf("From foo: the string is %s\n", q);
return q;
}
Is the output right? What's the difference against the no-free.c?
Source Code: no-free2.c
free
the memorychar *a = NULL;
char *b = NULL;
a = foo("Hi there, Chris");
free(a);
b = foo("Goodbye");
free(b);
printf("From main: %s %s\n", a, b);
What's the output? is this the expected behaviour?
Source code: free.c
This material is generated thanks to some extracts from following resources:
gmail | tec | intel
.com