473,396 Members | 2,109 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.

checking if pointer is NULL allowed?

atv
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 some instances where
i did just that and printf reported something like : (null).

I'm asking because i read somewhere that it is not valid. I thought it
simply wasn't valid to dereference
a pointer who is _not_ set to point to anything.

But i can check if it is NULL or not right ? ( i hope ) :-)

Rgds,
-alef

Jan 24 '07
51 4150
On Feb 2, 12:10 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
tedu wrote On 02/02/07 13:28,:
On Jan 25, 4:03 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
>In article <1169768146.763494.270...@a75g2000cwd.googlegroups .com>,
>tedu <t...@zeitbombe.orgwrote:
>>>>It is common practice in many shops I've worked at to have a function or
macro used in debugging routines that can take a pointer and return the
pointer or "null", depending on if the pointer is valid to dereference
or not.
>>>Maybe, but that's not possible in standard C.
>>why not?
>You can tell whether it's null (which I suspect is what was meant) but
you can't check that it's "valid to dereference" - it might be a
free()ed pointer for example.
[little slow noticing the followup.] but one can keep a list of freed
pointers (or conversely, a list of all allocated pointers).
In isgood(p), what if p points to a perfectly valid
object that wasn't obtained from malloc()?
Or, what if p points "into" rather than "at" a block
of allocated memory?
good points. i was focusing too heavily on "can this go to free?"
perhaps. i generally try to avoid too many 'pointers into objects'
running loose, as they frequently end up in unexpected places. so in
that sense, i would count them as bad.
Or, what if p points to a block of memory that has
been freed and then reallocated?
it goes back in the list. true, it won't find stale pointers in that
case.

Feb 2 '07 #51
On Feb 2, 11:16 am, "Harald van Dijk" <true...@gmail.comwrote:
tedu wrote:
[little slow noticing the followup.] but one can keep a list of freed
pointers (or conversely, a list of all allocated pointers).

In standard C, you're not even allowed to compare a freed pointer for
equality to another pointer.
right. i haven't had too many problems with [violating] that rule,
but it's a rule nonetheless.

Feb 2 '07 #52

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

Similar topics

30
by: Michael B Allen | last post by:
I have a general purpose library that has a lot of checks for bad input parameters like: void * linkedlist_get(struct linkedlist *l, unsigned int idx) { if (l == NULL) { errno = EINVAL;...
3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...

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.