Re: segfault w/ block, but not file scope



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
.



Relevant Pages

  • Re: object creation and naming at runtime?
    ... >>replaced with memory references by the compiler. ... I don't know where the computer will allocate space in ... memory for my int so I'll use a name for the address and let ... In the source code the pointer object (which will ...
    (alt.comp.lang.learn.c-cpp)
  • Re: 2-dimensional arrays and functions
    ... > int* foo ... >> that return memory addresses. ... >> You obviously don't know the difference between a pointer and a memory ... > address of the dynamically allocated memory is stored. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Explications for malloc ang some questions about pointers
    ... A pointer is a thing that contains the address of some ... block of memory somewhere. ... "malloc" allocates a number of bytes and returns a pointer to ... You have to go through the lists a_tmp and b_tmp, ...
    (comp.lang.c)
  • Re: FIFO Problem
    ... text but a *pointer* to some text. ... pointing to the string in the clients memory. ... if you want to pass a string from one process to the other then ... int length; ...
    (comp.os.linux.development.apps)
  • Re: 2D array of structures
    ... Don't cast the return value of malloc(), ... you allocate here memory for 7 such structures. ... a pointer to the start of this memory, which is of type 'STRUCTURE *' ...
    (comp.lang.c)

Loading