473,804 Members | 2,277 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
<OT>
foo
</OT>

Chad

Jan 9 '06 #111
ri*****@cogsci. ed.ac.uk (Richard Tobin) writes:
In article <sl************ **********@rand om.yi.org>,
Jordan Abel <ra*******@gmai l.com> wrote:
That's not "emulating pass by reference", that is passing by reference.


You will find yourself talking at cross-purposes if you take that
approach, since most people don't take "has call-by-reference" to mean
"can achieve the effect of call-by-reference by use of a pointer".

You're well on your way to the Turing tar-pit.


The phrase wasn't "has call-by-reference" (referring to the C
language), it was "is passing by referenece" (referring to the
technique of passing a pointer to an object to allow a function to
modify the object).

C doesn't "have" linked lists, but in a program using the usual
technique involving structures and pointers, the linked lists are real
linked lists.

--
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 9 '06 #112
M.B

Keith Thompson wrote:
"M.B" <mb*******@gmai l.com> writes:
Chuck F. wrote:
M.B wrote:
> sorry if thjis is a duplicate reply...
>
> I guess this is a scope issue for variable "namelist" I suggest
> to change the function int getFileName(cha r *,struct dirent
> ***); and make necessary changes to code. it may work fine
>
> bcoz scandir i guess sets "struct dirent **" itself. if its
> global all is fine, but local variables 0 its problem-same as
> pass by value

Please use English, not dudespeak, in this newsgroup, and include
adequate context. Between the two your article is totally
meaningless. See below for further advice.
For letting out context, my mistake sorry.
English - hope this is not an english literature group. People want
answers and its enough if they get those in anyway. go hell with
correct English.


You don't need to write flawless English, especially if it doesn't
happen to be your native language, but avoiding abbreviations like "u"
for "you" and "bcoz" for "because" make it *much* easier to understand
you.

Too many SMS s . I will try
Also please check if the advice given in your signature works
correctly.
As far as I know it does. Are you saying it doesn't?


Not for me (and i dont know why).
And finally, noone has time and patience to reply to trolls.
Chuck F. is not a troll. Trust me on this.


I know. Chuck F and others, I apologise

--
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 9 '06 #113
On 2006-01-09, Richard Tobin <ri*****@cogsci .ed.ac.uk> wrote:
In article <sl************ **********@rand om.yi.org>,
Jordan Abel <ra*******@gmai l.com> wrote:
That's not "emulating pass by reference", that is passing by reference.


You will find yourself talking at cross-purposes if you take that
approach, since most people don't take "has call-by-reference" to mean
"can achieve the effect of call-by-reference by use of a pointer".


Nobody said "has call-by-reference". In the initial context, the mention
of "pass by reference / pass by value" was perfectly clear. It's only
this subsequent arguing in circles that has muddied the waters.
Jan 9 '06 #114
Mark McIntyre wrote:

I have no clue what that babble means, but then, Trolls rarely make
sense.

You're starting to get it. Now, the final enlightenment will be to stop
replying to this cretin.

Brian
Jan 9 '06 #115

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.

http://publib.boulder.ibm.com/infoce...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++?

Jan 9 '06 #116

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? Are you kidding? Chapter and verse
please!

--
aegis

Jan 9 '06 #117

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.


How does this make your case? This in no way
suggests that C supports pass by reference.
If you keep on insisting that C supports this concept
then I will have to ask you for a chapter and
verse please.

--
aegis

Jan 9 '06 #118

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.


No it does not. See "call by value: 3.3.2.2." straight 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./"

Pass by reference is about passing around actual containers
and not values. The standard is clear here.
There are no two ways about it.

--
aegis

Jan 9 '06 #119

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!

--
aegis

Jan 9 '06 #120

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
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
10347
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
10090
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
9173
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
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...
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.
3
3001
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.