473,804 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

segfault w/ block, but not file scope

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-09, Keith Thompson <ks***@mib.or g> wrote:

[context is the term "pass by reference]
What do you call the corresponding programming technique? (Ideally,
I'd like to see a pity and language-independent term, just as we use
the term "linked list" regardless of the implementation language.)

If some other language, say C+++ (sic),
Or, say, lisp.
had built-in support for linked lists, would you insist on not using
the term "linked list" in the context of a C program?


Apparently not.
Jan 10 '06 #151
On 2006-01-09, aegis <ae***@mad.scie ntist.com> wrote:

Jordan Abel wrote:
On 2006-01-08, aegis <ae***@mad.scie ntist.com> wrote:
> Passing by reference has to do with passing around actual containers
> or in C's case, objects.


I disagree.
> We don't do that in C. We instead pass the
> values stored in objects around to functions. The standard is clear
> here and supports absolutely diddly squat in regards to a concept
> called "pass by reference".


The language itself supports absolutely squat in regards to another
concept called "linked list", or "binary tree", or any number of other
things, which means absolutely squat to people who use those concepts
anyway in C regardless.


What a terrible comparison. The actual passing mechanism supported
by C is defined by the standard. Because it is a mechanism of the
language. Concepts such as 'linked list' and 'binary tree' are
implemented with the language. Not /into/ the language. So you
compare to a concept not associated with making up the actual
language. Call by value or pass by value is clearly outlined in
section 3.3.2.2 of c89. Which section describes this so called
concept 'pass by reference' or 'call by reference'? I suspect
there is no section as there is no mechanism of such implemented
into the language.


yet there _is_ a technique, which has been described in detail,
which can be implemented with C, and which is used extensively in
the standard library, and you have repeatedly failed to put forth a
better name for it than "pass by reference".
Jan 10 '06 #152
In article <dp**********@m alatesta.hpl.hp .com> Chris Dollin <ke**@hpl.hp.co m> writes:
Dik T. Winter wrote:
The distinction between passing by reference and passing by value is
that at the callers end you can not distinguish the two, it is at
the callees end that the two are distinguished. So in Algol 60
(which has proper pass by reference (actually pass by name, but the
distinction is minor)


Surely not, as Jensen's Device illustrates. (And trying to compile
it.)


In the context the difference is minor. And I know exactly how call
by name was compiled on the system I did use.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jan 10 '06 #153
In article <dp***********@ pc-news.cogsci.ed. ac.uk> ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <Is********@cwi .nl>, Dik T. Winter <Di********@cwi .nl> wrote:
(which has proper pass by reference (actually pass by name, but the
distinction is minor) and pass by value)
The distinction is minor when the actual parameter is a variable, but
not when it is an expression.


I know all that. I have quite a bit of experience with Algol 60 (it was
the first computer language I ever used). My "minor" was intended in
context.

Anyhow the major distinction between call by value, reference, name,
value-return, or whatever is not at the callers side. It is at the
callees side. Even in Algol 68 you could write:
function(argume nt)
without knowing whether "argument" could be changed or not, unless you
looked at the definition of "function". In this sense C is an improvement
on Algol 68; if I write:
function(argume nt)
I am sure that argument will not change, at most what argument points to.
In Algol 68 this is quite a bit more subtle.
not when it is an expression. In the latter case you have something
like a closure (traditionally called a "thunk" in this context).

Or was your use of "proper" meant to imply that pass by reference
ought to work like this?

-- Richard

--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jan 10 '06 #154

Jordan Abel wrote:
On 2006-01-09, aegis <ae***@mad.scie ntist.com> wrote:

Jordan Abel wrote:
On 2006-01-08, aegis <ae***@mad.scie ntist.com> wrote:
> Passing by reference has to do with passing around actual containers
> or in C's case, objects.

I disagree.

> We don't do that in C. We instead pass the
> values stored in objects around to functions. The standard is clear
> here and supports absolutely diddly squat in regards to a concept
> called "pass by reference".

The language itself supports absolutely squat in regards to another
concept called "linked list", or "binary tree", or any number of other
things, which means absolutely squat to people who use those concepts
anyway in C regardless.


What a terrible comparison. The actual passing mechanism supported
by C is defined by the standard. Because it is a mechanism of the
language. Concepts such as 'linked list' and 'binary tree' are
implemented with the language. Not /into/ the language. So you
compare to a concept not associated with making up the actual
language. Call by value or pass by value is clearly outlined in
section 3.3.2.2 of c89. Which section describes this so called
concept 'pass by reference' or 'call by reference'? I suspect
there is no section as there is no mechanism of such implemented
into the language.


yet there _is_ a technique, which has been described in detail,
which can be implemented with C, and which is used extensively in
the standard library, and you have repeatedly failed to put forth a
better name for it than "pass by reference".


My original post was in reply to your opposition of the claim
that C does not support 'pass by reference'. I thought it was clear
that this was about a mechanism of the language and not
something you implement with the language. If instead,
you mean to say the value of a pointer object can
also be called a reference and that when passing it to
a function therefore qualifies the language as supporting
'pass by reference', then I think that you are confused. What
you really want to say is 'pass a reference'. I know this is
an international forum so English may not be your native
language but do you understand the difference?
--
aegis

Jan 10 '06 #155
Jordan Abel wrote:
On 2006-01-09, Chris Dollin <ke**@hpl.hp.co m> wrote:
the use "pass by" instead of "call by" distinguishes the programming
technique from the language feature.


That's interesting distinction, but the only place I ever seen it made
is in your post - have you a (preferably online) reference to update me?


I coined it just now because I think the programming technique deserves
a name. What would you call the technique used in the list of standard
library functions i named upthread?


If you could be more explicit about the list (I looked back a way but
found it not), then I could offer a name.

I note that the pass by / call by distinction you offer is idiosyncratic
to you.

--
Chris "believes seventeen improbable things before coffee" Dollin
Seventeen, forty-two - what else is there?
Jan 10 '06 #156
Keith Thompson wrote:
Here are a couple of questions for those of you who insist that the
term "pass by reference" should apply only to a built-in language
feature:

What do you call the corresponding programming technique? (Ideally,
I'd like to see a pity and language-independent term, just as we use
the term "linked list" regardless of the implementation language.)
"passing a pointer". Or "indirectio n". Or "passing a reference". Or
even (as I'm an old fogey who defaults, as we have seen, to the term
"call by reference" for the language feature) "pass by reference",
but since that appears to be becomming the term for the feature, I'd
best change my habits.
If some other language, say C+++ (sic), had built-in support for
linked lists, would you insist on not using the term "linked list" in
the context of a C program?


No. I'd just say that C+++ "had" linked lists, and C didn't. (Actually
I'd be likely to say that they had/had not "lists", because my own
idiosyncracy is to think of "list" as /meaning/ linked list; these
funny psuedo-arrays that eg Java call "lists" always seem to me to
be misnamed. But I know that's Just Me.)

I'd try to be /clear/, in any case. "Well, C+++ has linked lists as
a built-in feature; C doesn't have them, but you can implement them,
or something pretty close."

--
Chris "believes seventeen improbable things before coffee" Dollin
(But the coffee machine is broken. Belief overload!)
Jan 10 '06 #157
M.B

Chris Dollin wrote:
Keith Thompson wrote:
Here are a couple of questions for those of you who insist that the
term "pass by reference" should apply only to a built-in language
feature:

What do you call the corresponding programming technique? (Ideally,
I'd like to see a pity and language-independent term, just as we use
the term "linked list" regardless of the implementation language.)
"passing a pointer". Or "indirectio n". Or "passing a reference". Or
even (as I'm an old fogey who defaults, as we have seen, to the term
"call by reference" for the language feature) "pass by reference",
but since that appears to be becomming the term for the feature, I'd
best change my habits.
If some other language, say C+++ (sic), had built-in support for
linked lists, would you insist on not using the term "linked list" in
the context of a C program?


No. I'd just say that C+++ "had" linked lists, and C didn't. (Actually
I'd be likely to say that they had/had not "lists", because my own
idiosyncracy is to think of "list" as /meaning/ linked list; these
funny psuedo-arrays that eg Java call "lists" always seem to me to
be misnamed. But I know that's Just Me.)

I'd try to be /clear/, in any case. "Well, C+++ has linked lists as
a built-in feature; C doesn't have them, but you can implement them,
or something pretty close."

Does this answer this long argument
http://c-faq.com/ptrs/passbyref.html
--
Chris "believes seventeen improbable things before coffee" Dollin
(But the coffee machine is broken. Belief overload!)


Jan 10 '06 #158
On 2006-01-10, aegis <ae***@mad.scie ntist.com> wrote:
My original post was in reply to your opposition of the claim that C
does not support 'pass by reference'. I thought it was clear that this
was about a mechanism of the language and not something you implement
with the language. If instead, you mean to say the value of a pointer
object can also be called a reference and that when passing it to a
function therefore qualifies the language as supporting 'pass by
reference', then I think that you are confused. What you really want
to say is 'pass a reference'.
yes - you "pass a reference" _by which reference_ you are thus passing
the object whose address is taken. thus "pass by [that] reference". the
'reference' or 'pointer' is something you are _using_ as a mechanism to
pass something, which is why the word "by" is used. The only difference
is whether you, the programmer, are doing it, vs the compiler doing it.
I know this is an international forum so English may not be your
native language but do you understand the difference?


I think _you_ are the one who is not looking at the actual meanings of
the words, and unjustifiably seeing the phrase "pass by reference" as
indivisible.
Jan 10 '06 #159
On 2006-01-10, Chris Dollin <ke**@hpl.hp.co m> wrote:
Keith Thompson wrote:
Here are a couple of questions for those of you who insist that the
term "pass by reference" should apply only to a built-in language
feature:

What do you call the corresponding programming technique? (Ideally,
I'd like to see a pity and language-independent term, just as we use
the term "linked list" regardless of the implementation language.)


"passing a pointer". Or "indirectio n". Or "passing a reference". Or
even (as I'm an old fogey who defaults, as we have seen, to the term
"call by reference" for the language feature) "pass by reference",
but since that appears to be becomming the term for the feature, I'd
best change my habits.
If some other language, say C+++ (sic), had built-in support for
linked lists, would you insist on not using the term "linked list" in
the context of a C program?


No. I'd just say that C+++ "had" linked lists, and C didn't. (Actually
I'd be likely to say that they had/had not "lists", because my own
idiosyncracy is to think of "list" as /meaning/ linked list; these
funny psuedo-arrays that eg Java call "lists" always seem to me to
be misnamed. But I know that's Just Me.)

I'd try to be /clear/, in any case. "Well, C+++ has linked lists as
a built-in feature; C doesn't have them, but you can implement them,
or something pretty close."


But saying "C doesn't have linked lists, this isn't C+++" in response to
EVERY mention of "linked list", even when it was clear that it wasn't
about a language feature, would be widely seen as a troll, wouldn't it?
Jan 10 '06 #160

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
34287
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...
18
3465
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; }
6
2484
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) {
12
2730
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.
7
7720
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?
7
1478
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
8
3209
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>
24
2675
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?
10
1879
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?
0
9714
marktang
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...
0
10599
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
jinu1996
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...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6863
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();...
0
5531
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...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.