Re: segfault w/ block, but not file scope
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Thu, 05 Jan 2006 22:42:28 -0600
On Thu, 05 Jan 2006 22:44:27 -0500, Dieter <usenet.stuff@xxxxxxxxxxx>
wrote in comp.lang.c:
> Dieter wrote:
> > 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(H5DIR, namelist); /* included from mylib.h */
> > freeFileNames(namelist, n); /* included from mylib.h */
> >
> > return 0;
> > }
> >
> >
> > Thank you very much for your comments,
> > Dieter
>
> Here's the actual code if needed. Although dirent.h is platform
> specific, I think my question is relative to standard C.
The actual code is not needed, and is indeed off-topic. Your problem
has a great deal to do with how the function scandir(), which is
apparently from dirent.h, deals with pointers.
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <dirent.h>
> #include <errno.h>
>
> #define H5DIR "/home/stella/data/h5/rawTables"
>
> int getFileNames(char *directory, struct dirent **namelist);
> int freeFileNames(struct dirent **namelist, int entries);
>
> struct dirent **namelist;
When you define this pointer a file scope, it has static storage
duration and is therefore initialized to NULL. When you define it
inside a function, it has automatic storage duration by default and it
not initialized at all.
> int main(void)
> {
> int n;
>
> n = getFileNames(H5DIR, namelist);
> printf("%d\n",n);
> err = freeFileNames(namelist, n);
> if (err==0)
> printf("There wasn't any files");
> return 0;
> }
>
> int getFileNames(char *directory, struct dirent **namelist)
> {
> int n, i;
>
> n = scandir(directory, &namelist, 0, alphasort);
[snip]
On the last line of code above, you pass a pointer to 'namelist'
(therefore a char ***) to scandir(). Presumably this function checks
whether the pointed-to char ** is NULL or not, and if it is NULL, it
allocates the necessary memory. And also presumably, if the
pointed-to char ** is not NULL, it assumes that it points to valid
memory and uses it. At the end you try to free a pointer that was not
allocated.
I would suggest that you study the documentation for the function
scandir() and see what the requirements are for that parameter.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- Follow-Ups:
- Re: segfault w/ block, but not file scope
- From: Jordan Abel
- Re: segfault w/ block, but not file scope
- From: Dieter
- Re: segfault w/ block, but not file scope
- References:
- segfault w/ block, but not file scope
- From: Dieter
- Re: segfault w/ block, but not file scope
- From: Dieter
- segfault w/ block, but not file scope
- Prev by Date: Re: Another question related to pointers.
- Next by Date: Re: segfault w/ block, but not file scope
- Previous by thread: Re: segfault w/ block, but not file scope
- Next by thread: Re: segfault w/ block, but not file scope
- Index(es):
Relevant Pages
|
Loading