Thread: Pls correct me if I am wrong, but is this a bit of bad code?

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    Pls correct me if I am wrong, but is this a bit of bad code?

    THIS IS NOT MY CODE, it is open source so I am just using it.

    I got this open file, read dir, using a char *path, then freeing path without malloc to create it. as my knowledge is limited, would that not cause problems in a long run?

    Code:
    int len = strlen(origpath);
    char *path = (char*) malloc(len+1);
    should not the above be added to this code? or something like that.
    as I just wrote that off the top of my head. not really sure if that
    is even right even though it looks logical.

    Code:
    void add_file_to_filelist_recursively(char *origpath, unsigned char level)
    {
        struct stat st;
        char *path;
        int recursive = 1;
    
    
        if (!origpath)
            return;
    
        path = strdup(origpath);
     
    
        if (level == FILELIST_FIRST)
        {
            /* First time through, sort out pathname */
            int len = 0;
    
            len = strlen(path);
            if (path[len - 1] == '/')
                path[len - 1] = '\0';
    
        char *newpath = get_absolute_path(path);
    
                free(path); <------ here
                path = newpath;
    
        }
    
        errno = 0;
        if (stat(path, &st))
    
        {
            printf("bad path %s\n", path);
            free(path); <--- here
            exit(1);
        }
    
            if ((S_ISDIR(st.st_mode)) && (level != FILELIST_LAST)) {
            struct dirent **de;
            DIR *dir;
            int cnt, n;
    
            if ((dir = opendir(path)) == NULL)
            {
                printf("couldn't open directory %s:", path);
                free(path); < ---- here
                return;
            }
    
            n = scandir(path, &de, NULL, NULL);
            if (n < 0) {
                switch (errno) {
                case ENOMEM:
                    printf("Insufficient memory to scan directory %s:", path);
                    break;
                default:
                    printf("Failed to scan directory %s:", path);
                }
            } else
            {
                for (cnt = 0; cnt < n; cnt++) {
                    if (strcmp(de[cnt]->d_name, ".")
                            && strcmp(de[cnt]->d_name, ".."))
                            {
                                char *newfile;
    
                                newfile = strjoin("", path, "/", de[cnt]->d_name, NULL);
                          //      printf("add_2_front %s newfile %s\n", path, newfile);
    
                        //gets recursive always
                        if (recursive)
                        {
    
                            add_file_to_filelist_recursively(newfile, FILELIST_CONTINUE);
                        }
                        else
    
                            add_file_to_filelist_recursively(newfile, FILELIST_LAST);
                            free(newfile);
                    }
                    free(de[cnt]);
                }
                free(de);
            }
            closedir(dir);
        }
        else if (S_ISREG(st.st_mode))
        {
            add_2_front(&filelist, path);
        }
        free(path); <-- here
        return;
    }
    MOD:
    I just added
    Code:
    int len = strlen(origpath);
    char *path = (char*) malloc(len+1);
    and compiled, and that little bit of code I asked about is working, but it is proper?
    Last edited by userxbw; 10-05-2017 at 10:35 AM.

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    strdup is a POSIX standard function that duplicates a string. It uses malloc to allocate memory for the string, so it can be freed with free.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by christop View Post
    strdup is a POSIX standard function that duplicates a string. It uses malloc to allocate memory for the string, so it can be freed with free.
    ok, but it wasn't working in c++ written like that, but that code is now working in C++ written like that.

    I asked it in here C, because my program is already completed, and it is acting funny, not ha ha funny.

    I have written this program to change a file at a given time, display it on the background (wallpaper), if I set that time to say every 5 to 20ish seconds, eventually it will freeze up my entire system (Linux).
    it has to be a memory thing right? (even though that maybe a hard question to answer).

    I was actually hoping that was the problem. because I want to rewrite it in C++ but not every bit of code.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well the best thing to do is compile it with the "-g" flag to get debug information, then run the program in the debugger.

    When it locks up (or seems to), you can interrupt it and get a stack trace.

    From there, you can examine variables, set breakpoints, single step the code (and so on).
    Determine whether it's actually "stuck", or just stuck in some loop of yours which never ends.

    Maybe your list wound up pointing to itself.

    A resource you're likely to run out of early is open file handles. Check that you're closing everything you open.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Salem View Post
    Well the best thing to do is compile it with the "-g" flag to get debug information, then run the program in the debugger.

    When it locks up (or seems to), you can interrupt it and get a stack trace.

    From there, you can examine variables, set breakpoints, single step the code (and so on).
    Determine whether it's actually "stuck", or just stuck in some loop of yours which never ends.

    Maybe your list wound up pointing to itself.

    A resource you're likely to run out of early is open file handles. Check that you're closing everything you open.
    plus take a week or so to learn ddd (linux gdb gui debugger)

  6. #6
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by userxbw View Post
    plus take a week or so to learn ddd (linux gdb gui debugger)
    Hey, thanks userbxw for turning us on to DDD. The only thing I didn't like about it was the white background, but I repaired that by putting
    Code:
    Ddd*XmText.background: DarkGrey
    Ddd*XmTextField.background: DarkGrey
    at the top of ~/.ddd/init file. (For more resources to modify look in /etc/X11/app-defaults/Ddd in Ubuntu. But don't modify them there, just use that file as a reference. ) DDD even integrates with Emacs. If you run M-x gdb <RETURN> ddd -t programname, DDD will put the Debugger Console in an Emacs window, and clicking the Edit button in DDD will open your source in Emacs. Emacs will complain about the file being locked and ask what to do. I didn't run into any problems pressing p to proceed. I think everything else shows up in DDD windows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please correct my code
    By x3.Aditya in forum C Programming
    Replies: 1
    Last Post: 02-12-2016, 03:32 AM
  2. Can someone please correct my code?
    By Alex Coven in forum C Programming
    Replies: 6
    Last Post: 09-04-2013, 03:19 AM
  3. Correct algorithm, wrong result (cout problem?)
    By dember in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2009, 06:03 PM
  4. [Help]Correct or Wrong?Loops
    By exjames1991 in forum C Programming
    Replies: 11
    Last Post: 10-19-2009, 02:11 AM
  5. What is wrong here, just won't print the correct numbers
    By Unregistered in forum C++ Programming
    Replies: 16
    Last Post: 05-10-2002, 03:31 PM

Tags for this Thread