Why Not Local Loops? Reasons Explained

  • Thread starter Thread starter jk22
  • Start date Start date
  • Tags Tags
    Local Loops
Click For Summary

Discussion Overview

The discussion revolves around the practice of declaring loop control variables within the scope of a loop in programming, particularly in C and C++. Participants explore the implications of this practice, including coding standards, scope management, and the potential for confusion in variable naming.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Some participants argue that declaring loop variables within the loop is acceptable and can enhance code readability by limiting the variable's scope.
  • Others suggest that traditional practices recommend declaring variables at the top of a program to avoid scope-related issues, particularly in older C standards.
  • A few participants mention that coding standards, such as MISRA, advise against inline declarations to prevent variable shadowing and enhance code reviewability.
  • Some express that using generic variable names like 'i' or 'j' can lead to maintenance challenges, advocating for more descriptive names instead.
  • There is a discussion about the performance implications of variable scope and locality in relation to CPU cache behavior, particularly in the context of C99's allowance for inline declarations.
  • Participants note that while some instructors may discourage inline declarations, many programmers find them practical and commonly used in modern coding practices.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best practice regarding loop variable declarations. There are multiple competing views on the appropriateness of inline declarations versus traditional variable declarations, as well as differing opinions on naming conventions.

Contextual Notes

Some participants highlight the importance of context in coding standards, noting that different environments may have varying requirements. The discussion also reflects a tension between modern programming practices and older conventions.

Who May Find This Useful

Programmers, software developers, and students interested in coding standards, variable scope management, and best practices in loop construction may find this discussion relevant.

  • #31
newjerseyrunner said:
strcpy is sort of like this, but with one very big difference. destination=source won't copy the character, it'l copy the pointer (*destination = *source) does a copy, notice that you must dereference the pointer and like I said destination++ has no pointer.
I absolutely agree. My only excuse is that I was late to an appointment and a bug snuck in (never write code in a hurry). Here are the correct statements:

char *destination, *source;

while (*destination++ = *source++);
...
while (*++destination= *source++);
...
while (*destination++ = *++source);
...
while (*++destination= *++source);
...
 
  • Like
Likes   Reactions: newjerseyrunner
Technology news on Phys.org
  • #32
I never understood this, does ++source jump 4 bytes since it is an address ?
 
  • #33
jk22 said:
I never understood this, does ++source jump 4 bytes since it is an address ?
"Pointer arithmetic is scaled by the type of object pointed to."
What this means is that you can't know what incrementing, decrementing, adding an integer to, subtracting an integer from, or subtracting two pointers will do, unless you know what type they are pointers to.

If source is declared as char * source;
then incrementing source evaluates to the address in memory of the next character.

If source is declared as int * source;
then incrementing source evaluates to the address in memory of the next int, which would typically be four bytes higher in memory.

If source is declared as double * source;
then incrementing source evaluates to the address in memory of the next double, which would typically be eight bytes higher in memory.
 
  • Like
Likes   Reactions: jk22
  • #34
jk22 said:
I never understood this, does ++source jump 4 bytes since it is an address ?
No. It depends on what the pointer points to. Thus:
C:
char *cptr;
cptr++; /* Increases the pointer by 1, since it points to (an array of) characters */

short *sptr;
sptr++; /* Increases the pointer by 2, since it points to (an array of) shorts (16 bit) */

long *lptr;
lptr++; /* Increases the pointer by 4, since it points to (an array of) longs (32 bit) */

struct complicated *comptr;
comptr++; /* Increases the pointer by [I]sizeof(struct complicated[/I]), since it points to (an array of) structs */

But beware:

void *ptr;
ptr++;  /* Illegal, causes a compiler error. A void pointer is just a pointer, it cannot be used for anything but as a parameter in a call */

long *lptr;
lptr = ptr; /* Legal, since the void pointer is compatible with all pointers */
lptr++;  /* Now we know how to increment the pointer */
 
Last edited:
  • Like
Likes   Reactions: jk22

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K