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 3747 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 finally got
it working. Now here's the problem:
I am able to get the 2D array dynamically allocated correctly as long
as I am doing it "in-line" (i.e. without invoking any function). The
moment I try to do it in another function, I get a...
|
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 about using !ptr or ptr in test conditions (of loops, if or ?:)
?
For example:
|
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 printed
will it be all 0's or 0x12345678
int main(void)
{
char *ptr;
ptr = 0;
|
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
which is part of the "atoms" structure that is passed into the function
from the outside. I have not yet found where it is allocated but I'm
reasonably sure from other chunks of this code that it was by:
|
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 advance.
Erik
|
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 try my own small program. The problem is, I seem to be having trouble with memory, i.e. sometimes my program will work and display the correct output, and sometimes it will not and display garbage (in a printf call) so i assume i have been using...
|
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 "right" way to go
about calling malloc instead is to dereference a pointer; apparently even
if said pointer is undefined.
E.g.
ptr = malloc(sizeof(struct some_struct)); = wrong
ptr = malloc(*ptr); = right
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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();...
|
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...
|
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
| |
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...
| |