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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:04:43+00:00 2026-05-27T16:04:43+00:00

I have a function that is given two integers and returns a string. Right

  • 0

I have a function that is given two integers and returns a string. Right now I have this:

char* myfunc( int a, int b, int* len )
{
    int retLen = ...
    char* ret = malloc( retLen + 1 );

    if ( len != NULL )
    {
        *len = retLen;
    }

    return ret;
}

However, most functions in the C library tend to do something more like:

int myfunc( char* ret, int a, int b )
{
    ...

    return retLen;
}

You are then expected to allocate the memory for the function to fill. This allows you to do a bit more like choose where the string is allocated.

In this case though there is some maths required in the function to get the length, and there is no reason to have a buffer of any size other than the one needed. There is no upper limit on the size of the buffer (not one that is reasonable anyway).

What is considered good practice when returning a string whose length is dynamically found given the inputs?

  • 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-27T16:04:44+00:00Added an answer on May 27, 2026 at 4:04 pm

    A pattern I saw in kernel-mode programs is:

    1. You call the function once, with some allocated memory if you happen to have some available, or null as param if you happen to have none
    2. If you had memory allocated and the function found it enough it puts the result in that memory and returns OK
    3. If you had no memory to pass in, or the memory passed was too little, the function returns ERROR_NOT_ENOUGH_MEMORY, and puts in an output parameter the needed memory.
      • You then allocate this needed memory and call the function again

    Sample:

    int myfunc(
        __out char*  output, 
        __in  size_t given, 
        __out size_t needed_or_resulted, 
        extra params ...
    ){
        ... implementation
    }
    

    The needed_or_resulted can be also used to transmit how much of the given memory was used in case of success.

    To be used like:

    int result = myfunc(output, given, needed_or_resulted, extra params ...);
    if(result == OK) {
        // all ok, do what you need done with result of size "needed_or_resulted" on "output"
    } else if(result == ERROR_NOT_ENOUGH_MEMORY) {
        output = malloc(needed ...
        result = myfunc(output, given, needed_or_resulted, extra params ...);
        if(result == OK) {
            // all ok, do what you need done with result of size "needed_or_resulted" on "output"
        } else if(result == ERROR_OTHER) {
            // handle other possible errors
        } else {
            // handle unknown error
        }
    } else if(result == ERROR_OTHER) {
        // handle other possible errors
    } else {
        // handle unknown error
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have C function that takes string pointer as a parameter. This functions returns
I have a function that processes a given vector, but may also create such
So I have function that formats a date to coerce to given enum DateType{CURRENT,
I have written a function that gets a given number of random records from
I have a function that takes, amongst others, a parameter declared as int privateCount
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have a SQL function called get_forecast_history(integer,integer) that takes two arguments, a month and
I have a function f that takes two parameters ( p1 and p2 ):
I'm trying to write a function that, given two quadtrees representing images, will output
I have two function that do the exact same thing in two different ways.

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.