A program was written by my friend and fellow software engineer Thomas to demonstrate how to use memory allocation using C programming language. Thank you Tom for sharing your code to us.
I am currently accepting programming work inventory system, enrollment system, accounting system, payroll system, information system, website design and development using WordPress, IT projects, school and application development, programming projects, thesis, and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details. If you want to advertise on my website kindly contact me also in my email address also. Thank you.
My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.My mobile number here in the Philippines is 09173084360. My telephone number at home here in Bacolod City, Negros Occidental Philippines is +63 (034) 4335675.
Here in Bacolod City, Negros Occidental I also accepting computer repair, web development using WordPress, Computer Networking, and Arduino Project development at a very affordable price. My personal website is http://www.jakerpomperada.com
Program Listing
#define DEBUG
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
void* alloc_mem(size_t size, const char *file, unsigned line)
{
assert(size > 0);
assert(file != NULL);
void* res = malloc(size);
if (res == NULL)
fprintf(stderr, "malloc failed at %s(%u)\n",file, line);
else {
fprintf(stderr, "alloc\t%u\t%u\t%s\t%u\n", (unsigned int)res,
size,file, line);
memset(res, 0xCC, size);
}
return res;
}
void free_mem(void *p, const char *file, unsigned line)
{
assert(p != NULL);
fprintf(stderr, "free\t%u\t?\t%s\t%u\n", (unsigned int)p, file, line);
}
#ifdef DEBUG
#define malloc(size) alloc_mem(size, __FILE__, __LINE__)
#define free(p) free_mem(p, __FILE__, __LINE__);
#endif
int f()
{
int *num = malloc(sizeof(int *));
return *num;
}
int main()
{
int *ip = malloc(sizeof(int *));
if (ip != NULL)
{
*ip = 11;
printf("*ip = %d\n", *ip);
}
printf("Num = %d\n", f());
free(ip);
//system("pause");
return EXIT_SUCCESS;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
void* alloc_mem(size_t size, const char *file, unsigned line)
{
assert(size > 0);
assert(file != NULL);
void* res = malloc(size);
if (res == NULL)
fprintf(stderr, "malloc failed at %s(%u)\n",file, line);
else {
fprintf(stderr, "alloc\t%u\t%u\t%s\t%u\n", (unsigned int)res,
size,file, line);
memset(res, 0xCC, size);
}
return res;
}
void free_mem(void *p, const char *file, unsigned line)
{
assert(p != NULL);
fprintf(stderr, "free\t%u\t?\t%s\t%u\n", (unsigned int)p, file, line);
}
#ifdef DEBUG
#define malloc(size) alloc_mem(size, __FILE__, __LINE__)
#define free(p) free_mem(p, __FILE__, __LINE__);
#endif
int f()
{
int *num = malloc(sizeof(int *));
return *num;
}
int main()
{
int *ip = malloc(sizeof(int *));
if (ip != NULL)
{
*ip = 11;
printf("*ip = %d\n", *ip);
}
printf("Num = %d\n", f());
free(ip);
//system("pause");
return EXIT_SUCCESS;
No comments:
Post a Comment