#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Istanbul, TR
    Posts
    85
    Rep Power
    12

    Lightbulb stat and pointer arrays


    i have the below problemm..

    char i;
    char **ptr_files;

    /* with function open_mydir here comes filenames in dir
    and save it in a pointer array here works great*/

    ptr_files = open_mydir(dir);

    //loop print the filenames in order.. here also works
    i=0;
    while (ptr_files[i] != NULL) {
    printf("%s\n", ptr_files[i]);
    i++;
    }

    /* below doesnt work when i send filenames to function
    file_modified_time. */

    for (i=0; i<6; i++) {
    printf("%s: %s\n",ptr_files[i], file_modified_time(ptr_files[i]));
    }

    -------------------------
    if i insert the files manually to pointer array ptr_files

    char **ptr_files[] = {"/home/jazzy/cfiles/apache-scalp.c",
    "/home/jazzy/cfiles/arrtest",
    "/home/jazzy/cfiles/arrtest.c",
    "/home/jazzy/cfiles/exclfile",
    "/home/jazzy/cfiles/install.sh",
    "(char *)0"
    };
    for (i=0; i<6; i++) {
    printf("%s: %s\n",fil[i],file_modified_time(fil[i]));
    }

    then it works...

    I couldnt understand the diffrence between the first code and the second..

    Anyone has an idea about it? thanks for advice.

    I am sure file_modified_time and open_mydir function works..

    char *file_modified_time(char * filename) {

    struct stat stat_p;
    struct tm *time_str;

    if ( stat (filename, &stat_p) == -1 ) {
    fprintf(stderr," Can't stat file: %s\n", filename);
    exit(0);
    }

    time_str = localtime(&stat_p.st_mtime);

    strftime(stbuff, sizeof stbuff, "%Y%m%d", time_str);

    return stbuff;
    }

    and my open_mydir function is;

    char ** open_mydir(char dir[]) {

    struct dirent *dptr;
    char *buff;
    char **myarr;
    char **excldir;
    DIR *dirp;

    int i=0,p;

    buff = (char *) malloc(50 * sizeof(char *));
    myarr = (char **) malloc(100 * sizeof(char **));

    buff = dir;

    if( (dirp = opendir(buff)) == NULL )
    {
    fprintf(stderr,"Error opening %s ",buff);
    perror("dirlist");
    exit(1);
    }

    while(dptr=readdir(dirp)) {
    if ((strcmp(dptr->d_name, ".") == 0 || strcmp(dptr->d_name, "..") == 0)) {
    continue;
    }
    if (stat (dptr->d_name, &st) != -1 && S_ISDIR (st.st_mode)) {
    continue;
    }

    myarr[i] = dptr->d_name;
    i++;
    }

    closedir(dirp);

    return myarr;

    free(myarr);
    }
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0
    Not trying to be negative (hopefully more constructive), but I would suggest reviewing how to use pointers and memory allocation.

    1) you've lost the address to your allocated memory here:

    buff = (char *) malloc(50 * sizeof(char *));
    buff = dir;

    2) This is not NULL terminated:

    char **ptr_files[] = {"/home/jazzy/cfiles/apache-scalp.c",
    "/home/jazzy/cfiles/arrtest",
    "/home/jazzy/cfiles/arrtest.c",
    "/home/jazzy/cfiles/exclfile",
    "/home/jazzy/cfiles/install.sh",
    "(char *)0"
    };

    3) This is a memory leak, the free will never be called:

    return myarr;

    free(myarr);

    4) You are returning addresses to local instances, which if you're quick enough, may work... but will more than likely blow up:

    myarr[i] = dptr->d_name;

    (note: if you don't care about memory leaks, just do this:
    myarr[i] = strdup(dptr->d_name);
    )
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Istanbul, TR
    Posts
    85
    Rep Power
    12
    1)yes you right i give the addr of dir to buff and i lost the address of allocated memory. First i changed

    char ** open_mydir(char dir[]) {
    .........
    buff = (char *) malloc(50 * sizeof(char *));
    buff = dir;
    if( (dirp = opendir(buff)) == NULL )

    to; (i erase buff and use the function parameter dir)

    if( (dirp = opendir(dir )) == NULL )


    2)can you explain this.. What can i do to make it NULL terminated then..


    3) this is a bad mistake to free the returned address.

    4)I used strdup function and i changed
    myarr[i] = dptr->d_name); to
    myarr[i] = strdup(dptr->d_name);

    my code works now.. Thank you very much...
    But you said "if you don't care about memory leaks use strdup"
    TO avoid memory leak what can i do..
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0
    Two things that you should avoid doing:

    - returning addresses to allocated memory in general use routines, even though it is annoying to have handle allocation elsewhere. example: strdup.

    (NOTE: using strdup does not cause a memory leak, but you leave it up to the caller to deallocate.)

    - returning addresses to local variables, even if they're static. This is not thread safe... but for most applications, you shouldn't run into any issues if you're careful. example: strtok.

    The simple method to avoid these issues is to make the caller allocate/deallocate the memory required for the results. A more complex method would be to use smart pointers.


    Here's a null terminated list:

    char **ptr_files[] = {"/home/jazzy/cfiles/apache-scalp.c",
    "/home/jazzy/cfiles/arrtest",
    "/home/jazzy/cfiles/arrtest.c",
    "/home/jazzy/cfiles/exclfile",
    "/home/jazzy/cfiles/install.sh",
    NULL
    };

    But actually *they* say not to use NULL in this context... so:

    char **ptr_files[] = {"/home/jazzy/cfiles/apache-scalp.c",
    "/home/jazzy/cfiles/arrtest",
    "/home/jazzy/cfiles/arrtest.c",
    "/home/jazzy/cfiles/exclfile",
    "/home/jazzy/cfiles/install.sh",
    0
    };

    Basically just get rid of your quotes. Your "for" won't go to the 6th index anyway.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    Who are "they" and why shouldn't you use NULL there?
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0
    Ack, you're going to make me actually look something up! :)

    pg 286, "C++ How To Program, Second Edition" Deitel & Deitel, ISBN: 0-13-528910-6

    "... Initializing a pointer to NULL is equivalent to initializing a pointer to 0, but in C++ 0 is preferred. When 0 is assigned, it is converted to a pointer of the appropriate type. The value 0 is the only integer value that can be assigned directly to a pointer variable without casting the integer to a pointer type first."

    Don't know if this explains *why* exactly...
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Istanbul, TR
    Posts
    85
    Rep Power
    12

    point


    sluggish, thanx for the detailed information..

IMN logo majestic logo threadwatch logo seochat tools logo