473,804 Members | 2,215 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 6931

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.

--
aegis

Jan 9 '06 #141
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.or g> wrote:
Perhaps it would be clearer to say that you are "passing a reference"
rather than "passing by reference"?
No, because too many readers will assume "reference" refers to a
certain C++ language feature that C lacks.


Not being a C++ user, I was unaware of that. But I think it will be
hard to find a term that won't be misunderstood by some group, and I
am disinclined to allow C++ to distort our terminology so much.

-- Richard
Jan 9 '06 #142
Keith Thompson wrote:
Joseph Dionne <jd*****@hotmai l.com> writes:

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.


Does K&R really say that arguments are passed by placing their values
on "the stack"? I'd be a bit surprised by that, since the C standard
doesn't talk about a stack, and a number of real-world implementations
pass most arguments in registers.

The only references to a "stack" I find in K&R2 is their own stack
routines they devised for doing some calculator-type stuff.

Brian
Jan 9 '06 #143
On 9 Jan 2006 22:32:16 GMT, in comp.lang.c , ri*****@cogsci. ed.ac.uk
(Richard Tobin) wrote:
Not being a C++ user, I was unaware of that. But I think it will be
hard to find a term that won't be misunderstood by some group, and I
am disinclined to allow C++ to distort our terminology so much.


This is probably a case where we should avoid the confusion.
Look at the mess that "const" and sizeof('Q') creates...
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 =----
Jan 9 '06 #144
Default User wrote:
Keith Thompson wrote:

Joseph Dionne <jd*****@hotmai l.com> writes:


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.


Does K&R really say that arguments are passed by placing their values
on "the stack"? I'd be a bit surprised by that, since the C standard
doesn't talk about a stack, and a number of real-world implementations
pass most arguments in registers.


The only references to a "stack" I find in K&R2 is their own stack
routines they devised for doing some calculator-type stuff.

Brian


It astounds me to read some the post here, most being "off topic" or "don't do
that" for people seeking solutions to problems the responders have most likely
implemented.

It also astounds me to realize how many c programmers, a low level language,
have little to no understanding of how the c compiler works, some even
claiming the OS "jumps to main" which is completely false. It is apparent
many posters have little understanding of the structure of an executable
object created by the compiler, or how executable object loaders function.

I guess "in the old days" when we micro programmers has to write our own
program overlay loaders to work in the 65KB of available memory are not only
gone forever, but completely forgotten.

The first time I saw my first simple Windoze "hello world" MS C program create
and executable of 640KB, I almost freaked.

With all due respect people, we are coding to hardware architectures -- CPUs,
PIAs, memory address controllers, etc -- and using c for it tight code
generation for speed. I doubt any new c programmers even attempt to learn
assembler for the processor they work on most, or use their c compiler to emit
the assembler code and see the correlations between the c code to what is
being generated.

I never thought micro programmers would become as arrogant as mainframe
programmers use to be towards we who jumped to micros thirty years ago.

Sorry if I offended anyone, it was not my intention. Just need to vent, I guess.
Jan 9 '06 #145
Joseph Dionne wrote:

It astounds me to read some the post here, most being "off topic" or
"don't do that" for people seeking solutions to problems the
responders have most likely implemented.


More blither blather from you. And, in my mind, more than enough.
*plonk*

Brian
Jan 9 '06 #146
Joseph Dionne <sp******@minds pring.com> writes:
Default User wrote:
Keith Thompson wrote:
Joseph Dionne <jd*****@hotmai l.com> writes:
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.

Does K&R really say that arguments are passed by placing their values
on "the stack"? I'd be a bit surprised by that, since the C standard
doesn't talk about a stack, and a number of real-world implementations
pass most arguments in registers. The only references to a "stack" I find in K&R2 is their own stack
routines they devised for doing some calculator-type stuff.
Brian


It astounds me to read some the post here, most being "off topic" or
"don't do that" for people seeking solutions to problems the
responders have most likely implemented.


This newsgroup discusses the C programming language as defined by the
ISO standard(s) (C99 and C90); older references such as K&R1 are also
topical for historical discussions. If you want to discuss something
that's specific to some system, you can do so in a newsgroup that
discusses that system.

I understand that the name "comp.lang. c" might lead you to think that
we discuss anything and everything having to do with C programming.
We don't. Now you know.
It also astounds me to realize how many c programmers, a low level
language, have little to no understanding of how the c compiler works,
some even claiming the OS "jumps to main" which is completely false.
It is apparent many posters have little understanding of the structure
of an executable object created by the compiler, or how executable
object loaders function.
There is no such thing as "the C compiler". There are a plethora of C
compilers, and they don't all work the same way. The only thing they
all have in common is that they all (to the extent that they're
correct) implement the C programming language. The core of that
language is defined by the ISO standard(s). (I'm being vague about
C90 vs. C99 because C99 is the current "official" standard, but C90 is
still more widely implemented.)

[snip]
With all due respect people, we are coding to hardware architectures
-- CPUs, PIAs, memory address controllers, etc -- and using c for it
tight code generation for speed. I doubt any new c programmers even
attempt to learn assembler for the processor they work on most, or use
their c compiler to emit the assembler code and see the correlations
between the c code to what is being generated.


Most of us are coding programs that are intended to perform some
particular task. Very often we can do so in standard C, without
concern for what the underlying hardware look like; if so, the code is
likely to be portable to different systems, which is an extremely
useful attribute. Occasionally, we might need to delve into
system-specific features; such things are best discussed in
system-specific newsgroups.

And I couldn't help noticing that you never actually answered my
question. I'll ask it again: Does K&R really say that arguments are
passed by placing their values on "the stack"? I've just checked my
copies of K&R1 and K&R2, and the answer appears to be no (I see no
mention of a "stack" in the section on functions and argument passing,
and there's no "stack" entry in either book's index). But, as they
say, you can't grep dead trees, and I could have missed something.

In the short time you've been here, you've posted a number of things
that are simply incorrect. (The most blatant was your statement about
unary vs. binary "&".) We all make mistakes now and then, but
refusing to acknowledge them is an efficient way to destroy your own
credibility.

--
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.
Jan 10 '06 #147
On Mon, 09 Jan 2006 23:08:42 GMT, in comp.lang.c , Joseph Dionne
<sp******@minds pring.com> wrote:

It also astounds me to realize how many c programmers, a low level language,
have little to no understanding of how the c compiler works,
You can't infer this from posts to CLC, since the topic here is hte C
language, not any specific compiler and how it works
With all due respect people, we are coding to hardware architectures
sure, but in CLC we're discussing C, nto architectures, cpus etc.,
Sorry if I offended anyone, it was not my intention. Just need to vent, I guess.


Also need to stay ontopic.
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 =----
Jan 10 '06 #148
Keith Thompson wrote:
Joseph Dionne <sp******@minds pring.com> writes:

[..]

This newsgroup discusses the C programming language as defined by the
ISO standard(s) (C99 and C90); older references such as K&R1 are also
topical for historical discussions. If you want to discuss something
that's specific to some system, you can do so in a newsgroup that
discusses that system.

I understand that the name "comp.lang. c" might lead you to think that
we discuss anything and everything having to do with C programming.
We don't. Now you know.


Thanks for the clarification. I'll leave you to your blobviations.

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

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


I posted this the other day but google delayed it by a substantial
amount of time. Being that the post did not appear immediately
(when it usually does), I figured the issue was something else and
hence for my reposting.


Ah. My apologies - I was somewhat agitated at seeing the three messages
all at once, but google's screwups (or their upstreams, as the case may
be) are hardly your fault.
Jan 10 '06 #150

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
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
10350
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...
0
10096
tracyyun
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...
0
9174
agi2029
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...
1
7638
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
5534
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
4311
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
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.