Skip to contentSkip to conversation

hw/i386: Fix compiler warning when CONFIG_IDE_ISA is disabled

PATCH In review Thomas Huth 6 participants 12 messages Nov 15, 2019 – Nov 18, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/i386: Fix compiler warning when CONFIG_IDE_ISA is disabled

When CONFIG_IDE_ISA is disabled, compilation currently fails:

hw/i386/pc_piix.c: In function ‘pc_init1’:
hw/i386/pc_piix.c:81:9: error: unused variable ‘i’ [-Werror=unused-variable]

Move the variable declaration to the right code block to avoid this problem.

Fixes 4501d317b50e ("hw/i386/pc: Extract pc_i8259_create()")
---
 hw/i386/pc_piix.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/i386/pc_piix.c +1 -2
index 2aefa3b8df..d187db761c 100644
@@ -78,7 +78,6 @@ static void pc_init1(MachineState *machine,
     X86MachineState *x86ms = X86_MACHINE(machine);
     MemoryRegion *system_memory = get_system_memory();
     MemoryRegion *system_io = get_system_io();
-    int i;
     PCIBus *pci_bus;
     ISABus *isa_bus;
     PCII440FXState *i440fx_state;
@@ -253,7 +252,7 @@ static void pc_init1(MachineState *machine,
     }
 #ifdef CONFIG_IDE_ISA
 else {
-        for(i = 0; i < MAX_IDE_BUS; i++) {
+        for (int i = 0; i < MAX_IDE_BUS; i++) {
             ISADevice *dev;
             char busname[] = "ide.0";
             dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],
Signature · 1 line
2.23.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Thomas Huth

When CONFIG_IDE_ISA is disabled, compilation currently fails:

hw/i386/pc_piix.c: In function ‘pc_init1’:
hw/i386/pc_piix.c:81:9: error: unused variable ‘i’ [-Werror=unused-variable]

Move the variable declaration to the right code block to avoid this problem.

Fixes 4501d317b50e ("hw/i386/pc: Extract pc_i8259_create()")
Quoted 27 lines
---
 hw/i386/pc_piix.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/i386/pc_piix.c +1 -2
index 2aefa3b8df..d187db761c 100644
@@ -78,7 +78,6 @@ static void pc_init1(MachineState *machine,
     X86MachineState *x86ms = X86_MACHINE(machine);
     MemoryRegion *system_memory = get_system_memory();
     MemoryRegion *system_io = get_system_io();
-    int i;
     PCIBus *pci_bus;
     ISABus *isa_bus;
     PCII440FXState *i440fx_state;
@@ -253,7 +252,7 @@ static void pc_init1(MachineState *machine,
     }
 #ifdef CONFIG_IDE_ISA
 else {
-        for(i = 0; i < MAX_IDE_BUS; i++) {
+        for (int i = 0; i < MAX_IDE_BUS; i++) {
             ISADevice *dev;
             char busname[] = "ide.0";
             dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],
Signature · 1 line
2.23.0
#2 lore raw
In reply to Thomas Huth
Quoted 28 lines · Thomas Huth

When CONFIG_IDE_ISA is disabled, compilation currently fails:

hw/i386/pc_piix.c: In function ‘pc_init1’:
hw/i386/pc_piix.c:81:9: error: unused variable ‘i’ [-Werror=unused-variable]

Move the variable declaration to the right code block to avoid this problem.

Fixes 4501d317b50e ("hw/i386/pc: Extract pc_i8259_create()")
---
 hw/i386/pc_piix.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/i386/pc_piix.c +0 -1
index 2aefa3b8df..d187db761c 100644
@@ -78,7 +78,6 @@ static void pc_init1(MachineState *machine,
     X86MachineState *x86ms = X86_MACHINE(machine);
     MemoryRegion *system_memory = get_system_memory();
     MemoryRegion *system_io = get_system_io();
-    int i;
     PCIBus *pci_bus;
     ISABus *isa_bus;
     PCII440FXState *i440fx_state;

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 258

Quoted 8 lines
hw/i386/pc_piix.c +1 -1
@@ -253,7 +252,7 @@ static void pc_init1(MachineState *machine,
     }
 #ifdef CONFIG_IDE_ISA
 else {
-        for(i = 0; i < MAX_IDE_BUS; i++) {
+        for (int i = 0; i < MAX_IDE_BUS; i++) {
             ISADevice *dev;
             char busname[] = "ide.0";
             dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],

Don't put variable declarations inside 'for' statements, please. They should go at the start of a {} block.

thanks
-- PMM

#3 lore raw
Quoted 32 lines · Peter Maydell
Quoted 31 lines · Thomas Huth

When CONFIG_IDE_ISA is disabled, compilation currently fails:

hw/i386/pc_piix.c: In function ‘pc_init1’:
hw/i386/pc_piix.c:81:9: error: unused variable ‘i’ [-Werror=unused-variable]

Move the variable declaration to the right code block to avoid this problem.

Fixes 4501d317b50e ("hw/i386/pc: Extract pc_i8259_create()")
---
 hw/i386/pc_piix.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/i386/pc_piix.c +0 -1
index 2aefa3b8df..d187db761c 100644
@@ -78,7 +78,6 @@ static void pc_init1(MachineState *machine,
     X86MachineState *x86ms = X86_MACHINE(machine);
     MemoryRegion *system_memory = get_system_memory();
     MemoryRegion *system_io = get_system_io();
-    int i;
     PCIBus *pci_bus;
     ISABus *isa_bus;
     PCII440FXState *i440fx_state;
@@ -253,7 +252,7 @@ static void pc_init1(MachineState *machine,
     }
 #ifdef CONFIG_IDE_ISA
 else {
Quoted 8 lines

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 258

Quoted 5 lines
hw/i386/pc_piix.c +1 -1
-        for(i = 0; i < MAX_IDE_BUS; i++) {
+        for (int i = 0; i < MAX_IDE_BUS; i++) {
             ISADevice *dev;
             char busname[] = "ide.0";
             dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],

Don't put variable declarations inside 'for' statements, please. They should go at the start of a {} block.

Why? We're using -std=gnu99 now, so this should not be an issue anymore.

Thomas

#4 lore raw
Quoted 42 lines · Thomas Huth
Quoted 32 lines · Peter Maydell
Quoted 31 lines · Thomas Huth

When CONFIG_IDE_ISA is disabled, compilation currently fails:

hw/i386/pc_piix.c: In function ‘pc_init1’:
hw/i386/pc_piix.c:81:9: error: unused variable ‘i’ [-Werror=unused-variable]

Move the variable declaration to the right code block to avoid this problem.

Fixes 4501d317b50e ("hw/i386/pc: Extract pc_i8259_create()")
---
 hw/i386/pc_piix.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/i386/pc_piix.c +0 -1
index 2aefa3b8df..d187db761c 100644
@@ -78,7 +78,6 @@ static void pc_init1(MachineState *machine,
     X86MachineState *x86ms = X86_MACHINE(machine);
     MemoryRegion *system_memory = get_system_memory();
     MemoryRegion *system_io = get_system_io();
-    int i;
     PCIBus *pci_bus;
     ISABus *isa_bus;
     PCII440FXState *i440fx_state;
@@ -253,7 +252,7 @@ static void pc_init1(MachineState *machine,
     }
 #ifdef CONFIG_IDE_ISA
 else {
Quoted 8 lines

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 258

Quoted 5 lines
hw/i386/pc_piix.c +1 -1
-        for(i = 0; i < MAX_IDE_BUS; i++) {
+        for (int i = 0; i < MAX_IDE_BUS; i++) {
             ISADevice *dev;
             char busname[] = "ide.0";
             dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],

Don't put variable declarations inside 'for' statements, please. They should go at the start of a {} block.

Quoted 1 line

Why? We're using -std=gnu99 now, so this should not be an issue anymore.

For now I can squash the following while we discuss coding standards. :)

hw/i386/pc_piix.c +2 -1
index fa62244f4d..0130b8fb4e 100644
@@ -244,7 +244,8 @@ static void pc_init1(MachineState *machine,
     }
 #ifdef CONFIG_IDE_ISA
 else {
-        for (int i = 0; i < MAX_IDE_BUS; i++) {
+        int i;
+        for (i = 0; i < MAX_IDE_BUS; i++) {
             ISADevice *dev;
             char busname[] = "ide.0";
             dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],

Paolo

#5 lore raw
Quoted 52 lines · Paolo Bonzini
Quoted 42 lines · Thomas Huth
Quoted 32 lines · Peter Maydell
Quoted 31 lines · Thomas Huth

When CONFIG_IDE_ISA is disabled, compilation currently fails:

hw/i386/pc_piix.c: In function ‘pc_init1’:
hw/i386/pc_piix.c:81:9: error: unused variable ‘i’ [-Werror=unused-variable]

Move the variable declaration to the right code block to avoid this problem.

Fixes 4501d317b50e ("hw/i386/pc: Extract pc_i8259_create()")
---
 hw/i386/pc_piix.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/i386/pc_piix.c +0 -1
index 2aefa3b8df..d187db761c 100644
@@ -78,7 +78,6 @@ static void pc_init1(MachineState *machine,
     X86MachineState *x86ms = X86_MACHINE(machine);
     MemoryRegion *system_memory = get_system_memory();
     MemoryRegion *system_io = get_system_io();
-    int i;
     PCIBus *pci_bus;
     ISABus *isa_bus;
     PCII440FXState *i440fx_state;
@@ -253,7 +252,7 @@ static void pc_init1(MachineState *machine,
     }
 #ifdef CONFIG_IDE_ISA
 else {
Quoted 8 lines

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 251

Quoted 5 lines
hw/i386/pc_piix.c +1 -1
-        for(i = 0; i < MAX_IDE_BUS; i++) {
+        for (int i = 0; i < MAX_IDE_BUS; i++) {
             ISADevice *dev;
             char busname[] = "ide.0";
             dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],

Don't put variable declarations inside 'for' statements, please. They should go at the start of a {} block.

Quoted 1 line

Why? We're using -std=gnu99 now, so this should not be an issue anymore.

For now I can squash the following while we discuss coding standards. :)

hw/i386/pc_piix.c +0 -0
index fa62244f4d..0130b8fb4e 100644

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 251

Quoted 9 lines
hw/i386/pc_piix.c +2 -1
@@ -244,7 +244,8 @@ static void pc_init1(MachineState *machine,
     }
 #ifdef CONFIG_IDE_ISA
 else {
-        for (int i = 0; i < MAX_IDE_BUS; i++) {
+        int i;
+        for (i = 0; i < MAX_IDE_BUS; i++) {
             ISADevice *dev;
             char busname[] = "ide.0";
             dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],

Yes, please do. I guess we won't update CODING_STYLE.rst during the hard freeze anymore ;-)

Thomas

#6 lore raw
Quoted 44 lines · Thomas Huth
Quoted 43 lines · Paolo Bonzini
Quoted 42 lines · Thomas Huth
Quoted 40 lines · Peter Maydell
Quoted 28 lines · Thomas Huth

When CONFIG_IDE_ISA is disabled, compilation currently fails:

hw/i386/pc_piix.c: In function ‘pc_init1’:
hw/i386/pc_piix.c:81:9: error: unused variable ‘i’ [-Werror=unused-variable]

Move the variable declaration to the right code block to avoid this problem.

Fixes 4501d317b50e ("hw/i386/pc: Extract pc_i8259_create()")
---
  hw/i386/pc_piix.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
hw/i386/pc_piix.c +0 -1
index 2aefa3b8df..d187db761c 100644
@@ -78,7 +78,6 @@ static void pc_init1(MachineState *machine,
      X86MachineState *x86ms = X86_MACHINE(machine);
      MemoryRegion *system_memory = get_system_memory();
      MemoryRegion *system_io = get_system_io();
-    int i;
      PCIBus *pci_bus;
      ISABus *isa_bus;
      PCII440FXState *i440fx_state;

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 258

Quoted 8 lines
hw/i386/pc_piix.c +1 -1
@@ -253,7 +252,7 @@ static void pc_init1(MachineState *machine,
      }
  #ifdef CONFIG_IDE_ISA
  else {
-        for(i = 0; i < MAX_IDE_BUS; i++) {
+        for (int i = 0; i < MAX_IDE_BUS; i++) {
              ISADevice *dev;
              char busname[] = "ide.0";
              dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],

Don't put variable declarations inside 'for' statements, please. They should go at the start of a {} block.

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

Why? We're using -std=gnu99 now, so this should not be an issue anymore.

For now I can squash the following while we discuss coding standards. :)

Thanks.

Quoted 10 lines
Quoted 10 lines
hw/i386/pc_piix.c +0 -1
index fa62244f4d..0130b8fb4e 100644
@@ -244,7 +244,8 @@ static void pc_init1(MachineState *machine,
      }
  #ifdef CONFIG_IDE_ISA
  else {
-        for (int i = 0; i < MAX_IDE_BUS; i++) {
Quoted 8 lines

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 251

Quoted 5 lines
hw/i386/pc_piix.c +2 -0
+        int i;
+        for (i = 0; i < MAX_IDE_BUS; i++) {
              ISADevice *dev;
              char busname[] = "ide.0";
              dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],

Yes, please do. I guess we won't update CODING_STYLE.rst during the hard freeze anymore ;-)

#7 lore raw
In reply to Thomas Huth
Quoted 26 lines · Thomas Huth
Quoted 15 lines · Peter Maydell
Quoted 14 lines · Thomas Huth
hw/i386/pc_piix.c +0 -1
@@ -78,7 +78,6 @@ static void pc_init1(MachineState *machine,
     X86MachineState *x86ms = X86_MACHINE(machine);
     MemoryRegion *system_memory = get_system_memory();
     MemoryRegion *system_io = get_system_io();
-    int i;
     PCIBus *pci_bus;
     ISABus *isa_bus;
     PCII440FXState *i440fx_state;
@@ -253,7 +252,7 @@ static void pc_init1(MachineState *machine,
     }
 #ifdef CONFIG_IDE_ISA
 else {
Quoted 8 lines

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 258

Quoted 5 lines
hw/i386/pc_piix.c +1 -1
-        for(i = 0; i < MAX_IDE_BUS; i++) {
+        for (int i = 0; i < MAX_IDE_BUS; i++) {
             ISADevice *dev;
             char busname[] = "ide.0";
             dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],

Don't put variable declarations inside 'for' statements, please. They should go at the start of a {} block.

Quoted 1 line

Why? We're using -std=gnu99 now, so this should not be an issue anymore.

Consistency with the rest of the code base, which mostly avoids this particular trick. See the 'Declarations' section of CODING_STYLE.rst.

As Paolo points out, there's a nice convenient block here already, so there's not much to be gained from putting the declaration in the middle of the for statement.

thanks
-- PMM

#8 lore raw
Quoted 27 lines · Peter Maydell
Quoted 26 lines · Thomas Huth
Quoted 23 lines · Peter Maydell
Quoted 11 lines · Thomas Huth
hw/i386/pc_piix.c +0 -1
@@ -78,7 +78,6 @@ static void pc_init1(MachineState *machine,
     X86MachineState *x86ms = X86_MACHINE(machine);
     MemoryRegion *system_memory = get_system_memory();
     MemoryRegion *system_io = get_system_io();
-    int i;
     PCIBus *pci_bus;
     ISABus *isa_bus;
     PCII440FXState *i440fx_state;

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 258

Quoted 8 lines
hw/i386/pc_piix.c +1 -1
@@ -253,7 +252,7 @@ static void pc_init1(MachineState *machine,
     }
 #ifdef CONFIG_IDE_ISA
 else {
-        for(i = 0; i < MAX_IDE_BUS; i++) {
+        for (int i = 0; i < MAX_IDE_BUS; i++) {
             ISADevice *dev;
             char busname[] = "ide.0";
             dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],

Don't put variable declarations inside 'for' statements, please. They should go at the start of a {} block.

Quoted 4 lines
Quoted 1 line

Why? We're using -std=gnu99 now, so this should not be an issue anymore.

Consistency with the rest of the code base, which mostly avoids this particular trick.

We've also got a few spots that use it...
(run e.g.: grep -r 'for (int ' hw/* )

Quoted 1 line

See the 'Declarations' section of CODING_STYLE.rst.

OK, that's a point. But since this gnu99 is a rather new option that we just introduced less than a year ago, we should maybe think of whether we want to allow this for for-loops now, too (since IMHO it's quite a nice feature in gnu99).

Thomas

#9 lore raw
Quoted 28 lines · Thomas Huth
Quoted 27 lines · Peter Maydell
Quoted 26 lines · Thomas Huth
Quoted 23 lines · Peter Maydell
Quoted 11 lines · Thomas Huth
hw/i386/pc_piix.c +0 -1
@@ -78,7 +78,6 @@ static void pc_init1(MachineState *machine,
      X86MachineState *x86ms = X86_MACHINE(machine);
      MemoryRegion *system_memory = get_system_memory();
      MemoryRegion *system_io = get_system_io();
-    int i;
      PCIBus *pci_bus;
      ISABus *isa_bus;
      PCII440FXState *i440fx_state;

hw/i386/pc_piix.c static void pc_init1(MachineState *machine, · line 258

Quoted 8 lines
hw/i386/pc_piix.c +1 -1
@@ -253,7 +252,7 @@ static void pc_init1(MachineState *machine,
      }
  #ifdef CONFIG_IDE_ISA
  else {
-        for(i = 0; i < MAX_IDE_BUS; i++) {
+        for (int i = 0; i < MAX_IDE_BUS; i++) {
              ISADevice *dev;
              char busname[] = "ide.0";
              dev = isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i],

Don't put variable declarations inside 'for' statements, please. They should go at the start of a {} block.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Why? We're using -std=gnu99 now, so this should not be an issue anymore.

Consistency with the rest of the code base, which mostly avoids this particular trick.

We've also got a few spots that use it...
(run e.g.: grep -r 'for (int ' hw/* )

~31 (vs 8000+):

$ git grep -E 'for\s*\((int|size_t)'|egrep -v '\.(cc|cpp):'
audio/audio_legacy.c:400:        for (int i = 0; audio_prio_list[i]; i++) {
hw/block/pflash_cfi02.c:281:    for (int i = 0; i < pflash_regions_count(pfl); ++i) {
hw/block/pflash_cfi02.c:889:    for (int i = 0; i < nb_regions; ++i) {
hw/i386/fw_cfg.c:39:    for (size_t i = 0; i < ARRAY_SIZE(fw_cfg_arch_wellknown_keys); i++) {
hw/i386/pc.c:1478:    for (size_t i = 0; i < ISA_NUM_IRQS; i++) {
hw/isa/lpc_ich9.c:70:    for (intx = 0; intx < PCI_NUM_PINS; intx++) {
hw/isa/lpc_ich9.c:126:        for (intx = 0; intx < PCI_NUM_PINS; intx++) {
hw/microblaze/xlnx-zynqmp-pmu.c:71:    for (int i = 0; i < XLNX_ZYNQMP_PMU_NUM_IPIS; i++) {
hw/microblaze/xlnx-zynqmp-pmu.c:124:    for (int i = 0; i < XLNX_ZYNQMP_PMU_NUM_IPIS; i++) {
hw/mips/cisco_c3600.c:472:    for (int i = 0; i < ISA_NUM_IRQS; i++) {
hw/mips/mips_malta.c:1471:    for (int i = 0; i < ISA_NUM_IRQS; i++) {
hw/ppc/fw_cfg.c:39:    for (size_t i = 0; i < ARRAY_SIZE(fw_cfg_arch_wellknown_keys); i++) {
hw/riscv/sifive_e.c:187:    for (int i = 0; i < 32; i++) {
hw/riscv/sifive_gpio.c:32:    for (int i = 0; i < SIFIVE_GPIO_PINS; i++) {
hw/riscv/sifive_gpio.c:360:    for (int i = 0; i < SIFIVE_GPIO_PINS; i++) {
hw/sd/aspeed_sdhci.c:130:    for (int i = 0; i < ASPEED_SDHCI_NUM_SLOTS; ++i) {
hw/sparc/sun4m.c:117:    for (size_t i = 0; i < ARRAY_SIZE(fw_cfg_arch_wellknown_keys); i++) {
hw/sparc64/sun4u.c:108:    for (size_t i = 0; i < ARRAY_SIZE(fw_cfg_arch_wellknown_keys); i++) {
hw/usb/hcd-xhci.c:3553:    for (intr = 0; intr < xhci->numintrs; intr++) {
hw/virtio/vhost.c:454:        for (int i = 0; i < n_old_sections; i++) {
qemu-nbd.c:302:            for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
target/i386/hvf/hvf.c:497:    for (int i = 0; i < 8; i++) {
target/i386/hvf/x86_decode.c:34:    for (int i = 0; i < decode->opcode_len; i++) {
tests/pflash-cfi02-test.c:343:    for (int region = 0; region < nb_erase_regions; ++region) {
tests/pflash-cfi02-test.c:407:    for (int region = 0; region < nb_erase_regions; ++region) {
tests/pflash-cfi02-test.c:447:    for (int region = 0; region < nb_erase_regions; ++region) {
tests/pflash-cfi02-test.c:448:        for (int i = 0; i < config->nb_blocs[region]; ++i) {
tests/pflash-cfi02-test.c:458:    for (int region = 0; region < nb_erase_regions; ++region) {
tests/pflash-cfi02-test.c:469:    for (int region = 0; region < nb_erase_regions; ++region) {
tests/pflash-cfi02-test.c:470:        for (int i = 0; i < config->nb_blocs[region]; ++i) {
tests/pflash-cfi02-test.c:658:    for (size_t i = 0; i < nb_configurations; ++i) {

[I introduced most of them without respecting the CODING_STYLE,
but checkpatch didn't complained].

Quoted 6 lines
Quoted 1 line

See the 'Declarations' section of CODING_STYLE.rst.

OK, that's a point. But since this gnu99 is a rather new option that we just introduced less than a year ago, we should maybe think of whether we want to allow this for for-loops now, too (since IMHO it's quite a nice feature in gnu99).

I agree with Thomas. I started to clean some signed/unsigned warnings and various cases the scope of some variables is confuse.

The related question is, is it OK to use size_t to iterate over an array?

  for (size_t i = 0; i < ARRAY_SIZE(array); i++) {
    ...
  }

Asking in this thread so we can modify CODING_STYLE accordingly :)

#10 lore raw

Philippe Mathieu-Daudé <[email protected]> writes:

[...]

Quoted 5 lines

The related question is, is it OK to use size_t to iterate over an array?

  for (size_t i = 0; i < ARRAY_SIZE(array); i++) {
    ...
  }

My rule of thumb on integer types is "whatever lets me avoid not-obviously-safe conversions (implicit ones in particular) with the least type cast clutter.

Quite often, int fits the bill. But not always.

To reply to your example: depends on what's hiding in the ... :)

Quoted 1 line

Asking in this thread so we can modify CODING_STYLE accordingly :)

#11 lore raw
Quoted 9 lines · Markus Armbruster

Philippe Mathieu-Daudé <[email protected]> writes:

[...]

Quoted 5 lines

The related question is, is it OK to use size_t to iterate over an array?

  for (size_t i = 0; i < ARRAY_SIZE(array); i++) {
    ...
  }
Quoted 7 lines

My rule of thumb on integer types is "whatever lets me avoid not-obviously-safe conversions (implicit ones in particular) with the least type cast clutter.

Quite often, int fits the bill. But not always.

To reply to your example: depends on what's hiding in the ... :)

The problem here is that ARRAY_SIZE() gives you an size_t, so the compiler might complain about comparing signed int with unsigned size_t.

Thus if i is only used as array index in the "..." part, I think it's fine to use size_t for i here.

Thomas