Array of pointers? And other issues...

D

Daniel Rudy

Hello Group,

Consider the following code fragment:


#define PSM_TABLESIZE 2441 /* size of hash table (prime number) */
#define uint32 unsigned long int


typedef struct psmalloc_table_tag
{
void *p; /* pointer to data */
uint32 size; /* size of memory block */
uint32 flags; /* operational flags */
struct psmalloc_table_tag *next; /* pointer to next record */
} psmalloc_table_t;


typedef struct psmalloc_tag
{
uint32 valid; /* valid struct flag */
uint32 count; /* number of entries used */
uint32 slimit; /* allocation size limit */
uint32 tlimit; /* total allocation limit */
uint32 tsize; /* total size count register */
uint32 flags; /* flags to use if PSM_SEC_USEFLAGS */
int security; /* sets default security mode */
psmalloc_table_t *ht[PSM_TABLESIZE]; /* hash table */
} psmalloc_t;


/* computes hash value from pointer */
uint32 psm_hashkey(void *p)
{
uint32 hash;

151: hash = PSM_TABLESIZE * ((int)p * .618 % 1);
return(hash);
}

Gcc gives the following error about the above line: psmalloc.c:151:
error: invalid operands to binary %



/* **** HASH TABLE MANAGEMENT **** */

/* adds the specified entry to the hash table */
int psm_tableadd(psmalloc_t *state, void *ptr, uint32 size, uint32 flags)
{
uint32 hash;
psmalloc_table_t *p;

hash = psm_hashkey(ptr);

/* locate position in table and allocate memory for it */
185: if (state->ht[hash] == NULL)
{
/* table position empty */
188: state->ht[hash] = malloc(sizeof(psmalloc_table_t));

And here it gives errors as well:

psmalloc.c:185: error: invalid operands to binary ==
psmalloc.c:188: error: incompatible types in assignment


What I am trying to do is declare and array of pointers of type
psmalloc_table_t named ht in the structure. But when I try to reference
the pointers themselves, gcc keeps flagging it as an error. I did check
the book, and so far everything looks ok. I did look at the FAQ about
these problems and I didn't really find anything that applied to this
situation. What am I missing?

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
S

swengineer001

Hello Group,

Consider the following code fragment:

#define PSM_TABLESIZE 2441 /* size of hash table (prime number) */
#define uint32 unsigned long int

typedef struct psmalloc_table_tag
{
void *p; /* pointer to data */
uint32 size; /* size of memory block */
uint32 flags; /* operational flags */
struct psmalloc_table_tag *next; /* pointer to next record */
} psmalloc_table_t;

typedef struct psmalloc_tag
{
uint32 valid; /* valid struct flag */
uint32 count; /* number of entries used */
uint32 slimit; /* allocation size limit */
uint32 tlimit; /* total allocation limit */
uint32 tsize; /* total size count register */
uint32 flags; /* flags to use if PSM_SEC_USEFLAGS */
int security; /* sets default security mode */
psmalloc_table_t *ht[PSM_TABLESIZE]; /* hash table */
} psmalloc_t;

/* computes hash value from pointer */
uint32 psm_hashkey(void *p)
{
uint32 hash;

151: hash = PSM_TABLESIZE * ((int)p * .618 % 1);
return(hash);
}

Gcc gives the following error about the above line: psmalloc.c:151:
error: invalid operands to binary %

According to section 6.5.5 of the standard "The operands of the %
operator shall
have integer type."

Also, I think you want to be careful about the cast of the pointer to
an int as this is not portable. Again the following is from the
standard, section 6.3.2.3, "Any pointer type may be converted to an
integer type. Except as previously specified, the
result is implementation-defined. If the result cannot be represented
in the integer type,
the behavior is undefined. The result need not be in the range of
values of any integer
type."
 
D

Daniel Rudy

At about the time of 2/25/2007 8:11 PM, (e-mail address removed) stated
the following:
Hello Group,

Consider the following code fragment:

#define PSM_TABLESIZE 2441 /* size of hash table (prime number) */
#define uint32 unsigned long int

typedef struct psmalloc_table_tag
{
void *p; /* pointer to data */
uint32 size; /* size of memory block */
uint32 flags; /* operational flags */
struct psmalloc_table_tag *next; /* pointer to next record */
} psmalloc_table_t;

typedef struct psmalloc_tag
{
uint32 valid; /* valid struct flag */
uint32 count; /* number of entries used */
uint32 slimit; /* allocation size limit */
uint32 tlimit; /* total allocation limit */
uint32 tsize; /* total size count register */
uint32 flags; /* flags to use if PSM_SEC_USEFLAGS */
int security; /* sets default security mode */
psmalloc_table_t *ht[PSM_TABLESIZE]; /* hash table */
} psmalloc_t;

/* computes hash value from pointer */
uint32 psm_hashkey(void *p)
{
uint32 hash;

151: hash = PSM_TABLESIZE * ((int)p * .618 % 1);
return(hash);
}

Gcc gives the following error about the above line: psmalloc.c:151:
error: invalid operands to binary %

According to section 6.5.5 of the standard "The operands of the %
operator shall
have integer type."

And that is the problem....
Also, I think you want to be careful about the cast of the pointer to
an int as this is not portable. Again the following is from the
standard, section 6.3.2.3, "Any pointer type may be converted to an
integer type. Except as previously specified, the
result is implementation-defined. If the result cannot be represented
in the integer type,
the behavior is undefined. The result need not be in the range of
values of any integer
type."

I doubt that will be an issue in what I am doing, but I'll make a note
of it in the source in case of problems later on...like running on 64bit
machines.


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
D

Daniel Rudy

At about the time of 2/25/2007 7:35 PM, Daniel Rudy stated the following:
Hello Group,

Consider the following code fragment:


#define PSM_TABLESIZE 2441 /* size of hash table (prime number) */
#define uint32 unsigned long int


typedef struct psmalloc_table_tag
{
void *p; /* pointer to data */
uint32 size; /* size of memory block */
uint32 flags; /* operational flags */
struct psmalloc_table_tag *next; /* pointer to next record */
} psmalloc_table_t;


typedef struct psmalloc_tag
{
uint32 valid; /* valid struct flag */
uint32 count; /* number of entries used */
uint32 slimit; /* allocation size limit */
uint32 tlimit; /* total allocation limit */
uint32 tsize; /* total size count register */
uint32 flags; /* flags to use if PSM_SEC_USEFLAGS */
int security; /* sets default security mode */
psmalloc_table_t *ht[PSM_TABLESIZE]; /* hash table */
} psmalloc_t;

This is one of those senior moments that I get...seems to be happening
more and more frequently the older I get.

It just might work if I save the header file *AFTER* I make the change.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
C

Chris Torek

typedef struct psmalloc_table_tag
{ [some snippage]
struct psmalloc_table_tag *next; /* pointer to next record */
} psmalloc_table_t;

The rest of this has already been addressed.

I want to point out that this combination sequence, of declaring
a structure tag and then -- by putting "typedef" way at the front,
before the whole thing, and a variable declaration way at the back,
after the whole thing -- is a big mess. "Don't do that." If you
like typedefs, do this instead:

typedef struct psmalloc_table_tag psmalloc_table_t;
struct psmalloc_table_tag {
...
psmalloc_table_t *next;
};

That is, put the typedef *first*. Then you can *use* it inside
the structure. This also keeps the alias close to the "true name",
instead of wedging the typedef at one end, the entire contents in
the middle, and the alias at the end. (My own preference is to
avoid the typedef entirely, but tastes differ.)

For (much) more on this, see <http://web.torek.net/torek/c/types2.html>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top