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: -
if ( ! (is the pointer malloc'd)) {
-
pointer = malloc(.....);
-
}
-
so how can i check that ,is it possible....?
I searched the google and found no answer...
Thank you for your help....
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: -
if ( ! (is the pointer malloc'd)) {
-
pointer = malloc(.....);
-
}
-
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.
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.
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???
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.
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: -
if ( ! (is the pointer malloc'd)) {
-
pointer = malloc(.....);
-
}
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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;...
|
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...
|
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...
|
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...
|
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++){
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| |