473,756 Members | 3,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

avoiding multiple free calls on dynamically allocated memory

4 New Member
How can we avoid/detect multiple free calls for a dynamically allocated memory.

Expand|Select|Wrap|Line Numbers
  1. void example() {
  2.   int *a;
  3.   a = (int *)malloc(10);
  4.   free(a);
  5.   free(a); //how can we detect/avoid multiple free calls
  6. }
Apr 24 '10 #1
2 2410
hype261
207 New Member
I don't believe there is a way to tell if a piece of memory has already been freed or not. It is up to the programmer to exercise good memory management.
Apr 24 '10 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Set the deleted pointer to 0.

Calling free() with a zero pointer is OK.

However, if you have another pointer containing the same address, then all bets are off unles tyou implement reference checking. Very hard in C but in C++ you can use a Handle class.

The best thing is to run a profiler on your code after you think its working.
Apr 25 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2362
by: Christian Seberino | last post by:
I wrote some C functions and successfully imported them into Python. All is apparently well but there is one question about the object returned by the C function that is bugging me... Take for example this last line of a C extension: return Py_BuildValue("s", my_string);
9
1475
by: Jim H | last post by:
In one of my functions I create a char string s, of dynamic allocated length. I want to free the memory before my function returns. Everywhere I find says free(s) is the way to do this, but I'm not so sure. How can the language know how much space to free? It looks to me as if this would just free the first char in the string, since an array is a pointer to it's first element.
9
2404
by: zerro | last post by:
Hello, I try to understand heap overflows (under Linux), but can not understand one thing with freeing memory allocated with malloc(). Here it comes: I have a program called 1.c: main() { char *a, *b, *c, *d, *e; a = malloc(8);
3
550
by: Kurt | last post by:
Hi all, I know there's been a lot of articles and posts because of this malloc and free combination and I've read quite a lot of them. Still I can't get my program to work: My Structure: typedef struct rule { char ip; struct rule *next;
40
2530
by: boris | last post by:
Hi! I'm seeking some answers about what seems to be a memory leak. I have a loop that looks much like this: double *largeArray = (double*) calloc(); for (...) { printf("iteration #...\n"); for (...) { double *foo = (double*) calloc();
86
4151
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory that was freed by realloc(), but the only way explicitly mentioned in the C89 standard to free memory via realloc() is to realloc() it down to 0 bytes. I had always assumed it would automatically free the previous memory, but is the behaviour...
19
3063
by: santosh | last post by:
Hi all, In the following program I allocate a block of pointers to type char, initialised to zero. I then point each of those pointers to a block of allocated memory of fixed size (33 bytes). A unique 'string' is then generated using itoa() and rand() for each block of memory. Finally using pointer-to-pointer of type char, I print the contents of the blocks of memory, i.e. the strings, using both printf() and manually, character by...
13
2477
by: a.zeevi | last post by:
free() multiple allocation error in C ==================================== Hi! I have written a program in C on PC with Windows 2000 in a Visual C environment. I have an error in freeing multiple allocation as follows: 1. I allocated an array of pointer. 2. I Read line by line from a text file. 3. I allocated memory for the read line.
33
2216
by: Reddy | last post by:
Hello, Can someone clarify this? char *p = (char *) malloc (100*sizeof(char)); p=p+50; free(p); Does free clear all the 100 bytes of memory or only 50 as it is pointing to 51st byte of memory now?
13
5082
by: Chad | last post by:
Excercise 6-5 Write a functon undef that will remove a name and defintion from the table maintained by lookup and install. Code: unsigned hash(char *s); void undef(char *s)
0
9872
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.