segfault w/ block, but not file scope

N

Nelu

It is just as amusing as you assertion, backed by no well known authority of
the c language specification, that c does not support pass by reference.

Your continued reference to 'object' during replies proves my assertions --
recent OOP development (decades ago implemented recent, I mean) have
obfuscated the meaning of "pass by reference". I suspect this is due mostly
to the declining software knowledge, and skills, that has again raised the
acclaim and praise for "garbage collection" languages.

Show me any authority that shares you "opinion" that c does not "pass by
reference".

Arguments are always passed by value in C function calls, meaning that local copies of the values of the arguments are passed to the routines. Changes that you may make to the arguments in the function are made only to the local copies of the arguments. Passing by reference means you can change the value of the argument outside the scope of your function, which doesn't happen. If you have a function like: void foo(char *a) you cannot change the value of the argument, but you can change the content of the memory that the address points to. Pass-by-xxxx refers to the way the arguments are passed to the function and, in C, it's always by value. The fact that the function can change the value at the address your pointer points to is just a side-effect and has nothing to do with how the arguments are passed.

I don't remember exatly and I don't have a copy of the book right now, but I think K&R says that arguments are always passed by value in C function calls. No one will say anything about pass by reference because there's no such thing in C.

JAVA is a languae where everything's a reference. JAVA also passes the arguments to functions by value *only* (if you don't believe me check the documentation), but you can change the elements of an array that's passed to a function and it doesn't mean that array was passed by reference.

If I remember correctly in C++:

void foo(int &i) {
i=2;
}
....
int i=0;
foo(i)
=> i=2

type is int, you send int, int is changed in the function. That's pass by reference.

in C:

void foo(int *i) {
*i=2;
}
....
int i=0;
foo(&i);
=> i=2

type is int, you send a pointer to int, int is changed in the function. The functions doesn't tell you that it needs an int to change an int. It tells you it needs a pointer to int that it can't change.
Although you can consider &int a reference to an int, the function doesn't ask for a reference but for a type which is called "pointer to int" and what you send is the value of a pointer to int type.
 
J

Joseph Dionne

Nelu said:
Arguments are always passed by value in C function calls, meaning that local copies of the values of the arguments are passed to the routines. Changes that you may make to the arguments in the function are made only to the local copies of the arguments. Passing by reference means you can change the value of the argument outside the scope of your function, which doesn't happen. If you have a function like: void foo(char *a) you cannot change the value of the argument, but you can change the content of the memory that the address points to. Pass-by-xxxx refers to the way the arguments are passed to the function and, in C, it's always by value. The fact that the function can change the value at the address your pointer points to is just a side-effect and has nothing to do with how the arguments are passed.

I don't remember exatly and I don't have a copy of the book right now, but I think K&R says that arguments are always passed by value in C function calls. No one will say anything about pass by reference because there's no such thing in C.

JAVA is a languae where everything's a reference. JAVA also passes the arguments to functions by value *only* (if you don't believe me check the documentation), but you can change the elements of an array that's passed to a function and it doesn't mean that array was passed by reference.

If I remember correctly in C++:

void foo(int &i) {
i=2;
}
...
int i=0;
foo(i)
=> i=2

type is int, you send int, int is changed in the function. That's pass by reference.

It is pass by reference *because* i can and is modified. So to is i modified
in c, only the syntax has been changed to protect the innocent?
 
N

Nelu

<something about pass by reference>

Really sorry for the last post. I switched, recently,
from google groups to slrn and I didn't know it
knows not how to cut long lines. It's the first time
I use it...
I'll remember this for the future.
Also, from slrn it's easier to understand why keeping
some context when repying is useful :).
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Fortunately, the C language has rules that permit implementations to
distinguish between a "binary bitwise logical and" operator and a "unary
prefix address of" operator, even if they both share the same translation token.

Are you telling me that

{
int a = 7, b = 4, *c;

c = & a;
}

will result in a syntax error? I think not.
I'm beginning to suspect that you may be a deliberate troll. [snip]
The unary "&" operator doesn't yield the contents of a pointer; it
yields the address of an object.
[snip]

It is just as amusing as you assertion, backed by no well known
authority of the c language specification, that c does not support pass
by reference.

- From the C 9989-1999 proposed standard (and, IIRC, from previous C standards,
all the way back to K&R C and forward to the accepted C'99 standard)

6.5.2.2 Function Calls

4 An argument may be an expression of any object type. In preparing for a
call to the function, the arguments are evaluated, and each parameter is
assigned the value of the corresponding argument.

Sounds like the standard says "pass by value" to me. And it doesn't say "pass
by reference" anywhere in it.

Perhaps you've become fooled by the mechanics of pointer variables. Passing
pointers to functions can /look/ like "pass by reference" even though it is
actually "pass by value". Consider the following code fragments

void foo(int *a)
{
/* body of function foo */
*a = 8;
}

void bar(void)
{
int q = 6;
foo(&q);
}

In the above functions, you might think that "pass by reference" is being
used, but it is not.

In the context of the function call to foo() ( "foo(&q);" )
q is an object of type int.

&q is another object (a temporary one crafted by the language to serve as a
vehicle to pass data to a function) of type "pointer to int"

q has a value of 6
&q has a value of a pointer to q

Function foo() will receive the /value/ of the temporary object &q. It does
not receive a /reference/ to q (or even to the temporary &q). It's as if that
original function had been coded as
void bar(void)
{
int q = 6;
int *pq = &q;

foo(pq); /* still pass by value, just value of pq */
}
Your continued reference to 'object' during replies proves my assertions
-- recent OOP development (decades ago implemented recent, I mean)

Well, Keith is using the word "object" in the same manner (and apparently with
the same meaning) as the C standard (and it's predecessor documents) does.

- From the C 9989-1999 proposed standard document section 3 ("Terms, definitions
and Symbols"):

3.14
object
region of data storage in the execution environment, the contents of which
can represent values

C 9989-1999 goes on using the word "object" throughout it's text, in exactly
the context that Keith has used it. I'm afraid that you don't have any choice
but to accept this terminology, recent OOP usage or not.
have
obfuscated the meaning of "pass by reference". I suspect this is due
mostly to the declining software knowledge, and skills, that has again
raised the acclaim and praise for "garbage collection" languages.

Show me any authority that shares you "opinion" that c does not "pass by
reference".

I think that the C standards should be sufficient authority to confirm Keith's
"opinion".

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDwKNjagVFX4UWr64RAvDrAJ9JRNN/WaXaq0J4O4egJiuRwCtNiQCeN/y5
ou4vGFXt3wBzmmUDCwCz/+I=
=p6kP
-----END PGP SIGNATURE-----
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
It is just as amusing as you assertion, backed by no well known authority of
the c language specification, that c does not support pass by reference.

Your continued reference to 'object' during replies proves my assertions --
recent OOP development (decades ago implemented recent, I mean) have
obfuscated the meaning of "pass by reference". I suspect this is due mostly
to the declining software knowledge, and skills, that has again raised the
acclaim and praise for "garbage collection" languages.

Show me any authority that shares you "opinion" that c does not "pass by
reference".


Arguments are always passed by value in C function calls, meaning that
local copies of the values of the arguments are passed to the routines. [snip]
I don't remember exatly and I don't have a copy of the book right now,
but I think K&R says that arguments are always passed by value in C function
calls. No one will say anything about pass by reference because there's no
such thing in C.

You are correct.

K&R C (First edition) says

"In preparing for the call to a function, a copy is made of each actual
parameter; thus, all argument-passing in C is strictly by value."

(Appendix A: C Reference Manual, "The C Programming Language", Brian W.
Kernighan & Dennis M. Ritchie, Copyright 1978 by Bell Telephone Laboratories,
Incorporated, Published by Prentice-Hall, Inc.)


[snip]

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDwKVVagVFX4UWr64RAv9TAKC564WPjQqJmYXWaCQkRp3X3j3vCQCfXWPG
Jbi1XWwB3A3vCNa0SQTfGac=
=kjjN
-----END PGP SIGNATURE-----
 
N

Nelu

It is pass by reference *because* i can and is modified. So to is i modified
in c, only the syntax has been changed to protect the innocent?
In the C function I posted &i is the argument not i.
The i outside the function is modified but that i is not
the same as the one in the function. Modifying the i in the function
has no effect on the i outside the function. The i outside the function
is modified by modifying something different in the function,
i.e. *i which is not the variable you passed to the function.
Even if this is a mechanism that allows for the implementation of
pass-by-reference, it's not the same thing. You can say that
pass-by-reference can hide a pass-by-value but not that a pass-by-value
hides a pass-by-reference (otherwise it means that making a change
to the copy automatically changes the original, which doesnt't happen),
so they are not the same thing.
 
J

Joseph Dionne

Nelu said:
In the C function I posted &i is the argument not i.
The i outside the function is modified but that i is not
the same as the one in the function. Modifying the i in the function
has no effect on the i outside the function. The i outside the function
is modified by modifying something different in the function,
i.e. *i which is not the variable you passed to the function.
Even if this is a mechanism that allows for the implementation of
pass-by-reference, it's not the same thing. You can say that
pass-by-reference can hide a pass-by-value but not that a pass-by-value
hides a pass-by-reference (otherwise it means that making a change
to the copy automatically changes the original, which doesnt't happen),
so they are not the same thing.

The syntax difference in c++ is sexier, easier for those new to the language
to implement with less debug time, but is nothing more than using the
principle of passing a reference to a variable in order to modify the original
memory contents.

A custom c pre processor can simulate the same syntax, even support both
syntax, but that would most likely introduce more developer coding bugs not
less as desired.

I will only agree to disagree with the assertions made that "pass by
reference" is more about language syntax than language capability. Pass by
reference as a principle is well defined, and c complies with the definition.

Now this *troll* needs to get on with real world work, as some suggest I lack.

Cheers to all, the discussion was enlightening.
 
R

Richard Heathfield

Joseph Dionne said:
A pointer can be passed by value,

Yes, I know. In fact, that is the only way it can be passed directly as a
function argument.
it that address of the memory storing an
address to memory used for "data", or passed by reference to "hide" it
memory address from the function called.

No, it can't be passed by reference.
For example, using your allocstr() function called by value;

{
char *t;

allocstr(t,10);
}

Will not have the desired result,
Gosh.

char pointer t will remain pointing at
it
original ram address both before and after the call to allocstr().

Is that right?
However, I can pass a reference to t, syntactically &s, that WILL have the
desired result.

No, you can't, because it's the wrong type, and in any case passing *a*
reference is not the same as passing *by* reference.

Note for terminology bigots: "The technical term used in programming
languags for an address of a location in memory is a /reference/ because it
/refers/ to a memory location." - Bruce Maclennan, "Principles of
Programming Languages", p57). The C Standard does not appear to use the
word in this way, however; at least, neither the C89 draft nor N869.
(Searching a PDF is like searching your spam for email, so I didn't look in
C99 Final.)

Further "by value" passing of pointers is strlen() implementation, for
example;

int strlen(char *s)

If that's how you implement strlen, remind me not to use your
implementation.
{
int ii;

for(ii=0;*s;ii++);

Undefined behaviour when (not if) ii overflows. It will overflow for all
strings except the empty string.
 
N

Nejat AYDIN

Joseph Dionne wrote:
[...]
Show me any authority that shares you "opinion" that c does not "pass by
reference".

"In preparing for the call to a function, a copy is made of each
argument; all argument-passing is strictly by value."
The C Programming Language, Second edition.
Brian W. Kernighan, Dennis M. Ritchie
A7.3.2 Function Calls (pages 201, 202)
 
R

Richard Heathfield

Keith Thompson said:
Then we need to make it very clear to J Random Newbie that C doesn't
support pass by reference as a languag construct.
Right.

Don't say that the
"*" means pass by reference; say that the "*" means you're passing a
pointer value, which can be used to do the equivalent of pass by
reference.

To a newbie, the two statements can seem equivalent, especially if he
doesn't know what "pass by reference" is and has to go and look it up. Why
use the phrase at all, when it doesn't add any value?
Until J Random Newbie is able to hold that entire complex
thought in his head, he's not going to be able to be an effective C
programmer.

We can ease that journey by not giving him useless junk to remember.
Pass by reference is a very common and useful programming technique,
one that C supports quite well (though arguably a little more clumsily
than some other languages do).

I know what you're saying, but I think that's an own-foot-shooting way to
say it.
 
A

aegis

Jordan said:
And uses it extensively in the standard library:

math.h: frexp, modf,
stdio.h: fscanf (and friends), fprintf (and friends, for %n), fgetpos,
fsetpos,
stdlib.h: strtol (and friends),
time.h: mktime, time, asctime, asctime, ctime, gmtime, localtime

Possibly others i've missed from later standards (this was written by
grepping c89 for (.*\*.*) and manually selecting the functions that use
this mechanism.

This is suppose to be suggestive of C supporting
pass by reference? Are you kidding? Chapter and verse
please!
 
R

Richard Heathfield

Joe Wright said:
Jordan said:
[...] time() takes its argument by reference [as do many other
functions in time.h, for historical reasons] You are conflating a
generic concept with a specific feature C++ has that C does not.

I may have to look this up because an exact example eludes me right now.

You needn't bother - Jordan is wrong about time() which does not in fact
take its argument by reference. It takes its parameter by value. The
argument is evaluated, and that value passed to time(). It's just like any
other parameter-taking C function in that regard.
 
R

Richard Heathfield

Joseph Dionne said:
No way! '&' is a bit wise logical AND, to get the contents of a pointer,
one codes it as "&pointer" not "& pointer" which will generate an error.

Case closed, I think, as far as Mr Dionne is concerned.

Troll, idiot, or newbie. In this case, I don't really see the point in
trying to distinguish between them.
 
R

Richard Heathfield

Joseph Dionne said:
Look who is being humorous. Sir, in you example, "buf" will never change
is
memory value, even *if* you pass it by reference, "& buf".

That isn't passing buf by reference. It's passing the address of buf by
value.
 
K

Keith Thompson

Joseph Dionne said:
Keith said:
Joseph Dionne said:
No way! '&' is a bit wise logical AND, to get the contents of a
pointer, one codes it as "&pointer" not "& pointer" which will
generate an error.
[...]
There are two "&" operators in C, a binary operator (taking two
operand) that performs a bitwise logical AND, and a unary operator
(taking one operand) that yields the address of its operand.
The expressions "&pointer" and "& pointer" are precisely equivalent.
The amount of whitespace between the operator and the operand is not
significant. The compiler is able to distinguish between unary and
binary "&" by the context in which it appears.
The unary "&" operator doesn't yield the contents of a pointer; it
yields the address of an object. [...]
I suggest you read a good C textbook (K&R2 is very good). You have a
lot of things to unlearn.
(Or if you're doing this deliberately, please please please go away;
it's not amusing.)

It is just as amusing as you assertion, backed by no well known
authority of the c language specification, that c does not support
pass by reference.

I didn't say that C doesn't support pass by reference. I said that C
doesn't *directly* support pass by reference, but that a programmer
can *implement* pass by reference using the features of the language.
Your continued reference to 'object' during replies proves my assertions --
recent OOP development (decades ago implemented recent, I mean) have
obfuscated the meaning of "pass by reference". I suspect this is due
mostly to the declining software knowledge, and skills, that has again
raised the acclaim and praise for "garbage collection" languages.

What does my use of the term "object" have to do with anything? The C
standard defines an "object" as a "region of data storage in the
execution environment, the contents of which can represent values"
(C99 3.14). I wasn't using the term in the sense of object-oriented
programming. (The C++ standard's definition of "object" is similar.)
Show me any authority that shares you "opinion" that c does not "pass
by reference".

C99 6.5.2.2p4:

An argument may be an expression of any object type. In preparing
for the call to a function, the arguments are evaluated, and each
parameter is assigned the value of the corresponding argument. 79)

Footnote 79:

A function may change the values of its parameters, but these
changes cannot affect the values of the arguments. On the other
hand, it is possible to pass a pointer to an object, and the
function may change the value of the object pointed to. A
parameter declared to have array or function type is adjusted to
have a pointer type as described in 6.9.1.

Which is what I've been saying all along.
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:

To a newbie, the two statements can seem equivalent, especially if he
doesn't know what "pass by reference" is and has to go and look it up. Why
use the phrase at all, when it doesn't add any value?


We can ease that journey by not giving him useless junk to remember.


I know what you're saying, but I think that's an own-foot-shooting way to
say it.

Ok, I see your point. I suppose it depends on the kind of newbie.

Personally, I learned Pascal before I learned C, so I already knew
about pass-by-value and pass-by-reference. From that perspective,
understanding that passing a pointer is the way to emulate
pass-by-reference in C was very important. Things might be different
for a non-programmer learning C.

On the other hand, I'm not sure that C is a good choice for a first
language. In any case, a programmer should *eventually* understand
the concepts of pass-by-value and pass-by-reference. And I tend to
think that it's at least as important to use C (or any language) as a
vehicle for learning about programming as it is to learn the details
of the language itself.
 
N

Netocrat

]
I didn't say that C doesn't support pass by reference. I said that C
doesn't *directly* support pass by reference, but that a programmer
can *implement* pass by reference using the features of the language.
[...]
C99 6.5.2.2p4:

An argument may be an expression of any object type. In preparing
for the call to a function, the arguments are evaluated, and each
parameter is assigned the value of the corresponding argument. 79)

Footnote 79:

A function may change the values of its parameters, but these
changes cannot affect the values of the arguments. On the other
hand, it is possible to pass a pointer to an object, and the
function may change the value of the object pointed to. A
parameter declared to have array or function type is adjusted to
have a pointer type as described in 6.9.1.

Which is what I've been saying all along.


- in both debates it's often argued that "C does not support the
concept directly, but supports its implementation trivially". In the case
of multi-dimensional arrays, the Standard contradicts this argument since
it explicitly describes a C array declared with more than one index as
multi-dimensional.

In the case of pass by reference, the opposite seems to be true: both
the Standard and its predecessor, K&R, explicitly describe C's parameter
passing as by value.

I think it's reasonable though to refer to C's simulation of this concept
as "pass by reference" (quote marks included) as a shortcut for a longer
expression similar to "pass the object's address to the function so that
the object's value may be modified", when it's clear that the writer is
not referring to the concept as supported more directly by languages like
C++ and Pascal.
 
N

Netocrat

On Sun, 08 Jan 2006 08:59:41 +0000, Netocrat wrote:
[slip-of-the-finger posted an article still being drafted; try again]
]
I didn't say that C doesn't support pass by reference. I said that C
doesn't *directly* support pass by reference, but that a programmer can
*implement* pass by reference using the features of the language.

You've written something very similar in the past about multidimensional
arrays. On that occasion I disagreed because the Standard contradicts it.
On this occasion, I agree with your analysis as it seems to be supported
by both the Standard and K&R.

[omit wafflier wordage of draft post]
 
N

nelu

Keith said:
Then we need to make it very clear to J Random Newbie that C doesn't
support pass by reference as a languag construct. Don't say that the
"*" means pass by reference; say that the "*" means you're passing a
pointer value, which can be used to do the equivalent of pass by
reference. Until J Random Newbie is able to hold that entire complex
thought in his head, he's not going to be able to be an effective C
programmer.
http://publib.boulder.ibm.com/infoc...pic=/com.ibm.cics.ts.doc/dfhp3/dfhp3b0037.htm
Now, the title of the article refers to both C and C++ so I'm not sure
about it but they say: "Passing a pointer is also known as passing a
value by reference.". But then what would be the difference between *
and & in C++?
 
C

Chuck F.

Nelu said:
.... snip ...

type is int, you send a pointer to int, int is changed in the function. The functions doesn't tell you that it needs an int to change an int. It tells you it needs a pointer to int that it can't change.
Although you can consider &int a reference to an int, the function doesn't ask for a reference but for a type which is called "pointer to int" and what you send is the value of a pointer to int type.

Please fix your right hand margin. Lines must never exceed 80
chars according to standards, and prefereably should not exceed
about 65 or so (to allow for quote marks).

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top