473,473 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Array of pointers? And other issues...

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
4 1347
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
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
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
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 (40°39.22'N, 111°50.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Kai Jaensch | last post by:
Hello, i am an newbie and i have to to solve this problem as fast as i can. But at this time i don´t have a lot of success. Can anybody help me (and understand my english :-))? I have a...
4
by: Ney André de Mello Zunino | last post by:
Hello. I have a question regarding arrays when used as arguments in function calls. In a situation like the following: void foo(int array) { // ... }
22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
3
by: kohvirus | last post by:
Hello everyone. I'm currently trying to write a program with an array but for the life of me I can't seem to get the right syntax for the array. Here is the program as follows: #include...
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
17
by: kleary00 | last post by:
Hi, I am writing a function that needs to return an array of strings and I am having some trouble getting it right. I need some help. Here is what I consider an array of 100 strings: char...
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.