473,836 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Test if pointer points to allocated memory

Is there anyway to test if a pointer points to allocated memory or
not?
For example if I have a pointer such as char *p is there a standard
way to test whether an assignment such as the following has been
applied?
p = (char *) malloc(sizeof(c har) * n);
Nov 14 '05
34 33175
On 21 Jan 2004 08:25:57 -0800,
Andrew <an***********@ ca.com> wrote
in Msg. <65************ **************@ posting.google. com>
Is there anyway to test if a pointer points to allocated memory or
not?
For example if I have a pointer such as char *p is there a standard
way to test whether an assignment such as the following has been
applied?
p = (char *) malloc(sizeof(c har) * n);


I'm entering this thread a bit late, and therefor it's quite likely that
someone already made this sugegstion:

Why don't you initialize the pointer variable in question as NULL, and
test if it is non-NULL when in doubt? If you want to re-use the variable,
you must set it to NULL after freeing it. Of course this method has the
obvious limitations, but I use it all the time and am very satisfied.

I'm sure you've already been instructed to use

p = malloc(n * sizeof *p)

instead of doing it the way quoted above. The cast hides a possible bug,
and sizeof(char) is just a complicated way of writing 1. The generally
accepted method works with all pointer types, discloses failure to include
stdlib.h, and is less verbose.

--Daniel
--
"With me is nothing wrong! And with you?" (from r.a.m.p)
Nov 14 '05 #31
Daniel Haude wrote:
On 21 Jan 2004 08:25:57 -0800,
Andrew <an***********@ ca.com> wrote
in Msg. <65************ **************@ posting.google. com>
Is there anyway to test if a pointer points to allocated memory or
not?
For example if I have a pointer such as char *p is there a standard
way to test whether an assignment such as the following has been
applied?
p = (char *) malloc(sizeof(c har) * n);

I'm entering this thread a bit late, and therefor it's quite likely that
someone already made this sugegstion:

Why don't you initialize the pointer variable in question as NULL, and
test if it is non-NULL when in doubt? If you want to re-use the variable,
you must set it to NULL after freeing it. Of course this method has the
obvious limitations, but I use it all the time and am very satisfied.

[ snip ]

Limitations indeed. If you know that you are careful enough to NULL a
pointer after freeing it, then what is the point of later checking it
for NULL? What will we do if it is not NULL? Error out?

On most modern 32-bit architectures a pointer value has 4 billion
possibilities, NULL and all the others. The C programmer can examine the
pointer value to determine whether it is NULL or not. Beyond that, its
validity is not knowable.

--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Nov 14 '05 #32
On Sun, 25 Jan 2004 12:41:39 -0500,
Joe Wright <jo********@com cast.net> wrote
in Msg. <PL************ ********@comcas t.com>
Limitations indeed. If you know that you are careful enough to NULL a
pointer after freeing it, then what is the point of later checking it
for NULL? What will we do if it is not NULL? Error out?


That depends on whether we expect the pointer to be pointing at useable
storage area or not.

--Daniel

--
"With me is nothing wrong! And with you?" (from r.a.m.p)
Nov 14 '05 #33
Daniel Haude wrote:

On Sun, 25 Jan 2004 12:41:39 -0500,
Joe Wright <jo********@com cast.net> wrote
in Msg. <PL************ ********@comcas t.com>
Limitations indeed. If you know that you are careful enough to NULL a
pointer after freeing it, then what is the point of later checking it
for NULL? What will we do if it is not NULL? Error out?


That depends on whether we expect the pointer to be pointing at useable
storage area or not.

It doesn't matter what you expect. NULL does not point to useable
storage. You can't tell whether any other pointer value does. Think
about it.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #34
On Mon, 26 Jan 2004 00:19:04 GMT,
Joe Wright <jo********@ear thlink.net> wrote
in Msg. <40**********@e arthlink.net>
It doesn't matter what you expect. NULL does not point to useable
storage. You can't tell whether any other pointer value does. Think
about it.


Don't worry, I already know all about it. If you think that I think that
any non-NULL pointer points to allocated memory, you're mistaken. Remember
that I pointed out the "obvious limitations" of this simple method right
at the beginning.

If one interprets the OP literally as a request for a method that, when
fed ANY pointer, can tell if it points to allocated storage, then of
course it's not sufficient to just test against NULL. But then such a
method would be at best of academic interest, but rather useless for
practical purposes (unless you're wrizing a debugger).

--Daniel

--
"With me is nothing wrong! And with you?" (from r.a.m.p)
Nov 14 '05 #35

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3616
by: Tony Johansson | last post by:
Hello Experts!! When you instansiate varaibles(object) you can do so in four different scops which are. Within a block which is called Local or block scope . Within a function which is called function scope. Whithin a class which is called class scope. Outside a function which is called global or file scope. Now to my question when you allocate memory dynamically you can't say
3
6902
by: mandark_br | last post by:
I have a function that returns a int pointer to a allocated memory. How I know the size of this allocated memory? Compiler = gcc Ex.: int main(void) {
3
2545
by: Santh | last post by:
Hi, I have a bulk of data available in safearray pointer. I want to copy that data to another pointer. I tried using malloc for new variable and memcopy. But due to virtual memory restrictions the allocation failed. This is because the system memory can not allocate another volume of memory. My requirement is , without allocating the new memory, how can I copy the data from safe array to my variable. Since after copying I dont need the...
19
2376
by: pinkfloydhomer | last post by:
Please read the example below. I'm sorry I couldn't make it smaller, but it is pretty simple. When client code is calling newThingy(), it is similar to malloc: the client gets a pointer to a chunk of memory that it can use for it's own purposes. But on the "library" side, I want to be able to do some book-keeping for each memory block that I hand out. I want some "hidden" meta-data (that I keep in struct Container) to be associated...
74
4710
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique seems to be setting a NULL pointer to the end of the list, and here we know that the allocated memory has been exhausted. All good. When this is a pointer to another type, say int, I could have a variable that records how much memory is being...
26
3070
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
0
9810
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, 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...
0
9656
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10821
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, 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...
0
10527
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 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...
1
10575
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10241
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 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...
1
7774
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 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...
0
6975
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();...
2
4001
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.