Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 808179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:31:18+00:00 2026-05-15T00:31:18+00:00

Sorry to keep hammering on this, but I’m trying to learn :). Is this

  • 0

Sorry to keep hammering on this, but I’m trying to learn :). Is this any good? And yes, I care about memory leaks. I can’t find a decent way of preallocating the char*, because there simply seems to be no cross-platform way.

const string getcwd()
{
    char* a_cwd = getcwd(NULL,0);
    string s_cwd(a_cwd);
    free(a_cwd);
    return s_cwd;
}

UPDATE2: without Boost or Qt, the most common stuff can get long-winded (see accepted answer)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:31:19+00:00Added an answer on May 15, 2026 at 12:31 am

    If you want to remain standard, getcwd isn’t required to do anything if you pass to it a NULL; you should instead allocate on the stack a buffer that is “large enough” for most occasions (say, 255 characters), but be prepared for the occasion in which getcwd may fail with errno==ERANGE; in that case you should allocate dinamically a bigger buffer, and increase its size if necessary.

    Something like this could work (notice: not tested, just written by scratch, can be surely improved):

    string getcwd()
    {
        const size_t chunkSize=255;
        const int maxChunks=10240; // 2550 KiBs of current path are more than enough
    
        char stackBuffer[chunkSize]; // Stack buffer for the "normal" case
        if(getcwd(stackBuffer,sizeof(stackBuffer))!=NULL)
            return stackBuffer;
        if(errno!=ERANGE)
        {
            // It's not ERANGE, so we don't know how to handle it
            throw std::runtime_error("Cannot determine the current path.");
            // Of course you may choose a different error reporting method
        }
        // Ok, the stack buffer isn't long enough; fallback to heap allocation
        for(int chunks=2; chunks<maxChunks ; chunks++)
        {
            // With boost use scoped_ptr; in C++0x, use unique_ptr
            // If you want to be less C++ but more efficient you may want to use realloc
            std::auto_ptr<char> cwd(new char[chunkSize*chunks]); 
            if(getcwd(cwd.get(),chunkSize*chunks)!=NULL)
                return cwd.get();
            if(errno!=ERANGE)
            {
                // It's not ERANGE, so we don't know how to handle it
                throw std::runtime_error("Cannot determine the current path.");
                // Of course you may choose a different error reporting method
            }   
        }
        throw std::runtime_error("Cannot determine the current path; the path is apparently unreasonably long");
    }
    

    By the way, in your code there’s a very wrong thing: you are trying to dellocate a_cwd (which presumably, in the nonstandard extension, is allocated with malloc or with some other memory allocation function, since getcwd is thought for C) with delete: you absolutely shouldn’t do that, keep in mind that each allocation method has its deallocation counterpart, and they must not be mismatched.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sorry to keep asking stupid questions, but I have tried to research this
Sorry if this is a simple question, but I've only recently started learn coding
Sorry, I keep on trying to adapt the tokens, but somehow I can't manage
Sorry if this is a dup; I haven't found any questions that pose quite
Sorry, I'm sure this is simple but I'm tired and can't figure it out.
Sorry for the unhelpful title, but hopefully I can explain this well enough. Lets
Sorry for the very involved question, but this is something I've been researching for
Sorry to keep asking so many questions, but you guys always seem to be
Sorry about the title, not sure how to describe without example. I trying to
im sorry if this has already been covered, i have had a look but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.