Code Monkey home page Code Monkey logo

Comments (6)

mananapr avatar mananapr commented on July 17, 2024
    // Stores number of files in the child directory
    len_preview = getNumberofFiles(next_dir);
    // Stores files in the child directory
    char* next_directories[len_preview];
    status = getFiles(next_dir, next_directories);
    if status == -1:
        continue;

Will the status check solve this problem?

from cfiles.

mlite avatar mlite commented on July 17, 2024

No, it will blow out your call stack. Call stack is meant for small and relatively fixed-sized arrays.
For your cases, I think the best approach is to allocate it on heap.

FYI, I use my Stensal SDK (https://stensal.com) to build and test your code, and found this problem.

from cfiles.

mananapr avatar mananapr commented on July 17, 2024

If I use malloc, the code will look like this:

        // Stores number of files in the child directory
        len_preview = getNumberofFiles(next_dir);
        // Stores files in the child directory
        char** next_directories;
        next_directories = (char **)malloc(len_preview * sizeof(char *));
        /*
            Rest of the code goes here
        */ 
        // Free Memory
        for( i=0; i<len_preview; i++ )
        {
            free(next_directories[i]);
        }
        free(next_directories);

So will this be enough?
Also I have used a similar declaration for char *directories[len];. So why is it the case that it is not wrong? Should I use malloc in that case too?

from cfiles.

mlite avatar mlite commented on July 17, 2024

I only see the allocation for next_directories. Presumabily you also allocate for each of next_directories[i]. If it is the case, yes, it's more reliable than allocating on call stacks.

It's not about right or wrong. It's less reliable and you can exhaust call stack quickly. The limit of call stack is much smaller than the limit of heap.

from cfiles.

mananapr avatar mananapr commented on July 17, 2024

I made the relevant changes in the latest commit 9be9889.
Let me know if this is good enough for closing this issue. Thanks!

from cfiles.

mananapr avatar mananapr commented on July 17, 2024

See #11

from cfiles.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.