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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:21:13+00:00 2026-06-01T07:21:13+00:00

gcc (GCC) 4.6.3 c89 apache runtime portable libraries Hello, Just a simple question I

  • 0
gcc (GCC) 4.6.3
c89
apache runtime portable libraries

Hello,

Just a simple question I guess I am trying to create a uuid value.

I want me function that creates it to be stand alone (black box) so I can just pass a buffer and the function will do the rest.

Which means I have to declare memory on the heap in that function, as the calling function doesn’t know much to declare. I know this a bad idea, as the callie will have to free the memory after it has finished with it.

Is there any way I can do this better?

When I run this the branch_id_buf is always null after the function returns. However, I have passed it to the function so should not be null when the function returns as it should point to some allocated memory.

Many thanks for any advice,

static void g_get_branch_id(char *branch_id_buf);

int main(void)
{
    char *branch_id_buf = NULL;

    g_get_branch_id(branch_id_buf);

    printf("branch id [ %s ]\n", branch_id_buf);

    free(branch_id_buf);

    return 0;
}

static void g_get_branch_id(char *branch_id_buf)
{
    apr_uuid_t uuid;

#define MAGIC_COOKIE_LENGTH 9

    const char *MAGIC_COOKIE = "z9hG4bk-";
    const int BRANCH_ID_LENGTH = APR_UUID_FORMATTED_LENGTH + MAGIC_COOKIE_LENGTH;

    branch_id_buf = malloc(BRANCH_ID_LENGTH);
    if(branch_id_buf == NULL) {
        return;
    }

    strncpy(branch_id_buf, MAGIC_COOKIE, BRANCH_ID_LENGTH);
    printf("branch id [ %s ]\n", branch_id_buf);
    apr_uuid_get(&uuid);

#define PREFIX_BRANCH_LENGTH 8
    apr_uuid_format(branch_id_buf + PREFIX_BRANCH_LENGTH, &uuid);

    printf("branch id [ %s ]\n", branch_id_buf);
}
  • 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-01T07:21:14+00:00Added an answer on June 1, 2026 at 7:21 am

    Generally, it is a very bad idea to allocate in one module and free in another. This is exactly how you create numerous memory leaks in your programs. Your code is particularly problematic, as you don’t only leave freeing to the caller, but also the responsibility to initialize the pointer to NULL

    That being said, it seems that you are only dealing with compile-time constants (?), so I don’t understand why you even need malloc. Can’t you do like this?

    static const char MAGIC_COOKIE[] = "z9hG4bk-";
    
    #define MAGIC_COOKIE_LENGTH (sizeof(MAGIC_COOKIE)/sizeof(char))
    
    #define BRANCH_ID_LENGTH (APR_UUID_FORMATTED_LENGTH + MAGIC_COOKIE_LENGTH)
    
    
    static void g_get_branch_id (char branch_id_buf [BRANCH_ID_LENGTH]);
    

    The array length in the prototype indicates to the caller that they should pass a pointer to a buffer with that length.

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

Sidebar

Related Questions

gcc (GCC) 4.6.3 c89 Hello, I am just wondering if this is the best
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 Apache protable runtime 1.4 c89 and compiling in -m32 mode
gcc c89 I am just looking to link against the below shared library. I
gcc (GCC) 4.6.3 c89 I am trying to use usleep . However, I keep
gcc 4.4.3 c89 I am just getting started with log4c. However, there is very
gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2) c89 Hello, I am creating a thread
gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2) c89 Hello, I am wondering if I
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 c89 Just wondering is there a better way to do
gcc 4.4.4 c89 I have a program that I am testing. I create a
gcc 4.5.1 c89 I am trying to free some memory. However, when I check

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.