Hi.
In the snippet of code below, I'm trying to understand why when the
struct dirent ** namelist
is declared with "file" scope, I don't have a problem freeing the
allocated memory. But when the struct is declared in main (block scope)
it will segfault when passing namelist to freeFileNames() .
Since this seems to be just a matter of my understanding scope and
pointer parameter passing better, I only included what thought to be
relevant code. I'll happily provide compilable code if deemed necessary.
Please see commented lines:
struct dirent **namelist; /* file scope works */
int main(void)
{
/* struct dirent **namelist */ /* block scope doesn't work */
int n;
n = getFileNames(H5 DIR, namelist); /* included from mylib.h */
freeFileNames(n amelist, n); /* included from mylib.h */
return 0;
}
Thank you very much for your comments,
Dieter
Jan 6 '06
165 6927
On 2006-01-07, Keith Thompson <ks***@mib.or g> wrote: Richard Heathfield <in*****@invali d.invalid> writes: Keith Thompson said: (I'm 100% certain that I'm not telling you anything you don't already know; the point is to figure out just where we disagree.)
I'm for whatever makes C easier to understand. Heaven knows it's hard enough already, without making it harder.
When you say "pointers" and "pass by reference", yes, OF COURSE I know what you mean. But J Random Newbie over there, who already has a hundred new concepts buzzing around his head right now and who does not fully understand what pointers *are* yet, let alone what they are *for*, is going to think "pointers, right, the little * thing, okay, pass-by-reference, right, * means pass by reference, which means I can update it, great..." and he is going to think he understands things a little better - and then he is (eventually) going to wonder why this:
void allocstr(char *s, size_t n) { s = malloc(n);
if(s == NULL) abort(); }
doesn't seem to do what he wants.
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.
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).
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.
Keith Thompson wrote: Joseph Dionne <jd*****@hotmai l.com> writes:
Keith Thompson wrote:
[...]
Suppose I have some Pascal code that uses pass-by-reference (no guarantee that I've gotten the syntax correct): function increment(var n: integer) begin n := n + 1; end; ... n : integer; n := 42; increment(n); The translation to C is straightforward : void increment(int *n) { (*n)++; }
...
int n; n = 42; increment(&n);
It has been twenty years since I coded pascal, I will dispute your pascal code other than to ask is "increment( 1)" syntactically correct and will the pascal increment() increment memory address 0x0001?
<OT> No, that's not a problem. In Pascal, for an ordinary (pass-by-value) parameter, the argument can be any expression of the appropriate type. For a var (pass-by-reference) parameter, the actual parameter must be a variable. </OT>
In your c code, increment(&n) *is* pass by reference simply because the original memory can be altered, but I can code an increment() function for pass by value too.
The call increment(&n) passes the *value* of &n. This is used to implement the equivalent of passing n by reference, something that the C language doesn't directly support.
The *value* of a long is memory big enough to hold a define number data bits,
same for the *value* of int, and char variables. The *value* of a pointer
variable is the number of bits needed to hold a memory address, a special
purpose "integer" if you will. Admittedly, this duality is confusing.
I believe the confusion, and abuse of pointers derives from the duality in
nature of a pointer. Sure, one can "read/write" to a pointer, the it is the
"magic" of automatic dereferencing by the compiler. The pointer variable only
contains a memory address.
That is not to be confused that a pointer's value, the memory address of the
data to which it points, can be passed by reference or by value, regardless of
the syntax by which this is done.
Keith Thompson wrote: Richard Heathfield <in*****@invali d.invalid> writes:
Keith Thompson said:
(I'm 100% certain that I'm not telling you anything you don't already know; the point is to figure out just where we disagree.)
I'm for whatever makes C easier to understand. Heaven knows it's hard enough already, without making it harder.
When you say "pointers" and "pass by reference", yes, OF COURSE I know what you mean. But J Random Newbie over there, who already has a hundred new concepts buzzing around his head right now and who does not fully understand what pointers *are* yet, let alone what they are *for*, is going to think "pointers, right, the little * thing, okay, pass-by-reference, right, * means pass by reference, which means I can update it, great..." and he is going to think he understands things a little better - and then he is (eventually) going to wonder why this:
void allocstr(char *s, size_t n) { s = malloc(n);
if(s == NULL) abort(); }
doesn't seem to do what he wants.
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.
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).
Yes it does! The "language construct" is simply not one character, but two "&n".
Joe Wright wrote: Joseph Dionne wrote:
Joe Wright wrote:
M.B wrote:
Richard Heathfield wrote:
> M.B said: > > >> its a pass by value v/s pass by reference issue > > > > > Since C doesn't have pass by reference, it is clear to me that you are > talking nonsense. > >
I presume u area c++ guy to talk like this. pass by reference is a generic term for ur info. C iimplements this by pointers.
M.B., you clearly don't know what 'pass by reference' means. C doesn't do it, even with pointers. Whether C++ does it is of no interest here.
Ah, newbies!
Definition of 'pass by reference'
http://publib.boulder.ibm.com/infoce...ef/cplr233.htm
Not a newbie. The above URL is not a definition of 'pass by reference' but an description of how to fake it.
Whatever!
On Sat, 07 Jan 2006 21:42:11 GMT, in comp.lang.c , Joseph Dionne
<jd*****@hotmai l.com> wrote: We all know about emulating pass by reference using pointers. If you're talking about something else, can you provide an example?
I disagree with your assertion that c does not support pass by reference.
snip"pass by reference', since the creation of c++ and other OOP languages, has been a new "meaning".
This may be so, but the current meaning is the one that is current.
Once upon a time gay meant happy, and eye meant egg. You can see there
is opportunity for considerable confusion by using an archaic meaning.
Mark McIntyre
--
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Joseph Dionne <jd*****@hotmai l.com> writes:
[...] The *value* of a long is memory big enough to hold a define number data bits, same for the *value* of int, and char variables.
Ok. (One could question whether values are made of bits, but let's
not get into that.)
The *value* of a pointer variable is the number of bits needed to hold a memory address, a special purpose "integer" if you will.
No, I won't. Pointers are not integers. Pointers are pointers.
Admittedly, this duality is confusing.
The value of a pointer variable is a pointer value, also known as an
address. I don't know what duality you're referring to. Pointers and
integers are different things. One of the operations you can perform
on a pointer, dereferencing, happens to give you the value of what it
points to. As long as you keep this straight, it shouldn't be all
that confusing. I believe the confusion, and abuse of pointers derives from the duality in nature of a pointer. Sure, one can "read/write" to a pointer, the it is the "magic" of automatic dereferencing by the compiler. The pointer variable only contains a memory address.
What automatic dereferencing are you referring to? In C, pointers are
not implicitly dereferenced; that's what the unary "*" operator is
for. Can you provide an example, in C code, of what you're talking
about?
That is not to be confused that a pointer's value, the memory address of the data to which it points, can be passed by reference or by value, regardless of the syntax by which this is done.
The value of a pointer (an address) can be passed *by value*. You can
effectively pass a pointer value by reference if you pass a
pointer-to-pointer; then you're passing the pointer-to-pointer by
value.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Joseph Dionne wrote: Keith Thompson wrote:
Joseph Dionne <jd*****@hotmai l.com> writes:
Keith Thompson wrote:
[...]
Suppose I have some Pascal code that uses pass-by-reference (no guarantee that I've gotten the syntax correct): function increment(var n: integer) begin n := n + 1; end; ... n : integer; n := 42; increment(n); The translation to C is straightforward : void increment(int *n) { (*n)++; }
...
int n; n = 42; increment(&n);
It has been twenty years since I coded pascal, I will dispute your pascal code other than to ask is "increment( 1)" syntactically correct and will the pascal increment() increment memory address 0x0001? <OT> No, that's not a problem. In Pascal, for an ordinary (pass-by-value) parameter, the argument can be any expression of the appropriate type. For a var (pass-by-reference) parameter, the actual parameter must be a variable. </OT>
In your c code, increment(&n) *is* pass by reference simply because the original memory can be altered, but I can code an increment() function for pass by value too. The call increment(&n) passes the *value* of &n. This is used to implement the equivalent of passing n by reference, something that the C language doesn't directly support.
The *value* of a long is memory big enough to hold a define number data bits, same for the *value* of int, and char variables. The *value* of a pointer variable is the number of bits needed to hold a memory address, a special purpose "integer" if you will. Admittedly, this duality is confusing.
I believe the confusion, and abuse of pointers derives from the duality in nature of a pointer. Sure, one can "read/write" to a pointer, the it is the "magic" of automatic dereferencing by the compiler. The pointer variable only contains a memory address.
That is not to be confused that a pointer's value, the memory address of the data to which it points, can be passed by reference or by value, regardless of the syntax by which this is done.
And, the whole topic gets even more confusing because of privative arrays,
i.e. int ii[5]. Here ii is "handled" as *both* a pointer and real memory
storage. Obviously one does not assign to the ii[] with "ii = 0" but uses a
subscript to isolate one int's actually memory storage address, i.e. "ii[0] =
0". However to pass the ii[] to a function(int *), the call can be simply
coded as "function(( int *)ii)" with the cast only needed to fake out
prototyping compilers.
Mark McIntyre wrote: On Sat, 07 Jan 2006 21:42:11 GMT, in comp.lang.c , Joseph Dionne <jd*****@hotmai l.com> wrote:
We all know about emulating pass by reference using pointers. If you're talking about something else, can you provide an example?
I disagree with your assertion that c does not support pass by reference.
snip
"pass by reference', since the creation of c++ and other OOP languages, has been a new "meaning".
This may be so, but the current meaning is the one that is current. Once upon a time gay meant happy, and eye meant egg. You can see there is opportunity for considerable confusion by using an archaic meaning. Mark McIntyre
So when enough people refer to the period from sunrise to sunshine (the day)
as night, we all need to use their incorrect definition?
Joseph Dionne <jd*****@hotmai l.com> writes: Keith Thompson wrote:
[...] 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. 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).
Yes it does! The "language construct" is simply not one character, but two "&n".
You're making Richard's point that we shouldn't talk about pass by
reference in C.
&n is simply an expression consisting of a unary "&" operator and an
identifier; it yields the address of n. *p is an expression
consisting of a unary "*" operator and an identifier; it dereferences
the pointer p and yields the value of the object it points to. These
constructs can be combined to implement the programming technique
known as pass by reference.
If you want to think of
void increment(int *p) ... increment(&n);
as "pass by reference", that's fine -- but you *must* understand that
it's built from lower-level constructs.
Note that a function with exactly the same interface:
void check_for_null( int *p)
{
if (p == NULL) {
fprintf(stderr, "p is a null pointer\n");
}
}
has nothing to do with pass by reference; it simply takes a pointer
parameter which is passed by value. Pointers aren't magical; they're
simply types with a particular set of operations that can be applied
to them.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
On Sat, 07 Jan 2006 23:23:22 GMT, in comp.lang.c , Joseph Dionne
<jd*****@hotmai l.com> wrote: Mark McIntyre wrote: On Sat, 07 Jan 2006 21:42:11 GMT, in comp.lang.c , Joseph Dionne <jd*****@hotmai l.com> wrote:
"pass by reference', since the creation of c++ and other OOP languages, has been a new "meaning".
This may be so, but the current meaning is the one that is current.
So when enough people refer to the period from sunrise to sunshine (the day) as night, we all need to use their incorrect definition?
If you want to avoid confusion, and being thought of as a pompous ass,
yes IMHO. YMMV. HAND.
Mark McIntyre
--
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =---- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: mike420 |
last post by:
I think everyone who used Python will agree that its syntax is
the best thing going for it. It is very readable and easy
for everyone to learn. But, Python does not a have very good
macro capabilities, unfortunately. I'd like to know if it may
be possible to add a powerful macro system to Python, while
keeping its amazing syntax, and if it could be possible to
add Pythonistic syntax to Lisp or Scheme, while keeping all
of the...
|
by: Minti |
last post by:
I was reading some text and I came across the following snippet
switch('5')
{
int x = 123;
case '5':
printf("The value of x %d\n", x);
break;
}
|
by: Code Raptor |
last post by:
Folks,
I am hitting a segfault while free()ing allocated memory - to make it
short, I have a linked list, which I try to free node-by-node. While
free()ing the 28th node (of total 40), I hit a segfault. This is legacy
code. I tried debugging this problem, and am not able to come up with a
valid reason for this. Following function is being used to free:
void DBFreePUF (DBPUFRec *userp)
{
|
by: G Patel |
last post by:
I've seen some code with extern modifiers in front of variables
declared inside blocks. Are these purely definitions (no definition)
or are they definitions with static duration but external linkage?
Not much on this in the FAQ or tutorials.
|
by: seamoon |
last post by:
Hi,
I'm doing a simple compiler with C as a target language. My language uses
the possibility to declare variables anywhere in a block with scope to the
end of the block. As I remembered it this would be easily translated to C,
but it seems variable declaration is only possible in the beginning of a
block in C. Any suggestions how to get around this?
| |
by: BT |
last post by:
Ok, for a school assignment we have to use a pointer for an array of
ints, intstead of the usual X way, it compiles fine but when i run
it I am getting a seg fault that i can't figure out how to fix. It
occurs at this line:
*d = rand() % 99 + 1
Here is the code for the first part of the program, the line that
causes the seg fault is
|
by: nobrow |
last post by:
Okay ... Im out of practice. Is it not possible to have a 2D array
where each column is of a different type, say an int and a struct*? The
following seg faults for me and I cant figure out what I need to
change.
Thanks.
#include <malloc.h>
#include <string.h>
|
by: Neal Becker |
last post by:
One thing I sometimes miss, which is common in some other languages (c++),
is idea of block scope. It would be useful to have variables that did not
outlive their block, primarily to avoid name clashes. This also leads to
more readable code. I wonder if this has been discussed?
|
by: somebody |
last post by:
There are two files below named search.c and search.h.
In the for loop in search.c, the for loop never exits,
even if mystruct.field1 has no match. Instead of
exiting the for loop it keeps going until it segfaults.
This seems to be related to the strcmp with the NULL
value. There are 2 comments below that indicate
the segfaults. I guess the question is, when there
is no match, how to I detect that and return without
a segfault?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |