473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

avoiding multiple free calls on dynamically allocated memory

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 2390
hype261
207 100+
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 Expert Mod 8TB
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
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...
9
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...
9
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() {...
3
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:...
40
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");...
86
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...
19
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...
13
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...
33
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...
13
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.