Name | Modified | Size | Downloads / Week |
---|---|---|---|
mem | 2013-05-09 | ||
README | 2013-05-09 | 2.5 kB | |
Totals: 2 Items | 2.5 kB | 0 |
This is a memory allocation library allocation is done through the expansion of the size of the heap the algorithm is used the first adaptation Library open source, you can be free to use and modify, but do not for malicious ways if you find bugs, please inform me Installation method: make install uninstall method: make remove Usage: #include <mem.h>, -lmcm attention: ALl out of bounds modify data's behavior will result in the operating function uncertain author: 颿æ³?(The English name: robin) There are four functions, respectively, as follows: void *mc_malloc(size_t size); this function's behavior is the same as the standard malloc, but mc_malloc(0) return NULL int mc_free(void *p); this function's behavior is the same as the standard function of free, do nothing with NULL mc_free, the successful release returns 0 Before release, or illegal pointer function returns -1 (must modify the contents of the memory to ensure that no cross-border) void *mc_calloc(size_t nobj, size_t size); this function's behavior is the same as the standard function of calloc, if nobj or the size is equal to 0, return NULL Memory allocated to ensure that every byte initialized to 0 (0 and '\ 0' does not always the same) void *mc_realloc(void *p, size_t size); this function's behavior is the same as the standard function of realloc (can shrink the memory) If the size is 0, release the memory of pointed p and then returns NULL, the memory returns NULL if p is NULL or p has been released if successfully invoked, the original pointer will not be in use (return pointer is probably the original pointer p) Example: /* * main.c */ #include <stdio.h> #include <string.h> #include <mem.h> int main(void) { char *p; char s[] = "1234"; char t[] = "123456789"; p = mc_malloc(sizeof(s)); strcpy(p, s); printf("s = %s\n", p); p = mc_realloc(p, sizeof(t)); strcpy(p, t); printf("t = %s\n", p); mc_free(p); return 0; } /* * makefile */ .PHONY:all all: cx objects := main.o cx: $(objects) $(CC) $(objects) -o $@ -lmcm CFLAGS := -c -g -std=c99 -W -O -D_POSIX_SOURCE main.o: main.c $(CC) -o $@ $(CFLAGS) main.c .PHONY:clean clean: $(RM) $(objects) You can enter the command of make, then. / Cx to view the results (remember to make clean to delete the unnecessary files)