473,474 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is it possible to check if a pointer has been malloc'd in C?

14 New Member
Hello again,

i am looking for a way to check if a pointer has been malloc'd.
I want to use this in a way, if the pointer has not been malloc'd to be malloc'd like that:
Expand|Select|Wrap|Line Numbers
  1.     if ( ! (is the pointer malloc'd))   {
  2.         pointer = malloc(.....);
  3.     }
  4.  
so how can i check that ,is it possible....?
I searched the google and found no answer...

Thank you for your help....
Jan 18 '08 #1
5 3740
Savage
1,764 Recognized Expert Top Contributor
Hello again,

i am looking for a way to check if a pointer has been malloc'd.
I want to use this in a way, if the pointer has not been malloc'd to be malloc'd like that:
Expand|Select|Wrap|Line Numbers
  1.     if ( ! (is the pointer malloc'd))   {
  2.         pointer = malloc(.....);
  3.     }
  4.  
so how can i check that ,is it possible....?
I searched the google and found no answer...

Thank you for your help....
It is.
If malloc fails,it sets the pointer to NULL.
Jan 18 '08 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Which is to say that unless you initialize all of your pointers to NULL, you can't tell if they've been malloc'd or not.
Jan 18 '08 #3
dreiko466
14 New Member
Thank you very much, i didn't know that,

but if it returns NULL the pointer will lost its previus value so i will have a memory leak, is any way i can check that without this memory leak???
Jan 18 '08 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
but if it returns NULL the pointer will lost its previus value so i will have a memory leak, is any way i can check that without this memory leak???
Nope.

If your pointer has been malloc'd, then you need to call free() on it before you use it in another malloc. Be aware, thart free() will crash your program if the pointer is NULL. Be sure your pointer is not NULL.

You have to assume responsibility here. For all this to work, there can be no non-NULL pointers that have not been malloc'd. That after you free() a pointer, you set it tro NULL. That you never free() a pointer unless you are positive there is not another copy of it somwhere else in the program.

Errors in memory management is one opf the big reasons C++ has replaced C.
Jan 20 '08 #5
send2aansari
7 New Member
Hello again,

i am looking for a way to check if a pointer has been malloc'd.
I want to use this in a way, if the pointer has not been malloc'd to be malloc'd like that:
Expand|Select|Wrap|Line Numbers
  1.     if ( ! (is the pointer malloc'd))   {
  2.         pointer = malloc(.....);
  3.     }
  4.  
so how can i check that ,is it possible....?
I searched the google and found no answer...

Thank you for your help....



There may be several cases . But when you are goin to allocate memory on heap then be carefully that whole responsiblity is your. How to manage it.

Let ptr is pointer which we have to chech.

case 1: If it's NULL, then no confusion just allocate memory or as you wish.

Case 2: If it's not NULL, then two cases it may be.
Case a: If it has some Valid Address then call Free
Case b: If it has some Invalid Address then make it NULL.

Remember one thing After calling function free() just make pointer to NULL manually, then you will get invalid address.


Asadullah Ansari
Jan 22 '08 #6

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

Similar topics

10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
10
by: masood.iqbal | last post by:
The code example below shows the dynamic allocation of a 2D array. I must admit that it took quite a while for me to get there (I already have another posting to that effect), but I am glad that I...
9
by: G Fernandes | last post by:
Hello fellow C-goers, Comparisons of pointer variables to 0 are automatically converted to comparisons to NULL (which can be represented at the bit level but something non-zero). But how...
42
by: junky_fellow | last post by:
Consider an implementation that doesn't use all bits 0 to represent a NULL pointer. Let the NULL pointer is represented by 0x12345678. On such an implementation, if the value of NULL pointer is...
48
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
3
by: David Mathog | last post by:
This one is driving me slightly batty. The code in question is buried deep in somebody else's massive package but it boils down to this, two pointers are declared, the first is: char **resname...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
98
by: Micheal Smith | last post by:
I recently read an article containing numerous gripes about common C practices. One of them contained a gripe about the use of the sizeof operator as an argument to malloc calls. The supposed...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
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...

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.