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

  • Home
  • SEARCH
  • 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 730715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:56:37+00:00 2026-05-14T06:56:37+00:00

On a Solaris 5.8 machine, I have the following code: [non-working code] char *buf;

  • 0

On a Solaris 5.8 machine, I have the following code:

[non-working code]

char *buf;
char *dir;
size_t psize;

psize = (size_t) 1024;
dir = getcwd(buf, psize);

On this unix machine, the above does not work and I get a segmentation fault when trying to run the program. It only works if I declare dir before buf:

[working code]

char *dir;
char *buf;
...
dir = getcwd(buf, psize);

When using another flavor of Unix, such as Mac OS X, I don’t get any of these what seem to be very strict rules on how to write the code. Can anyone explain what’s going on with the above example? Thanks!

  • 1 1 Answer
  • 1 View
  • 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-14T06:56:38+00:00Added an answer on May 14, 2026 at 6:56 am

    Here’s from getcwd(3):

    DESCRIPTION
         The getcwd() function copies the absolute pathname of the current working
         directory into the memory referenced by buf and returns a pointer to buf.
         The size argument is the size, in bytes, of the array referenced by buf.
    
         If buf is NULL, space is allocated as necessary to store the pathname.
         This space may later be free(3)'d.
    
    

    That is – set the buf to NULL and free(3) the dir when done; OR allocate space for the buf yourself (since you are telling the getcwd(3) you have 1K there).

    Edit:

    So to clean it up a bit, it’s either:

    char *dir = getcwd( NULL, 0 );
    
    if ( dir == NULL ) { /* handle error */ }
    /* use dir */
    free( dir );
    

    or

    char buf[1024]; /* or allocate it with malloc(3) */
    
    if ( getcwd( buf, 1024 ) == NULL ) { /* handle error */ }
    
    /* use buf, DO NOT free it if it's on the stack or static, 
       only if malloc-ed */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: foreach($summary as $machine) { $hostname = $machine['node']; $result = mysql_query(SELECT
I have two network interfaces on my Solaris machine and the output of ifconfig
I have 1000 files in a directory. I'm on a solaris machine I want
I have a Solaris sparc machine and when i build my programs, it generates
I have GIT running on a Solaris server. From a windows machine I installed
On Linux, I use stat --format="%s" FILE , but the Solaris machine I have
I have Solaris machine with oracle Release 10.2.0.4.0 I have problem - I cant
I have an app sits on machine A (solaris.example.com) and same apps sits on
I am trying this code to download a file from a Windows machine using
I have a doubt in the following piece of code and its behaviour: #include

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.