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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:50:05+00:00 2026-05-24T08:50:05+00:00

Question has been asking before, but I am still a bit at a loss

  • 0

Question has been asking before, but I am still a bit at a loss as to the best way. I have an integer and would like to obtain a char* to use as a member of a struct.

Similar questions are for example
here or here.
I’d rather not use stringstream or lexical_cast. Which as far as I can see basically leaves itoa, sprintf and snprintf.

I guess part of my problem is that I don’t quite know how char pointers work.

  1. If I request a char str[12], that reserves 12 characters?
  2. I presume that memory has then been assigned?
  3. So how does null termination work?
  4. Is that at the end of the 12 characters (with spaces padding?), or
    if the number only uses 2 characters, would it happen after two?
  5. Does the buffer size matter or should I just go with some maximum
    (30 or so I believe for 32 bits)?
  6. And if I use the pointer later in a simple struct, will the memory
    be cleared automatically or do I need a destructor?
  7. Does the destructor need to know the initialize buffer size?
  8. Why does no one bother to calculate the buffer length from the
    actual number?
  9. Is snprintf recommended over itoa?

Thanks

  • 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-24T08:50:07+00:00Added an answer on May 24, 2026 at 8:50 am

    If I request a char str[12], that reserves 12 characters?

    Yes, this reserves 12 characters (bytes) on the stack.

    I presume that memory has then been assigned?

    str now points to the beginning of 12 bytes of contiguous memory that has been allocated on the stack.

    So how does null termination work? Is that at the end of the 12 characters (with spaces padding?), or if the number only uses 2 characters, would it happen after two?

    In this case you have to null-terminate the string yourself (it doesn’t automatically happen with this type of declaration).

    So if you wanted to make str hold some (null-terminated) string value, you would do the following:

    str[0] = 'c';
    str[1] = 'a';
    str[2] = 't';
    str[3] = '\0';
    

    Another way to get a null-terminated string is to do the following:

    char *str = "cat"; // this is null-terminated
    

    There is no automatic padding with spaces.

    And if I use the pointer later in a simple struct, will the memory be cleared automatically or do I need a destructor?

    If the pointer is pointing to a piece of memory allocated on the stack, then the memory will be recovered when the stack is popped. Otherwise, if you allocate memory on the heap such as:

    char *newStr    = new char[12];
    char *mallocStr = (char*) malloc(sizeof(char) * 12);
    

    Then you will have to de-allocate the memory using delete or free (depending on which method you used).

    Does the destructor need to know the initialize buffer size?

    In this context we’re talking about heap-allocated memory and therefore the answer is no. The system knows how much memory was allocated. That’s why you just give the pointer to delete and free without having to provide a size.

    Why does no one bother to calculate the buffer length from the actual number?

    You can if you know that your number will never exceed a certain amount (say, 4 digits), but it’s usually easier to just give the maximum possible length, because in the end you’ll only be wasting a few bytes per string, which isn’t a big deal unless you have some really tight memory constraints.

    Is snprintf recommended over itoa?

    I would say yes because snprintf is considered “safe” because it respects the buffer size that you give it, whereas itoa will happily fill your buffer without checking its size (possibly running past the allocated space and overwriting other memory).

    int   num = 12345;
    char *str = new char[4];
    
    // this is bad because str is not big enough to hold the number
    // itoa does not check buffer size and will overrun the buffer (overwriting other data)
    str = itoa(num, str);
    
    // snprintf takes the buffer size as one of it's arguments
    // in this case the whole number will not fit, but at least we don't overrun the buffer
    snprintf(str, 4, "%i", num);
    

    Both snprintf and itoa will null-terminate the strings for you.

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

Sidebar

Related Questions

This question has been asked before, but I would like a little more detail
This question has been asked before ( link ) but I have slightly different
I realise this question has been asking before, but I've yet to find an
This question has been asked before but never really answered (at least the way
I know this question has been asked before, but I ran into a problem.
I know this specific question has been asked before , but I am not
I think my question has been asked here before, I did read them but
This question has been asked in a C++ context but I'm curious about Java.
A similar question has been asked: MSDN subscriptions on the cheap? , but I
I know the question has been asked before how often to commit with a

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.