Its always a trouble for me to securely freeing allocated memory. By Securely I mean free all the memory. So, I have developed a simple method that might help me to do so. I didnt use it in any project, but I will, anyway,
here is the two file: secmem.h -
#ifndef secmem_h
-
#define secmem_h 1
-
#include<stdlib.h>
-
void * secureAlloc(size_t size,int Line, char *FileName);
-
short secureFree(void *ptr);
-
short freeAll();
-
#endif
-
-
secmem.c -
#include "secmem.h"
-
#include <stdio.h>
-
#include <string.h>
-
struct MemoryList
-
{
-
long MemoryAddress;
-
int line;
-
char fileName[20];
-
struct MemoryList *Next;
-
};
-
static struct MemoryList *Start,*Last;
-
-
void * secureAlloc(size_t size,int Line, char *FileName)
-
{
-
long newAddress,newAddress2;
-
newAddress=(long)malloc(size);
-
newAddress2=(long)malloc(sizeof(struct MemoryList));
-
if(Start==NULL)
-
{
-
Start=(struct MemoryList *)newAddress2;
-
Last=Start;
-
Last->MemoryAddress=newAddress;
-
Last->line=Line;
-
strcpy(Last->fileName,FileName);
-
}
-
else
-
{
-
Last->Next=(struct MemoryList *)newAddress2;
-
Last=(struct MemoryList *)newAddress2;
-
Last->MemoryAddress=newAddress;
-
Last->line=Line;
-
strcpy(Last->fileName,FileName);
-
}
-
return (void *)newAddress;
-
}
-
short secureFree(void *ptr)
-
{
-
struct MemoryList *Cont,*Prev;
-
long temp=(long)ptr;
-
if(ptr!=NULL)
-
{
-
free(ptr);
-
}
-
else
-
{
-
return 1;
-
}
-
Cont=Start;
-
Prev=Cont;
-
while(Cont)
-
{
-
if((long)Cont->MemoryAddress==temp)
-
{
-
if(Cont==Start)
-
{
-
Start=Cont->Next;
-
free(Cont);
-
if(Start==NULL)
-
{
-
Last=NULL;
-
}
-
return 0;
-
}
-
else
-
{
-
Prev->Next=Cont->Next;
-
free(Cont);
-
if(Prev->Next==NULL)
-
{
-
Last=Prev;
-
}
-
return 0;
-
}
-
}
-
else
-
{
-
Prev=Cont;
-
Cont=Cont->Next;
-
}
-
}
-
return 1;
-
}
-
-
short freeAll()
-
{
-
struct MemoryList *Cont,*Next;
-
int Count=0;
-
Cont=Start;
-
while(Cont)
-
{
-
Next=Cont->Next;
-
printf("File Name: %s, Line Number : %d\n",Cont->fileName,Cont->line);
-
free(Cont);
-
Cont=Next;
-
Count++;
-
}
-
printf("Total Freed : %d\n",Count);
-
}
-
Test file: test.c -
#include <stdio.h>
-
#include "secmem.h"
-
struct test_
-
{
-
char *a;
-
char *b;
-
char *c;
-
struct test_ *Next;
-
}*test;
-
-
int main()
-
{
-
test=secureAlloc(sizeof(struct test_),__LINE__,__FILE__);
-
test->a=(char*)secureAlloc(10,__LINE__,__FILE__);
-
secureFree(test);
-
-
freeAll();
-
return 0;
-
}
-
-
Now how it work,
in the test.c, I have called secureAlloc, including the line number and file name.
at the end of the program, I have called freeAll() function.
When It runs the free all function it also print all the line number and the file name from where the allocation has been requested.
This helps a lot in case of debugging and make sure all the allocated memory has been freed.
Final Steps, whenever you are sure enough that all memory has been freed then add some macro in common header file and remove the freeAll() function test.h -
-
#include <stdio.h>
-
#include <stdlib.h>
-
//#include "secmem.h"
-
#define secureAlloc(a,b,c) malloc(a)
-
#define secureFree(a) free(a)
-
-
struct test_
-
{
-
char *a;
-
char *b;
-
char *c;
-
struct test_ *Next;
-
}*test;
-
-
int main()
-
{
-
test=secureAlloc(sizeof(struct test_),__LINE__,__FILE__);
-
test->a=(char*)secureAlloc(10,__LINE__,__FILE__);
-
secureFree(test);
-
-
// freeAll();
-
return 0;
-
}
-
Hope, this will help
0 4213 Sign in to post your reply or Sign up for a free account.
Similar topics |
by: benevilent |
last post by:
Hey,
I'm trying to debug the memory allocation in an embedded use of the
Python interpreter.
The longer I leave my program running the more memory it consumes. The
total number of objects in the system is not increasing (many are being
allocated and deallocated).
Using mtrace I have established that the only memory which is not being
|
by: mosfets |
last post by:
Hi,
I'm having a little trouble figuring out the difference in terms of
memory allocation between:
class person_info;
class A {
private:
|
by: Andrew Au |
last post by:
Dear all,
I am trying to write a piece of software that use Object Oriented design
and implement it with C, I did the following
== In Object.h ==
typedef struct ObjectStructure* Object;
Object object_create();
int object_getIntegerAttribute(Object object);
|
by: Johan Tibell |
last post by:
I've written a piece of code that uses sockets a lot (I know that
sockets aren't portable C, this is not a question about sockets per
se). Much of my code ended up looking like this:
if (function(socket, args) == -1) {
perror("function");
exit(EXIT_FAILURE);
}
I feel that the ifs destroy the readability of my code. Would it be
|
by: farshid |
last post by:
I have written a long program with c, and am using dynamic memory allocation. This program is supposed to be run over and over (300 times) for a long simulation. But the program stops after 120 cycles due to memory leackage. I am not very expert in programing but it seems that my free() function does not do anything to my program. One example of...
| |
by: Ken |
last post by:
In C programming, I want to know in what situations we should use
static memory allocation instead of dynamic memory allocation. My
understanding is that static memory allocation like using array is
faster than malloc, but dynamic memory allocation is more flexible.
Please comment... thanks.
|
by: Peterwkc |
last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back.
By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to...
|
by: siddhu |
last post by:
Dear experts,
If I do free(p); memory pointed by p is freed and is available for
further allocations in the process.
But how does it decide about how much memory (size) has to to be freed
and make it available for further allocations?
Regards,
Siddharth
|
by: vivek |
last post by:
i have some doubts on dynamic memory allocation and stacks and heaps
where is the dynamic memory allocation used?
in function calls there are some counters like "i" in the below
function. Is this stored in stack. If yes whether it will be deleted
on exiting from the function.
is dynamic memory allocation needed for this purpose
|
by: gianx80 |
last post by:
Hi,
I'm studying the basis of C, C++ and Assembly languages at my university (I have two exams about these subjects,
for now) ... and I have a problem ^^.
I wrote a program in C (not so optimized, this isn't our present goal for my professors) that recieves in input an
integer (matrix size) and a double pointer to a square matrix and...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |