Why do so few people know the difference between arrays and pointers.

M

Malcolm

Richard Bos said:
If I send a letter to you, should I care whether you live in a
detached house or in a flat? Of course not - as long as I have
your correct address, the letter will arrive fine.
But you need to know whether you are sending one gas bill (to the detached
house) or many (to each resident of the block of flats).

If "int *ptr;" were a pointer to an integer, period, then

ptr[100] = x

would be illegal.
 
I

Irrwahn Grausewitz

Malcolm said:
Richard Bos said:
If I send a letter to you, should I care whether you live in a
detached house or in a flat? Of course not - as long as I have
your correct address, the letter will arrive fine.
But you need to know whether you are sending one gas bill (to the detached
house) or many (to each resident of the block of flats).

If "int *ptr;" were a pointer to an integer, period, then

ptr[100] = x

would be illegal.

Since ptr[100] is just shorthand for (*((ptr)+(100))) , why should
it be illegal? Array subscripting is plain old pointer arithmetic
wrapped up in a nice syntactic construct, and it doesn't matter at
all, if it's performed on a 'genuine' pointer or on one yielded by
'array decay'. In your example ptr *is* a pointer to an integer - no
more, no less. Period.

Regards
 
M

Mike Wahler

Malcolm said:
But you need to know whether you are sending one gas bill (to the detached
house) or many (to each resident of the block of flats).

That's why we must use 'element count' arguments to functions
which use pointer (to element-type) arguments to process arrays.

Start at first flat, deliver bill, move to next flat, deliver bill.

-Mike
 
D

Default User

Joe said:
Also, there seems to be a shift towards software engineering and away from
general computer science.


You're going need to define those two terms for meaningful conversation.




Brian Rodenborn
 
A

Alex Monjushko

E. Robert Tisdale said:
Mike Wahler wrote:
Can you give us an example of a value that is *not* an object?

int a = 12;

The identifier 'a' is an object of type int, with the value of 12.
The integer literal 12 has the value of 12 but is not an object.
 
J

Joe Laughlin

Default said:
You're going need to define those two terms for
meaningful conversation.




Brian Rodenborn

"general computer science" => theory, algorithms, lower-level stuff
"software engineering" => the process of developing a complex software
system

That's how I understand them anyways.
 
E

E. Robert Tisdale

Joe said:
At the university I recently graduated from,
the professors didn't "teach" languages.

You don't need professors to teach programming languages.
You can get a lecturer, maybe a graduate student, to do that.
You don't need to teach programming languages
to computer science students.
Computer science students are usually smart enough
to pick up programming languages on their own as needed.
When computer programming languages are offered at a University
to computer science students they are considered "remedial".
They used languages to show various CS concepts.
Primarily, it was done in C++,
although the networking and OS classes used C.
I think this is rather typical.

Also, there seems to be a shift towards software engineering
and away from general computer science. Dunno if this is good or not.

I don't think so. Software Engineering is a field in Computer Science.
What you are witnessing is not a shift away from computer science
but rapid relative growth in a field of computer science
that has long been neglected and is exploding now only because
there are finally computers fast enough with enough memory and storage
that it is practical to design and implement
*very* large software packages.
As a recent graduate (this last week), I just started a new job,
and I've found out that I'm pretty rusty on pointers,
bit-shifting/byte-packing techniques, big/little-endians, etc.
However, I feel that I'm pretty good at software design,
object-oriented programming, project management, etc.

Unfortunately, programming has almost nothing to do
with software engineering as you well know.
A software engineer is *not* just a glorified computer programmer.
 
K

Keith Thompson

E. Robert Tisdale said:
Arthur said:

Wrong.

That's an object of type int with the [constant] value of 42.

C99 3.14 defines the word "object" as "region of data storage in the
execution environment, the contents of which can represent values".

There is no "region of data storage" necessarily associated with the
expression (int)42. It's an expression, and it has a value, but it's
not an object.

Evaluation of the expression may or may not involve an int-size region
of storage containing the value 42; that's an implementation detail,
irrelevant to C's definition of "object".

If that doesn't convince you, how about this:
int x = 41;
x + 1;

x + 1 is an expression whose value is 42. There is no object with
that value.
 
C

Chris Torek

int a = 12;

The identifier 'a' is an object of type int, with the value of 12.
The integer literal 12 has the value of 12 but is not an object.

More precisely, the identifier is the name of (or "a label for")
an object of type int. The object itself is the "region of storage":

3.15 Object

[#1] A region of data storage in the execution environment,
the contents of which can represent values. Except for
bit-fields, objects are composed of contiguous sequences of
one or more bytes, the number, order, and encoding of which
are either explicitly specified or implementation-defined.
When referenced, an object may be interpreted as having a
particular type; see 6.2.2.1.

(from a C99 draft).

No constant is an object, and no object is ever a constant, in C
(here C differs markedly from C++, where "const" variables *are*
constants, yet can be C-style objects). Note that I use the
ANSI C definition of the term "constant" here:

constant:
floating-constant
integer-constant
enumeration-constant
character-constant

(same C99 draft, but uchanged from C89). These differ from a
"constant-expression"; for instance, while (1+2) is a
"constant-expression", it is not a "constant".

String literals (except when used as initializers for character
arrays) are a special case: they create objects that have static
duration. The address of *any* static-duration object -- not just
those from string literals -- has a "constant address", which can
be computed at compile and/or link time in most cases, but this
does not make it a "constant", or even a "constant-expression",
in C. You *can* use them as initializers for other static-duration
variables though:

static char *p = "string literal";

or:

static double pi = 2.718281828459045235360287; /* [%] */
static double *dp = π

[% A rather precise pi, but not very accurate. :) ]

Hence, the concept of "constant" in C is somewhat peculiar and
subtle.

In C89, with one kind-of-broken exception, arrays are never values,
only objects. C99 attemps to correct the problem, which occurs
with struct-valued functions in which the struct contains (or
even consists of) an array:

struct S { int a[20]; };
struct S f(void);
...
... f().a ...

Here f().a appears as a sub-expression, yet because it part of a
returned struct, the array "a" is not an object with automatic,
static, or allocated duration. In effect, it is an array value,
instead of an array object. The problem is that you cannot take
the address of a value in C:

int *p = &42; /* invalid -- diagnostic required */

Array subscripting works, at least conceptually, by taking the
address of the array's first element, so f().a needs to "take
the address" of f().a -- an address that does not exist. C99's
solution to this problem in C89 wound up re-defining "lvalue" badly.
(I am not sure that there *is* any good solution to this. The
approach I would probably have taken is to define all structure-valued
functions as returning an "automatically-dereferenced pointer" to
a "very-short-duration object", but describing the storage duration
of that object is itself problematic. This pointer would allow
computing &(f().a), and even &(f()), and using it until the object's
duration expires. Depending on the lifetime, passing &(f()) to
memcpy() might become valid, for instance. I suspect the right
duration might be "until the next sequence point", though, and the
sequence point before the call to memcpy() would invalidate the
pointer. But then consider calls of the form: g(f(), f()), where
g() is a function taking a "struct S" -- are they defined, or not?)
 
S

Stephen Sprunk

E. Robert Tisdale said:
You don't need professors to teach programming languages.
You can get a lecturer, maybe a graduate student, to do that.
You don't need to teach programming languages
to computer science students.
Computer science students are usually smart enough
to pick up programming languages on their own as needed.
When computer programming languages are offered at a University
to computer science students they are considered "remedial".

Unfortunately many educational institutions disagree. I dropped my CS major
because of this, actually; the entire undergrad program was dedicated to
teaching me things I already knew or could have picked up on my own, and all
the classes I wanted to take to learn REAL computer science were
post-graduate level.
I don't think so. Software Engineering is a field in Computer Science.

I've always considered "sciences" to be about building on existing ideas to
create and validate (or invalidate) new ideas, whereas "engineering" is more
an application of existing ideas to real-world problems. In programming
this is a bit blurred, but one can surely see the distinction between trying
to find new compiler optimizations vs. coding some commercial database app.
Unfortunately, programming has almost nothing to do
with software engineering as you well know.
A software engineer is *not* just a glorified computer programmer.

I'd say that software design requires a very different skillset than
software implementation, but both are aspects of software engineering.

S
 
E

E. Robert Tisdale

Keith said:
C99 3.14 defines the word "object"
as "region of data storage in the execution environment,
the contents of which can represent values".

There is no "region of data storage"
necessarily associated with the expression (int)42.

Where is it written that a "region of data storage"
must *necessarily* be associated with the expression (int)42?
It's an expression, and it has a value, but it's not an object.

Evaluation of the expression may or may not involve
an int-size region of storage containing the value 42;
that's an implementation detail,
irrelevant to C's definition of "object".

Are we to infer that
the existence of an object depends upon the implementation?
If that doesn't convince you, how about this:

int x = 41;
x + 1;

x + 1 is an expression whose value is 42.
There is no object with that value.

You are wrong.

If x is a static variable,
it could be initialized with the value 41 by the compiler
but, is x is an automatic variable,
the code emitted by the compiler will need to get the value 41
from some storage somewhere to initialize x.
According to you, then 41 would be an object.
Unless your compiler optimizes away the "x + 1" expression
(perhaps because it is not used),
a temporary object will be allocated (in a register perhaps)
to store the value of x + 1.
Consider, for example,

int x = 41;
int y = x + 1;
int z = 3*y;
// no further references to y

A good optimizing compiler will elide the storage for y
and compute

int z = 3*(x + 1);

directly.

In C, an object is something *could* occupy storage.
Whether it actually does occupy storage or not
may be left up to the optimizer.
 
E

E. Robert Tisdale

Chris Torek wrote:

[snip]
No constant is an object, and no object is ever a constant,
in C (here C differs markedly from C++,
where "const" variables *are* constants, yet can be C-style objects).
[snip]

Hence, the concept of "constant" in C is somewhat peculiar and subtle.

And flawed.
In C89, with one kind-of-broken exception, arrays are never values,
only objects.
[snip]

C99's solution to this problem in C89 wound up re-defining "lvalue" badly.

These are symptoms of serious design flaws and muddled thinking.
You can't fix these problems be redefining words or "overloading"
the meaning of words. These are just *weasel* words or meanings.
The introduction of one weasel word creates problems
that require the introduction of other weasel words
until you can't keep up any more.
 
D

Dan Pop

In said:
Where is it written that a "region of data storage"
must *necessarily* be associated with the expression (int)42?

In the definition of "object". If it isn't, then (int)42 is not an
object. Which you can trivially check by attempting to take the address
of (int)42. If you succeed, then you have an object, if you don't, you
were merely spitting bullshit, as usual.

Dan
 
M

Mike Wahler

E. Robert Tisdale said:
Where is it written that a "region of data storage"
must *necessarily* be associated with the expression (int)42?

Nowhere. Exactly as Keith says. Read again what he wrote above.

the existence of an object depends upon the implementation?


You are wrong.

If x is a static variable,
it could be initialized with the value 41 by the compiler
but, is x is an automatic variable,
the code emitted by the compiler will need to get the value 41
from some storage somewhere to initialize x.

No, it need not.
According to you, then 41 would be an object.

No, according to Keith's explanation, it would not.
Read again what he wrote above.
Unless your compiler optimizes away the "x + 1" expression
(perhaps because it is not used),

Optimization has nothing to do with it.
a temporary object will be allocated (in a register perhaps)
to store the value of x + 1.
Consider, for example,

int x = 41;
int y = x + 1;
int z = 3*y;
// no further references to y

A good optimizing compiler will elide the storage for y
and compute

int z = 3*(x + 1);

directly.

In C, an object is something *could* occupy storage.

This assertion directly contradicts the C standard,
which specifically defines an object as a region of
storage. It *is* a region of storage, not 'might be'
or 'could be'.
Whether it actually does occupy storage or not
may be left up to the optimizer.

Huh? Again, 'optimizers', have nothing to do with it.
No storage, no object.

-Mike
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top