By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,404 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,404 IT Pros & Developers. It's quick & easy.

Array of pointers? And other issues...

P: n/a
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
Feb 26 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Feb 25, 10:35 pm, Daniel Rudy <spamt...@spamthis.netwrote:
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."
Feb 26 '07 #2

P: n/a
At about the time of 2/25/2007 8:11 PM, sw***********@gmail.com stated
the following:
On Feb 25, 10:35 pm, Daniel Rudy <spamt...@spamthis.netwrote:
>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
Feb 26 '07 #3

P: n/a
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
Feb 26 '07 #4

P: n/a
In article <yv****************@newssvr23.news.prodigy.net>
Daniel Rudy <ZGNydWR5QHBhY2JlbGwubmV0wrote:
>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>.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 27 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.