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 6916
Jordan Abel said: On 2006-01-08, Richard Heathfield <in*****@invali d.invalid> wrote: [...] Jordan is wrong about time() which does not in fact take its argument by reference. It takes its parameter by value.
It takes a time_t by reference.
No, it doesn't. It takes a time_t * by value.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
On 2006-01-08, Richard Heathfield <in*****@invali d.invalid> wrote: 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).
I know what you're saying, but I think that's an own-foot-shooting way to say it.
"own-foot-shooting" wording is something that C has its fair share of,
so best not to cast the first stone. See "equivalenc e of pointers and
arrays" (in, of course, all but three contexts), %f in printf vs scanf
(traceable to the problem of float-to-double argument promotion in
general), the example rand(), etc, etc.
To name an issue that recently came up in here, I think it would have
been better if %s for scanf took "pointer to array of char" rather than
"pointer to char as first member of array".
In particular, the use of the term "object" in C to refer to something
other than what it means to those familiar with other languages is
comparable to this use of "pass by reference" to refer to a programming
technique rather than the language feature it sometimes means instead.
On 2006-01-08, Chuck F. <cb********@yah oo.com> wrote: Fortunately for JRN the use of pass-by-name has virtually disappeared. In fact fortunately for most of us, IMO.
However, see also C Preprocessor macro expansion.
On 2006-01-08, Richard Heathfield <in*****@invali d.invalid> wrote: Jordan Abel said:
On 2006-01-08, Richard Heathfield <in*****@invali d.invalid> wrote: [...] Jordan is wrong about time() which does not in fact take its argument by reference. It takes its parameter by value.
It takes a time_t by reference.
No, it doesn't. It takes a time_t * by value.
Which is the means by which it implements a reference to a time_t.
Again, the programming technique (implemented quite easily in C) is
different from the language feature (which C, of course, does not have).
Richard Heathfield <in*****@invali d.invalid> writes: Jordan Abel said: On 2006-01-08, Richard Heathfield <in*****@invali d.invalid> wrote: [...] Jordan is wrong about time() which does not in fact take its argument by reference. It takes its parameter by value.
It takes a time_t by reference.
No, it doesn't. It takes a time_t * by value.
It does that too.
--
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 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.
Your continued reference to 'object' during replies proves my assertions
no, it proves he has more patience than most folk.
You're a troll.
*plonk*
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 =----
On Sun, 08 Jan 2006 00:01:42 GMT, in comp.lang.c , Joseph Dionne
<jd*****@hotmai l.com> wrote: Mark McIntyre wrote: On Sat, 07 Jan 2006 23:23:22 GMT, in comp.lang.c , Joseph Dionne <jd*****@hotmai l.com> wrote:
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.
Sorry, Mr. McIntyre, software is a precision art, not hand grenades or horseshoes. It has a history of development that ought not be forgotten or convoluted by future misrepresentati ons simply because the originating concept is difficult to grasp.
I have no clue what that babble means, but then, Trolls rarely make
sense.
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 =----
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.
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. The fact that the language doesn't have special syntax for it doesn't mean it's suddenly a different programming technique.
The phrase "programmin g technique" is a little bit slippery (in
much the same sense that an elephant is a little bit large :-) ).
C does not have garbage collection either, but you can "emulate"
it by replacing all operations of the form:
p = malloc(nbytes);
...
q = p;
...
q = r;
...
free(q);
...
with:
record_release( p); p = malloc(nbytes); record_ref(p);
...
record_release( q); q = p; record_ref(q);
...
record_release( q); q = r; record_ref(q);
...
record_release( q); free(q); q = NULL; /* for future record_release */
(make sure that p==NULL and q==NULL initially, of course).
If you do this, would you claim that C now "has" garbage collection?
You can emulate by-reference more easily than you can emulate
automatic garbage collection. Does the difficulty of emulation
play a role in deciding whether the language "has" the thing being
emulated?
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
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.
-- Richard 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: 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,...
| |
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: 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...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| | |