Thread: Array Elements Overwritten

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    7

    Angry Array Elements Overwritten

    I have the following function in a tiny C app. I basically take in an array of directory paths (the "paths" argument) and, for each path, iterate over each file in the directory and print the file name.

    The first iteration of my for loop over the paths array works fine - but then on subsequent iterations, somehow the contents of the paths array are being overwritten/turned to garbage. So in the second iteration, for example, the printf statement "Search path before loop:..." prints either a blank string or garbage. But I'm not modifying anything in the paths array, anywhere! What am I doing wrong?

    Thanks all in advance!

    Code:
    int processIt(const char* command,  char* paths[])
    {
      printf("processIt\n");
      
      struct stat buf;
      int status;
      int i;
    
      DIR *dir;
      struct dirent *entry;
      int x;  
    
      for (i = 0; paths[i] != NULL; i++) {
        printf("Search path before loop: %s\n", paths[i]);
      
        dir = opendir(paths[i]);
        if (dir == NULL) {
          printf("Error");
         return -1;
        }
    
        while ((entry = readdir(dir)) != NULL) {
          printf("Found a file.\n");
          printf("file:%s\n", entry->d_name);
        }
        
        closedir(dir);
    
        free(pCurPath);
        pCurPath = 0;
      }
    
    
      return 0;
    } /* processIt! */
    Last edited by Zoetermeer; 10-07-2008 at 10:04 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I suppose the question is, how sure are you that paths[i] is correct when the function starts? Maybe add an extra for-loop at the front to print all the paths before the main for loop that reads things. The only thing that I don't know what it is is this pCurPath thing. What is that, and what does it point to, and why do you free it every time through the loop?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    I actually have one at the beginning of the function, but I removed it before posting for brevity
    So yes, the paths array definitely looks okay at the beginning of the function.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, freeing pCurPath still looks a little suspicious. (What if it points to the paths?) I don't see anything else that can tromp on your array.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    sorry...forgot to take that line out. that's a leftover char* from when i tried, on each iteration, to copy the contents of path[i] into a temporary string pointer. i thought that would prevent any code from touching the actual array besides strcpy, but it didn't fix my problem.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then you're not having any problems then.
    Quote Originally Posted by my machine
    Search path before loop: DayDate
    Found a file.
    file:.
    .
    .
    .
    Search path before loop: Documents
    Found a file.
    .
    .
    .
    Search path before loop: Library
    Found a file.
    .
    .
    .

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    so you ran that fine on your machine? how could that be? i ran this code on both a linux vm, and then on os x, and got the same result for both. assuming the array argument looks fine coming into the function, is there anything that could mess with it?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The code looks like so:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <dirent.h>
    
    int processIt(const char* command,  char* paths[])
    {
      printf("processIt\n");
    
      int status;
      int i;
    
      DIR *dir;
      struct dirent *entry;
      int x;
    
      for (i = 0; paths[i] != NULL; i++) {
        printf("Search path before loop: %s\n", paths[i]);
    
        dir = opendir(paths[i]);
        if (dir == NULL) {
          printf("Error");
         return -1;
        }
    
        while ((entry = readdir(dir)) != NULL) {
          printf("Found a file.\n");
          printf("file:%s\n", entry->d_name);
        }
    
        closedir(dir);
    
      }
    
    
      return 0;
    } /* processIt! */
    
    int main(int argc, char *argv[])
    {
        char *paths[] = {"DayDate", "Documents", "Library", 0};
        processIt("ls", paths);
     return 0;
    }
    Obviously, those are just three directories I happened to choose out of my home directory. I took out some of the variables that weren't being used, too.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    I can do the same thing and everything works fine. This is crazy! In both cases, the array argument looks fine when I enter the function. But something else must be going on...can I send you the rest of my code (it's only like 3 files)?

    Thanks for your help!

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Zoetermeer View Post
    I can do the same thing and everything works fine. This is crazy! In both cases, the array argument looks fine when I enter the function. But something else must be going on...can I send you the rest of my code (it's only like 3 files)?

    Thanks for your help!
    You should be able to attach it to a message here, sure.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    ok, here are the three files (i tried to attach the makefile but i think the site won't allow it). let me know if this works.

    around line 150, i have both the "actual" code (that passes in the array, where i was having problems), as well as some commented code that passes in the test array as you did.

    also - i managed to look at this in the debugger, and the array contents seem to get mangled right after the first call to opendir.

    i can't thank you enough!

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Okay, so my original output was something like this:
    Could not access directory: tz??\z??\z?F?G?F?G?F?G?F?G?F?G?F?G?F?G?
    Could not access directory: z??\z?F?G?F?G?F?G?F?G?F?G?F?G?F?G?
    Could not access directory: F?G?F?G?F?G?F?G?F?G?F?G?
    Could not access directory: G?F?G?F?G?F?G?F?G?
    Could not access directory: ?G?
    I then added the line
    Code:
      printf("Before tokenize: &#37;s\n", pathlist);
    directly before the tokenize call and the line
    Code:
    for (i = 0; paths[i] != NULL; i++) printf("Path #%d: %s\n", i, paths[i]);
    Makes absolutely no difference to the actual program itself, right? Right:
    Before tokenize: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
    Path #0: /usr/bin
    Path #1: /bin
    Path #2: /usr/sbin
    Path #3: /sbin
    Path #4: /usr/local/bin
    Path #5: /usr/X11/bin
    false
    The "false" is because I was using ls as my command-line argument, which matches as far as strstr is concerned.

    So hooray heisenbugs. I had the thing set at -O3; maybe things were getting optimized away? I don't know. (Edit: I might be convinced to take another look at it in a bit, but that may or may not get you started.)
    Last edited by tabstop; 10-08-2008 at 10:11 PM.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    so you added that loop directly after the call to tokenize? i tried running it by adding those two lines and still got the garbled data in the array on the second iteration.

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Inside util.c, this looks wrong:
    Code:
      copyofstring= (char *)malloc(strlen(string));
      strcpy(copyofstring, string);
    Should allow for one more byte for trailing nul.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding More Array Elements?
    By Vermillion in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2008, 10:02 PM
  2. coping elements not in array
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 07:53 PM
  3. way to check 3 elements of array and set 4th
    By Syneris in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2006, 11:30 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM