confused

first i apologise, this is not the forum for this kind of questions, but i
bumped into it wgile writing a driver, hence i ask.

#include<stdio.h> #define TOTAL_ELEMENTS (sizeof(array) /
sizeof(array[0])) int array = {23,34,12,17,204,99,16};
int main()
{
int d;
for(d*=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf(“%d\n”,array[d
+*1]);
return 0;
}

y is the prog not going into the for loop? is it due to signed and
unsigned related isssues?</stdio.h>

I think the problem in TOTAL_ELEMENTS. If you use the array of constants why
don’t you use array of constant size - int array[7]…?

Andrey Gunko

soft Xpansion Ukraine Ltd.

Programmer

Powered by eKnow-how

Artjoma St. 118B … 83048 Donetsk … Tel/Fax: +38 062 3818874 …
Internet: [http:</http:> www.soft-xpansion.com]


From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Bedanto
Sent: Wednesday, December 13, 2006 1:12 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] confused

first i apologise, this is not the forum for this kind of questions, but i
bumped into it wgile writing a driver, hence i ask.

#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof
(array) / sizeof(
array[0]))
int array = {23,34,
12,17,204,99,16};

int main()

{

int
d;

for(d=-1;d
<= (TOTAL_ELEMENTS-2);d++)

printf(“%d\n”,array[
d+1]);

return 0;

}

y is the prog not going into the for loop? is it due to signed and unsigned
related isssues?

— Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256 To unsubscribe, visit the List
Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer</stdio.h>

Bedanto writes:
> first i apologise, this is not the forum for this kind of questions, but i
> bumped into it wgile writing a driver, hence i ask.
>
>
> #include<stdio.h> #define TOTAL_ELEMENTS (sizeof(array) /
> sizeof(array[0])) int array = {23,34,12,17,204,99,16};
> int main()
> {
> int d;
> for(d*=-1;d <= (TOTAL_ELEMENTS-2);d++)
> printf(“%d\n”,array[d
+*1]);
> return 0;
> }
>
>
>
>
>
> y is the prog not going into the for loop? is it due to signed and
> unsigned related isssues?

Yes, d is promoted to unsigned. Do

for(d=-1;d <= (int)(TOTAL_ELEMENTS-2);d++)
printf(“%d\n”,array[d+1]);

instead. Looking at the assembly output (with optimizations turned off),
and reading C language standard might be useful in general. :slight_smile:

Nikita.</stdio.h>

it looks like a signed unsigned problem!

As others have pointed out, referring to K&R (as this looks like basic C)
section A6.5 says if one operand is unsigned int the other is promoted to
unsigned int.

However, most compilers will warn about comparing signed and unsigned, so
the 3 main lessons here are

(a) keep all compiler warnings turned on
(b) read them
(c) don’t write confusing code - it may look clever, but not even you will
understand it 6 months later.

The last point has always seemed to me to be the most important and least
learned lesson :=) - Mike

(BTW did anyone spot the lack of comments? Any code where the comments are
less than half the text is automatically suspect)

----- Original Message -----
From: Bedanto
To: Windows System Software Devs Interest List
Sent: Wednesday, December 13, 2006 11:11 AM
Subject: [ntdev] confused

first i apologise, this is not the forum for this kind of questions, but i
bumped into it wgile writing a driver, hence i ask.

#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof
(array) / sizeof(
array[0]))
int array = {23,34,
12,17,204,99,16};
int main() { int
d;
for(d=-1;d
<= (TOTAL_ELEMENTS-2);d++)
printf(“%d\n”,array[
d+1]);
return 0; }

y is the prog not going into the for loop? is it due to signed and unsigned
related isssues?

— Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256 To unsubscribe, visit the List
Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer</stdio.h>

“Gunko Andrey” wrote in message news:xxxxx@ntdev…
>I think the problem in TOTAL_ELEMENTS. If you use the array of constants
>why
> don’t you use array of constant size - int array[7]…?

Actually, using TOTAL_ELEMENTS is the right thing to do, since it means the
array size can be changed without worrying about changing 7 or some #define
to keep the walk the same. I know a lot of professional shops that require
coding like this since it removes bugs.


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
http://www.windrvr.com
Remove StopSpam from the email to reply

I agree that in general using of such macro is the right thing to do. But in
case of his code it haven’t any sense. It only makes a signed/unsigned
mistake.

Andrey Gunko
soft Xpansion Ukraine Ltd.
Programmer
Powered by eKnow-how
Artjoma St. 118B … 83048 Donetsk … Tel/Fax: +38 062 3818874 …
Internet: [www.soft-xpansion.com]

|-----Original Message-----
|From: xxxxx@lists.osr.com [mailto:bounce-272761-
|xxxxx@lists.osr.com] On Behalf Of Don Burn
|Sent: Wednesday, December 13, 2006 3:45 PM
|To: Windows System Software Devs Interest List
|Subject: Re:[ntdev] confused
|
|
|“Gunko Andrey” wrote in message news:xxxxx@ntdev…
|>I think the problem in TOTAL_ELEMENTS. If you use the array of constants
|>why
|> don’t you use array of constant size - int array[7]…?
|
|Actually, using TOTAL_ELEMENTS is the right thing to do, since it means the
|array size can be changed without worrying about changing 7 or some #define
|to keep the walk the same. I know a lot of professional shops that require
|coding like this since it removes bugs.
|
|
|–
|Don Burn (MVP, Windows DDK)
|Windows 2k/XP/2k3 Filesystem and Driver Consulting
|http://www.windrvr.com
|Remove StopSpam from the email to reply
|
|
|
|
|—
|Questions? First check the Kernel Driver FAQ at
|http://www.osronline.com/article.cfm?id=256
|
|To unsubscribe, visit the List Server section of OSR Online at
|http://www.osronline.com/page.cfm?name=ListServer

> Yes, d is promoted to unsigned. Do

Exactly.

sizeof() has type size_t, which is unsigned, and then the signed “d” is
compared to unsigned value. This causes “d” to be promoted, and initially “d”
is -1, so it is promoted to 0xffffffff unsigned and is >= any unsigned value.

DDK build env will spit warnings about comparing signed to unsigned values.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

I think the message here is that macros are there to help us, but they won’t
if they embed source code in an arbitrary and confusing way. This example
obscures meaning because the macro is supposed to help with one particular
array (named array) that has not even been declared when the macro is
defined. It doesn’t really mean anything to the reader when first
encountered and is not in any way general.

A more useful macro would be for example

#define ARRAY_LENGTH(anyarray) ((int)(sizeof(anyarray) /
sizeof(anyarray[0])))

then the line

for(d=-1;d <= ARRAY_LENGTH(array)-2;d++)

should both work and be clearish, though it’d be even better if rearranged
so the iterator could start at 0.

Also ARRAY_SIZE is reusable for any array.

I know this is expanding on a not very relevant theme but we’ve all got to
learn so if this helps, great. Now my target machine has rebooted I’ll get
back to work…

BTW anyone who really wants to see how to write good C code should browse

http://www.ioccc.org/main.html

  • Mike

----- Original Message -----
From: Gunko Andrey
To: Windows System Software Devs Interest List
Sent: Wednesday, December 13, 2006 1:58 PM
Subject: RE: [ntdev] confused

I agree that in general using of such macro is the right thing to do. But in
case of his code it haven’t any sense. It only makes a signed/unsigned
mistake.

Andrey Gunko
soft Xpansion Ukraine Ltd.
Programmer
Powered by eKnow-how
Artjoma St. 118B … 83048 Donetsk … Tel/Fax: +38 062 3818874 …
Internet: [www.soft-xpansion.com]

|-----Original Message-----
|From: xxxxx@lists.osr.com [mailto:bounce-272761-
|xxxxx@lists.osr.com] On Behalf Of Don Burn
|Sent: Wednesday, December 13, 2006 3:45 PM
|To: Windows System Software Devs Interest List
|Subject: Re:[ntdev] confused
|
|
|“Gunko Andrey” wrote in message news:xxxxx@ntdev…
|>I think the problem in TOTAL_ELEMENTS. If you use the array of constants
|>why
|> don’t you use array of constant size - int array[7]…?
|
|Actually, using TOTAL_ELEMENTS is the right thing to do, since it means the
|array size can be changed without worrying about changing 7 or some #define
|to keep the walk the same. I know a lot of professional shops that require
|coding like this since it removes bugs.
|
|
|–
|Don Burn (MVP, Windows DDK)
|Windows 2k/XP/2k3 Filesystem and Driver Consulting
|http://www.windrvr.com
|Remove StopSpam from the email to reply
|
|
|
|
|—
|Questions? First check the Kernel Driver FAQ at
|http://www.osronline.com/article.cfm?id=256
|
|To unsubscribe, visit the List Server section of OSR Online at
|http://www.osronline.com/page.cfm?name=ListServer


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

> I think the message here is that macros are there to help us, but they won’t

The OP’s issue has nothing to do with macros, it has something to do with
signed/unsigned comparisons and size_t being unsigned.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

But it does crucially have to do with macros. Because the macro obscured the
meaning the obvious wasn’t visible. Had a macro not been used, it would have
been quicker to spot. Therefore I suggest that the OP wasn’t particualrly
skilled with macros. We all know the right way to do it of course, so it
doesn’t do much harm to mention it to the OP. A good example of inadvertent
obfuscation, hence the link - Mike

----- Original Message -----
From: Maxim S. Shatskih
To: Windows System Software Devs Interest List
Sent: Wednesday, December 13, 2006 4:39 PM
Subject: Re: [ntdev] confused

I think the message here is that macros are there to help us, but they
won’t

The OP’s issue has nothing to do with macros, it has something to do with
signed/unsigned comparisons and size_t being unsigned.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Maxim S. Shatskih wrote:

> I think the message here is that macros are there to help us, but they won’t
>

The OP’s issue has nothing to do with macros, it has something to do with
signed/unsigned comparisons and size_t being unsigned.

That’s not quite right. The use of a macro allowed the OP to believe
that the right side of his comparison was a simple integer. It hid the
fact that he was comparing against an expression that happened to have
an unsigned result.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.