Passing literals to union arguments

R

Richard

Malcolm McLean said:
Richard said:
No. Most include one. The index is generally the count.

void setzero(double *x, int N)
{
while(N--)
x[N] = 0;
}

One integer indexing

No integer indexing.

while(n--){
*x++=0;
}
void setzero(double *x, int N)
{
int i;

for(i=0;i<N;i++)
x = 0;
}

two integer indexing. We use two integers to calculate each array
access. At leat notionally, N is referenced for each operation. In


one integer. i. Dont confuse the limits with the index.
 
M

Malcolm McLean

santosh said:
santosh said:
Malcolm said:
"Malcolm McLean" <[email protected]> writes:

When you count the labels, you'll find that

int i;

and

for(i=0;i <

are two of the most common strings in C source. Every indexing
operation involves an integer, most involve two - the index variable
and the count.

No. Most include one. The index is generally the count.

void setzero(double *x, int N)
{
while(N--)
x[N] = 0;
}

One integer indexing

void setzero(double *x, int N)
{
int i;

for(i=0;i<N;i++)
x = 0;
}

two integer indexing. We use two integers to calculate each array
access.
At leat notionally, N is referenced for each operation. In fact the two
fucntions are identical and a clever compiler might optimise 2 away.


The two functions are not identical. The first one fails to zero out the
first element of the array.


Sorry about that. Bad day.

That's why 2 is usually a lot better. 1 is harder to follow, despite being
shorter.
 
R

Richard

Malcolm McLean said:
That's why 2 is usually a lot better. 1 is harder to follow, despite
being shorter.

No, not really. I have had this many times when discussing training material. Never, ever
program "down" for nOObs. Keep it clear and the use the language as it
is mean to be used. Intermediate indices, counters etc frequently only
obfuscate and lead to more bugs.

if a C programmer can not instantly recognise something like

while(n--)
*p++=NULL;

for example then they really have no place modifying a commercial legacy
code base.

next thing you'll be advocating

#define BEGIN {

:)
 
P

pete

Malcolm said:
santosh said:
santosh said:
Malcolm McLean wrote:

<big snip>

When you count the labels, you'll find that

int i;

and

for(i=0;i <

are two of the most common strings in C source. Every indexing
operation involves an integer,
most involve two - the index variable
and the count.

No. Most include one. The index is generally the count.

void setzero(double *x, int N)
{
while(N--)
x[N] = 0;
}

One integer indexing

void setzero(double *x, int N)
{
int i;

for(i=0;i<N;i++)
x = 0;
}

two integer indexing. We use two integers to calculate each array
access.
At leat notionally, N is referenced for each operation.
In fact the two
fucntions are identical and a clever
compiler might optimise 2 away.

The two functions are not identical.
The first one fails to zero out the
first element of the array.


Sorry about that. Bad day.


Take it back. You were right.

stezero(x, 1), will zero x[0] in either function definition.

That's why 2 is usually a lot better.
1 is harder to follow, despite being shorter.

#1, is my favorite way to step through an array.

void free_ptrs(e_type **s, size_t nmemb)
{
while (nmemb-- != 0) {
free(s[nmemb]);
}
}

http://www.mindspring.com/~pfilandr/C/e_driver/e_driver.c
 
S

santosh

pete said:
Malcolm said:
santosh said:
santosh wrote:

Malcolm McLean wrote:

<big snip>

When you count the labels, you'll find that

int i;

and

for(i=0;i <

are two of the most common strings in C source. Every indexing
operation involves an integer,
most involve two - the index variable
and the count.

No. Most include one. The index is generally the count.

void setzero(double *x, int N)
{
while(N--)
x[N] = 0;
}

One integer indexing

void setzero(double *x, int N)
{
int i;

for(i=0;i<N;i++)
x = 0;
}

two integer indexing. We use two integers to calculate each array
access.
At leat notionally, N is referenced for each operation.
In fact the two
fucntions are identical and a clever
compiler might optimise 2 away.

The two functions are not identical.
The first one fails to zero out the
first element of the array.

Sorry about that. Bad day.


Take it back. You were right.

stezero(x, 1), will zero x[0] in either function definition.


I was the one who said the above, not Malcolm, as you seem to think.
 
P

pete

santosh said:
Malcolm said:
santosh wrote:

Malcolm McLean wrote:

<big snip>

When you count the labels, you'll find that

int i;

and

for(i=0;i <

are two of the most common strings in C source. Every indexing
operation involves an integer,
most involve two - the index variable
and the count.

No. Most include one. The index is generally the count.

void setzero(double *x, int N)
{
while(N--)
x[N] = 0;
}

One integer indexing

void setzero(double *x, int N)
{
int i;

for(i=0;i<N;i++)
x = 0;
}

two integer indexing. We use two integers to calculate each array
access.
At leat notionally, N is referenced for each operation.
In fact the two
fucntions are identical and a clever
compiler might optimise 2 away.

The two functions are not identical.
The first one fails to zero out the
first element of the array.

Sorry about that. Bad day.


Take it back. You were right.

stezero(x, 1), will zero x[0] in either function definition.


I was the one who said the above, not Malcolm, as you seem to think.


Sorry about that.
 
S

Serve Lau

Malcolm McLean said:
void setzero(double *x, int N)
{
while(N--)
x[N] = 0;
}

With this case its so easy to introduce bugs when somebody decides to make N
unsigned
 
R

Richard Heathfield

Serve Lau said:
Malcolm McLean said:
void setzero(double *x, int N)
{
while(N--)
x[N] = 0;
}

With this case its so easy to introduce bugs when somebody decides to
make N unsigned

void setzero(double *x, unsigned int N)
{
while(N--)
x[N] = 0;
}

I see no bug. Do you?
 
P

pete

Richard said:
Serve Lau said:
Malcolm McLean said:
void setzero(double *x, int N)
{
while(N--)
x[N] = 0;
}

With this case its so easy to introduce
bugs when somebody decides to make N unsigned

void setzero(double *x, unsigned int N)
{
while(N--)
x[N] = 0;
}

I see no bug. Do you?

I use that way all the time to step through arrays
and always with an unsigned index type, usually size_t.
 
S

Serve Lau

Richard Heathfield said:
With this case its so easy to introduce bugs when somebody decides to
make N unsigned

void setzero(double *x, unsigned int N)
{
while(N--)
x[N] = 0;
}

I see no bug. Do you?

I meant style when I said case. When one codes like this you will find code
like
while(--n >= 0) Then trouble will begin.
I experienced that when I first had to start using lint. It would complain
when sizeof expressions were assigned to an int and I decided to change it
into unsigned
 

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,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top