473,396 Members | 1,996 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,396 software developers and data experts.

Null pointer check before calling free()?

I have a base class static character pointer d, which is called by 3 members in derived class in the following fashion

BaseClass::d = strdup(x);
//x is a character pointer whose value changes during execution.

Each time when this code is executed, character pointer d is overwritten and it cause memory leak. To prevent this memory leak, I want to call free(d) before strdup.
What will happen if free(d) is called when no memory is allocated to d(Initial case)?
How I can check if d is null pointer or not before calling free? Or is it safe to call free without this check?
Oct 13 '06 #1
1 11889
Banfa
9,065 Expert Mod 8TB
What will happen if free(d) is called when no memory is allocated to d(Initial case)?
How I can check if d is null pointer or not before calling free? Or is it safe to call free without this check?
If you call free(d) when d is not pointed at allocated memory you will get an exception and you program will crash.

Checking for a NULL pointer is easy

if (d == NULL)
{
}

but d will not automatically be NULL unless you set it to NULL somewhere.

It is not safe to free d without a check to see if it is actuall allocated so you code should be in the general case

Expand|Select|Wrap|Line Numbers
  1. if (d != NULL)
  2. {
  3.     free(d);
  4.     d = NULL;
  5. }
  6.  
The d = NULL; is a bit superflous if you are going to allocate more memory for it imediately but the point is that in the general case you need to set the pointer back to NULL after freeing it to make sure it isn't free'd again.
Oct 14 '06 #2

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

Similar topics

4
by: Matthew | last post by:
Hello, I have an executable that is calling a DLL I have built. The DLL requires two static libraries to run (genuine statics as opposed to the 'stub' .lib files that still require a DLL). In...
16
by: mike79 | last post by:
Hi all, I have a the following simple piece of code which has taken me hours to try and sort out the problem, but still unable to find what is wrong. void main( void ) { char (*string);...
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
51
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
15
by: jacob navia | last post by:
Problem You want to ensure that a pointer argument to a function is non-null. Solution int fn(double data); This means that the array (that is passed as a pointer)
51
by: atv | last post by:
Hi, Just to check, if i set a pointer explicitly to NULL, i'm not allowed to dereference it? Why is that, it's not like it's pointing to any garbage right? Why else set it to NULL. I can remember...
76
by: valentin tihomirov | last post by:
As explained in "Using pointers vs. references" http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca , the...
27
by: rocco.rossi | last post by:
I've been trying to write a function capable of checking if a pointer value is set to NULL, and if it isn't, of deallocating and setting it's value to NULL regardless of the pointer's type. I...
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
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,...
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
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,...

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.