Skip to contentSkip to conversation

"_ENTRY" is both a struct and a typedef?

U-Boot Robert P. J. Day 3 participants 3 messages Sep 8, 2016 – Sep 9, 2016

lore
Share & tools
lore
#0 lore raw

from lib/hashtable.c:

  typedef struct _ENTRY {
        int used;
        ENTRY entry;
  } _ENTRY;

ok, that's just kind of creepy ... defining a typedef over top of a struct of the same name. does anyone else find that strange?

rday

Signature · 7 lines
========================================================================
Robert P. J. Day                                 Ottawa, Ontario, CANADA
                        http://crashcourse.ca

Twitter:                                       http://twitter.com/rpjday
LinkedIn:                               http://ca.linkedin.com/in/rpjday
========================================================================
#1 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Robert" == Robert P J Day <[email protected]> writes:

Robert> from lib/hashtable.c:

Robert> typedef struct _ENTRY { int used; ENTRY entry; } _ENTRY;

Robert> ok, that's just kind of creepy ... defining a typedef over top Robert> of a struct of the same name. does anyone else find that Robert> strange?

It's standard practice in some C styles. Personally I'd delete the struct tag, as the typedef should be used everywhere instead.

Peter C

Signature · 2 lines
Dr Peter Chubb         Tel: +61 2 9490 5852      http://ts.data61.csiro.au/
Trustworthy Systems Group                           Data61 (formerly NICTA)
#2 lore raw
Quoted 4 lines · <[email protected]> writes:
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Robert" == Robert P J Day <[email protected]> writes:

Robert> from lib/hashtable.c:

Quoted 8 lines

Robert> typedef struct _ENTRY { int used; ENTRY entry; } _ENTRY;

Robert> ok, that's just kind of creepy ... defining a typedef over top Robert> of a struct of the same name. does anyone else find that Robert> strange?

It's standard practice in some C styles. Personally I'd delete the struct tag, as the typedef should be used everywhere instead.

I'd rather drop the typedef. The struct/typedef is only referenced by name in two places.

First in include/search.h:

/* Opaque type for internal use. */
struct _ENTRY;
[...]
/* Data type for reentrant functions. */

struct hsearch_data {
	struct _ENTRY *table;

It is standard practice to use "struct foo" in such constructs.

The second use is further down in lib/hashtable.c:

	htab->table = (_ENTRY *) calloc(htab->size + 1, sizeof(_ENTRY));

This line is terrible for two reasons:
1. Type-casting the return value of calloc is always wrong.
2. It is safer to use sizeof(*htab->table) instead of an explicit type.

If someone *really* wants to "fix" this, the proper thing is to drop the typedef and rewrite the calloc line to not explicitly mention the type.

Then again, this is code borrowed from uclibc/glibc, so perhaps it's best to simply leave it alone.

Signature · 1 line
M?ns Rullg?rd