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 8269339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:11:31+00:00 2026-06-08T06:11:31+00:00

I probably missed something but just wanna ask.. I found this code in the

  • 0

I probably missed something but just wanna ask.. I found this code in the book Advanced Linux programming:

    char* get_self_executable_directory ()
    {
      int rval;
      char link_target[1024];
      char* last_slash;
      size_t result_length;
      char* result;
      /* Read the target of the symbolic link /proc/self/exe. */
      rval = readlink (“/proc/self/exe”, link_target, sizeof (link_target));
      if (rval == -1)
        /* The call to readlink failed, so bail. */
        abort ();
      else
        /* NUL-terminate the target. */
        link_target[rval] = ‘\0’;
      /* We want to trim the name of the executable file, to obtain the
      directory that contains it. Find the rightmost slash. */
      last_slash = strrchr (link_target, ‘/’);
      if (last_slash == NULL || last_slash == link_target)
        /* Something strange is going on. */
        abort ();
      /* Allocate a buffer to hold the resulting path. */
      result_length = last_slash - link_target;
      result = (char*) xmalloc (result_length + 1);
      /* Copy the result. */
      strncpy (result, link_target, result_length);
      result[result_length] = ‘\0’;
      return result;
    }

And my question is, doesn’t this function return a dangling pointer?

  • 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-06-08T06:11:34+00:00Added an answer on June 8, 2026 at 6:11 am

    It returns a pointer and expects that deallocation will be done in client code. When you see functions that return pointers you always have to ask yourself (well really the author…) whether or not ownership of the memory (that is the responsibility to deallocate it) passes to the function’s client and if so how exactly it should be freed.

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

Sidebar

Related Questions

Probably I missed something, but lets say I have this: $('a.delete').click(function(){ resp=window.confirm(Are you sure
This is probably something fundamental and stupid that I've missed, but the various workarounds
Probably I'm just too dump for googling, but I always thought char arrays get
this is going to drive me mad, because its probably something I have missed
I've probably missed something big, but I've been trying for hours... So to get
This is probably impossible but i thought i would ask around and see if
It's probably late and I have missed this off by a long shot. I
Probably something really simple, but I'm having some real trouble with it. I have
Probably something simple, but heres my problem, I have implememnted a simple captcha into
Probably a bit off topic question, but it's something I'm really interested in getting

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.