How to traverse the items in an array ?

L

llothar

I need a function that iterates over an array, like this one:

int iterate_array (void* mem, size_t size, callback func)
{
char* p = (char*)mem;
while(size-- > 0) {
func(p);
p += size;
}
}

but i'm not sure how to get the value of 'size'. First of course was
the idea

int a[10];
iterate_array(a, sizeof(int), intfunc)

but what if the data is 6 byte? Then its up to the compiler (or
pragmas) it might be aligned to 8. How can i find this alignment. Will
a (sizeof(type[2])/2) expression always return the correct size
including alignment so that the generic iterator works ?
 
R

Richard Heathfield

llothar said:
I need a function that iterates over an array, like this one:

int iterate_array (void* mem, size_t size, callback func)
{
char* p = (char*)mem;

The cast is not required.
while(size-- > 0) {

No, that won't do at all. It does not express your intent. See below:
func(p);
p += size;
}
}

but i'm not sure how to get the value of 'size'. First of course was
the idea

int a[10];
iterate_array(a, sizeof(int), intfunc)

Why not sizeof a[0] ?
but what if the data is 6 byte?

So what?
Then its up to the compiler (or pragmas) it might be aligned to 8.

No, because array elements are contiguous. Here, I express your intent
more clearly and correctly:

typedef void (*callback)(void *);

int iterate_array(void *arr, size_t nmemb, size_t size, callback func)
{
unsigned char *p = arr;
while(nmemb--)
{
func(p);
p += size;
}
return 0; /* presumably you had a reason for making this function
return int, so fix this up when you're ready. */
}

Usage:

int a[10] = {0};

iterate_array(a, sizeof a / sizeof a[0], sizeof a[0], intfunc);

Take a close look at qsort and bsearch. You may find it educational.
 
L

llothar

No, that won't do at all. It does not express your intent. See below:

Sorry this was a stupid error, i had no code snippet so i typed it
here without thinking to much.
(I'm not such a bad programmer).
No, because array elements are contiguous. Here, I express your intent
more clearly and correctly:

Really? Okay this would be an easy answer to the question.
And is this always true even in the different c compiler
implementations, regardless of any used pragma settings?
 
R

Richard Heathfield

llothar said:

And is [the contiguity of array members] always true even in the
different c compiler implementations, regardless of any used
pragma settings?

It is true for a conforming C implementation. It's entirely possible to
stop an implementation from being conforming by using pragmas. For
example, an implementation might supply support for a pragma whose
meaning is "treat all + as - and all - as +". There's no accounting for
pragmas.
 
R

Richard Tobin

No, because array elements are contiguous.
[/QUOTE]
Really? Okay this would be an easy answer to the question.
And is this always true even in the different c compiler
implementations, regardless of any used pragma settings?

It's true for all conforming compilers, but if you run them in a
non-conforming mode anything might be true. And pragmas can have any
effect.

I have used a computer that used 5-byte floats, but there wouldn't be
much point in that if they had to be padded to 8 bytes in arrays. And
even if for some reason it was desirable, it could be done by having
8-byte floats and only using 5 of the bytes in arithmetic operations.

-- Richard
 
L

llothar

Really? Okay this would be an easy answer to the question.
And is this always true even in the different c compiler
implementations, regardless of any used pragma settings?

Sorry to much bad home made alcohol yesterday.

I meant the difference between a size of a single item and an array
item. But with "sizeof a[0] " it should be clear to get this size (in
the caller not in the callee. )

I would guess there is an pragma, attribute that allows packing of the
items inside the array or not.

But i see that this is a case i can't catch anyway without having
access to the original array declaration.
 
E

etherzhhb

iterate_array(a, sizeof a / sizeof a[0], sizeof a[0], intfunc);
what about when a is a pointer:
int b[10] = {0};
int *a = b;

"sizeof a" could get nothing but the size of that pointer.
 
R

Richard Heathfield

(e-mail address removed) said:
iterate_array(a, sizeof a / sizeof a[0], sizeof a[0], intfunc);
what about when a is a pointer:

What about when a is an Edsel?

Check the subject line again. He asked about arrays, not pointers.
 

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,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top