473,804 Members | 2,104 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 6933

Jordan Abel wrote:
On 2006-01-07, Joe Wright <jo********@com cast.net> 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.


Yes it does. 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.


You are the one doing the conflating. C supports pass by value
/only/. See "call by value: 3.3.2.2" from c89.
Specifically paragraph two. Which states:

"If the expression that denotes the called function has a type that
includes a prototype, the number of arguments shall agree with the
number of parameters. /Each argument shall have a type such that [its
value] may be assigned to an object with the unqualified version of the
type of its corresponding parameter./"

Passing by reference has to do with passing around actual
containers or in C's case, objects. 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".

--
aegis

Jan 9 '06 #121
Jordan Abel wrote:
On 2006-01-08, Mark McIntyre <ma**********@s pamcop.net> wrote:
On Sun, 08 Jan 2006 04:51:47 GMT, in comp.lang.c , Joseph Dionne
<jd*****@hotm ail.com> wrote:

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.


Well, jackass, it doesn't. It supports pass by value, and you may pass
the value of a pointer to emulate pass by reference.

That's not "emulating pass by reference", that is passing by reference.
The fact that the language doesn't have special syntax for it doesn't
mean it's suddenly a different programming technique.

Here, here! Just because K&R describes the process of how a function is
called, i.e. copies are placed on the stack before the function call, does not
imply c is only pass by value.

Under the hood, Pascal and c++ do exactly the same technique. However, I will
concede the syntax in Pascal and c++ supports pass by reference, making it
easier for developers to not "think" about what they code.

I guess learning assembler as my first language has endeared me to the c
language and I *might* be hypersensitive.
Jan 9 '06 #122
M.B

Jordan Abel wrote:
On 2006-01-08, Mark McIntyre <ma**********@s pamcop.net> wrote:
On Sun, 08 Jan 2006 04:51:47 GMT, in comp.lang.c , Joseph Dionne
<jd*****@hotmai l.com> wrote:
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.


Well, jackass, it doesn't. It supports pass by value, and you may pass
the value of a pointer to emulate pass by reference.


That's not "emulating pass by reference", that is passing by reference.
The fact that the language doesn't have special syntax for it doesn't
mean it's suddenly a different programming technique.


Ok if your argument is correct,
do you say C has (not emulate) linked lists,trees,... .because we can
create them iusing C
In that case C supports object orientation also.

Jan 9 '06 #123
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.
Jan 9 '06 #124
On 2006-01-09, aegis <ae***@mad.scie ntist.com> wrote:

Jordan Abel wrote:
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.


This is suppose to be suggestive of C supporting
pass by reference? You will have to try harder than that.
Chapter and verse please!


You just said the same thing, in response to the same message, three
different times, in three different ways. Did you forget about your two
earlier posts?
Jan 9 '06 #125
On 2006-01-09, M.B <mb*******@gmai l.com> wrote:

Jordan Abel wrote:
On 2006-01-08, Mark McIntyre <ma**********@s pamcop.net> wrote:
> On Sun, 08 Jan 2006 04:51:47 GMT, in comp.lang.c , Joseph Dionne
> <jd*****@hotmai l.com> wrote:
>
>>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.
>
> Well, jackass, it doesn't. It supports pass by value, and you may pass
> the value of a pointer to emulate pass by reference.


That's not "emulating pass by reference", that is passing by reference.
The fact that the language doesn't have special syntax for it doesn't
mean it's suddenly a different programming technique.


Ok if your argument is correct,
do you say C has (not emulate) linked lists,trees,... .because we can
create them iusing C
In that case C supports object orientation also.


C neither supports nor does not support it, because it's not a language
feature, it's a programming technique. THE SAME is true for pass by
reference.

"emulate" isn't the proper word. it's something the programmer _can
implement_ in C. Just like a million other things.
Jan 9 '06 #126
Jordan Abel wrote:
On 2006-01-09, M.B <mb*******@gmai l.com> wrote:

Jordan Abel wrote:
On 2006-01-08, Mark McIntyre <ma**********@s pamcop.net> wrote:
> On Sun, 08 Jan 2006 04:51:47 GMT, in comp.lang.c , Joseph Dionne
> <jd*****@hotmai l.com> wrote:
>
>>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.
>
> Well, jackass, it doesn't. It supports pass by value, and you may pass
> the value of a pointer to emulate pass by reference.

That's not "emulating pass by reference", that is passing by reference.
The fact that the language doesn't have special syntax for it doesn't
mean it's suddenly a different programming technique.


Ok if your argument is correct,
do you say C has (not emulate) linked lists,trees,... .because we can
create them iusing C
In that case C supports object orientation also.


C neither supports nor does not support it, because it's not a language
feature, it's a programming technique.


No; it's a language feature; that's the use for which the term was
created, and the use it still retains. A language has "call by
reference" when a call f(x) can change the value of the variable
x as a result of assignments in f to the formal parameter corresponding
to x.

It is a point of distinction between Pascal (I speak of the non-Delphic
variety) and C (K&R, 89, and 99) that you can do this in Pascal (by
declaring the formal parameter to be `VAR`) and you can't do it in C
(and you can do it in C++).

You can certainly get a similar /effect/ in C. You can use an argument
type of pointer-to-Spoo instead of Spoo, replace all references to the
formal argument spoo by *spoo, and insert the necessary & in all the calls.
The fact that you have to /do/ this, consistently, everywhere, is part
of the reason for saying that C doesn't "support" (I would say "have",
as "support" is such an unnecessarily vulnerable word) call-by-reference.

The term "call-by-reference" is part of a family of terms [1] describing
the ways in which parameters may pass into and out of procedures. That
any of these ways might /also/ be done explicitly by additional code
in the language - as a technique as opposed to a feature - doesn't make
the terms valueless and the distinctions they draw irrelevant.

[1] Call by value, call by result, call by value-result, call by reference,
call by value, call by name. "Pass by" would have been better ...

--
Chris "believes seventeen improbable things before coffee" Dollin
Seventeen, forty-two - what else is there?
Jan 9 '06 #127
On 2006-01-09, Chris Dollin <ke**@hpl.hp.co m> wrote:
Jordan Abel wrote:
On 2006-01-09, M.B <mb*******@gmai l.com> wrote:

Jordan Abel wrote:
On 2006-01-08, Mark McIntyre <ma**********@s pamcop.net> wrote:
> On Sun, 08 Jan 2006 04:51:47 GMT, in comp.lang.c , Joseph Dionne
> <jd*****@hotmai l.com> wrote:
>
>>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.
>
> Well, jackass, it doesn't. It supports pass by value, and you may pass
> the value of a pointer to emulate pass by reference.

That's not "emulating pass by reference", that is passing by reference.
The fact that the language doesn't have special syntax for it doesn't
mean it's suddenly a different programming technique.

Ok if your argument is correct,
do you say C has (not emulate) linked lists,trees,... .because we can
create them iusing C
In that case C supports object orientation also.
C neither supports nor does not support it, because it's not a language
feature, it's a programming technique.


No; it's a language feature; that's the use for which the term was
created, and the use it still retains. A language has "call by
reference" when a call f(x) can change the value of the variable
x as a result of assignments in f to the formal parameter corresponding
to x.


What does the use for which the term "call by reference" was created
have to do with the original poster's use of the term "pass by
reference"? [which, i might add, was completely clear in context.]
The term "call-by-reference" is part of a family of terms [1] describing
the ways in which parameters may pass into and out of procedures. That
any of these ways might /also/ be done explicitly by additional code
in the language - as a technique as opposed to a feature - doesn't make
the terms valueless and the distinctions they draw irrelevant.

[1] Call by value, call by result, call by value-result, call by reference,
call by value, call by name. "Pass by" would have been better ...


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

Jordan Abel wrote:
> On 2006-01-08, Mark McIntyre <ma**********@s pamcop.net> wrote:
> > On Sun, 08 Jan 2006 04:51:47 GMT, in comp.lang.c , Joseph Dionne
> > <jd*****@hotmai l.com> wrote:
> >
> >>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.
> >
> > Well, jackass, it doesn't. It supports pass by value, and you may
> > pass the value of a pointer to emulate pass by reference.
>
> That's not "emulating pass by reference", that is passing by
> reference. The fact that the language doesn't have special syntax for
> it doesn't mean it's suddenly a different programming technique.

Ok if your argument is correct,
do you say C has (not emulate) linked lists,trees,... .because we can
create them iusing C
In that case C supports object orientation also.

C neither supports nor does not support it, because it's not a language
feature, it's a programming technique.


No; it's a language feature; that's the use for which the term was
created, and the use it still retains. A language has "call by
reference" when a call f(x) can change the value of the variable
x as a result of assignments in f to the formal parameter corresponding
to x.


What does the use for which the term "call by reference" was created
have to do with the original poster's use of the term "pass by
reference"? [which, i might add, was completely clear in context.]


Because "pass by reference" means - as far as I am aware - "call by
reference" (and vice versa). "call by reference" is the earlier term,
the one which comes to my fingers; sorry if I've injected confusion
by using one rather than t'other.
The term "call-by-reference" is part of a family of terms [1] describing
the ways in which parameters may pass into and out of procedures. That
any of these ways might /also/ be done explicitly by additional code
in the language - as a technique as opposed to a feature - doesn't make
the terms valueless and the distinctions they draw irrelevant.

[1] Call by value, call by result, call by value-result, call by
[reference,
call by value, call by name. "Pass by" would have been better ...


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?

--
Chris "believes seventeen improbable things before coffee" Dollin
Seventeen, forty-two - what else is there?
Jan 9 '06 #129
In article <sl************ **********@rand om.yi.org> Jordan Abel <ra*******@gmai l.com> writes:
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.


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) and pass by value), you can not see from the
line:
routine(argumen t)
whether the argument is passed by reference or by value. Note also
that in some languages (Fortran) pass by reference is indistinguishab le
from pass by value-return (i.e. the value of the argument is passed to
the routine and when the routine terminates a new value is assigned to
the argument).

In C pass by references is only performed with array's (but it is actually
simulated by The Rule).
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jan 9 '06 #130

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
3466
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
2676
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
1880
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
9715
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
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10352
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
10354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6867
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
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
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
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...

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.