Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replaced old function with malloc_two()
- Loading branch information
Showing
1 changed file
with
6 additions
and
21 deletions.
There are no files selected for viewing
27 changes: 6 additions & 21 deletions
27
Memory_Allocation/malloc_implementations/malloc_two/main.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,18 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "basic_malloc.h" | ||
#include "xmalloc.h" | ||
#include "zmalloc.h" | ||
#include "malloc_two.h" | ||
|
||
int main(void) { | ||
const char *title = "The Unix Programming Environment"; | ||
const unsigned int size = strlen(title); | ||
char *ptr; | ||
|
||
// basic_malloc() testing function | ||
// Alloca 'size' byte sullo heap, dopodiche' copia e stampa la stringa | ||
ptr = basic_malloc(size); | ||
strcpy(ptr, title); | ||
printf("%s\n", ptr); | ||
|
||
/* Con la prima funzione per l'allocazione della memoria basic_malloc(), | ||
non puo' essere invocata free() per liberare la memoria, per cui si | ||
utilizza' un semplice puntatore a NULL, che peraltro dovrebbe essere una | ||
buona regola dopo la deallocazione della memoria. */ | ||
ptr = NULL; | ||
|
||
// xmalloc() testing function | ||
char *ptr2; | ||
ptr2 = xmalloc(size); | ||
strncpy(ptr2, title, strlen(title)+1); | ||
printf("%s\n", ptr2); | ||
xfree(ptr2); | ||
char *ptr; | ||
ptr = malloc_two(size); | ||
strncpy(ptr, title, strlen(title)+1); | ||
printf("%s\n", ptr); | ||
free_two(ptr); | ||
|
||
return(EXIT_SUCCESS); | ||
} |