Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question on free() ing a hash table

Reply
Thread Tools

Question on free() ing a hash table

 
 
CBFalconer
Guest
Posts: n/a
 
      10-27-2007
Chad wrote:
>

.... snip ...
>
> if ((np->defn = strdup(defn)) == NULL)
> return NULL;
>
> For reasons that still elude me, every time I see this line of
> code, I keep thinking that memory ISN'T allocated for np->defn.


Why? The dependency is obviously on strdup, and the return of NULL
presumably means that it failed. (strdup is non-standard.)
Therefore passing this test means it succeeded.

A better form IMO would be:

if (np->defn = strdup(defn)) return NULL;
else {
/* whatever is required */
}

and you can omit the else if you wish. But this form is clearer in
the middle of a function, while the omitted else is better used at
the start of the function.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Charlie Gordon
Guest
Posts: n/a
 
      10-27-2007
"CBFalconer" <(E-Mail Removed)> a écrit dans le message de news:
http://www.velocityreviews.com/forums/(E-Mail Removed)...
> Chad wrote:
>>

> ... snip ...
>>
>> if ((np->defn = strdup(defn)) == NULL)
>> return NULL;
>>
>> For reasons that still elude me, every time I see this line of
>> code, I keep thinking that memory ISN'T allocated for np->defn.

>
> Why? The dependency is obviously on strdup, and the return of NULL
> presumably means that it failed. (strdup is non-standard.)
> Therefore passing this test means it succeeded.
>
> A better form IMO would be:
>
> if (np->defn = strdup(defn)) return NULL;
> else {
> /* whatever is required */
> }


Ugly style:
- assignment as a test expression: a classic cause of stupid bugs.
- if and then clauses on the same line.
- no symmetry between then and else clause.

> and you can omit the else if you wish. But this form is clearer in
> the middle of a function, while the omitted else is better used at
> the start of the function.


Actually, direct return of NULL at the start of the function is better
without an else clause and is OK because it is easy to assert correctness.
This form would be quite error prone in the middle of a large function,
where returning NULL without proper cleanup of locally allocated memory
and/or acquired ressources would go unnoticed precisely because the return
statement does not stand out on its own. Definitely avoid this style.

--
Chqrlie.


 
Reply With Quote
 
 
 
 
Peter Pichler
Guest
Posts: n/a
 
      10-28-2007
santosh wrote:

> In particular if 'name' and 'defn' are the only pointers that point to
> their storage, then you must free() them _before_ calling free() on np,
> or else you'll create a memory leak.


Your emphasis on before makes it look like the alternative is after (as
opposed to "not at all"). Doing that would be an undefined behaviour,
not just a memory leak.
 
Reply With Quote
 
gw7rib@aol.com
Guest
Posts: n/a
 
      11-08-2007
On 27 Oct, 16:17, CBFalconer <(E-Mail Removed)> wrote:
> Chad wrote:
>
> ... snip ...
>
> > if ((np->defn = strdup(defn)) == NULL)
> > return NULL;

>
> > For reasons that still elude me, every time I see this line of
> > code, I keep thinking that memory ISN'T allocated for np->defn.

>
> Why? The dependency is obviously on strdup, and the return of NULL
> presumably means that it failed. (strdup is non-standard.)
> Therefore passing this test means it succeeded.
>
> A better form IMO would be:
>
> if (np->defn = strdup(defn)) return NULL;
> else {
> /* whatever is required */
> }


Correct me if I'm wrong, but doesn't your version do the opposite of
the original one?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Hash#keys, Hash#values order question Ronald Fischer Ruby 0 08-23-2007 09:34 AM
Table/table rows/table data tag question? Rio HTML 4 11-05-2004 08:11 AM
standard library for hash table storage and hash algorithm Pieter Claassen C Programming 1 08-04-2004 03:11 AM



Advertisments