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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:54:53+00:00 2026-05-15T13:54:53+00:00

As a C++ programmer I sometimes need deal with memory buffers using techniques from

  • 0

As a C++ programmer I sometimes need deal with memory buffers using techniques from C. For example:

char buffer[512];
sprintf(buffer, "Hello %s!", userName.c_str());

Or in Windows:

TCHAR buffer[MAX_PATH+1]; // edit: +1 added
::GetCurrentDirectory(sizeof(buffer)/sizeof(TCHAR), &buffer[0]);

The above sample is how I usually create local buffers (a local stack-allocated char array). However, there are many possible variations and so I’m very interested in your answers to the following questions:

  • Is passing the buffer as &buffer[0] better programming style than passing buffer? (I prefer &buffer[0].)
  • Is there a maximum size that is considered safe for stack allocated buffers?
    • Update: I mean, for example, the highest value that can be considered safe for cross-platform desktop applications on Mac, Windows, Linux desktops (not mobile!).
  • Is a static buffer (static char buffer[N];) faster? Are there any other arguments for or against it?
  • When using static buffers you can use return type const char *. Is this (generally) a good or a bad idea? (I do realize that the caller will need to make his own copy to avoid that the next call would change the previous return value.)
  • What about using static char * buffer = new char[N]; , never deleting the buffer and reusing it on each call.
  • I understand that heap allocation should be used when (1) dealing with large buffers or (2) maximum buffer size is unknown at compile time. Are there any other factors that play in the stack/heap allocation decision?
  • Should you prefer the sprintf_s, memcpy_s, … variants? (Visual Studio has been trying to convince me of this for a long time, but I want a second opinion :p )
  • 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-15T13:54:54+00:00Added an answer on May 15, 2026 at 1:54 pm

    I assume your interest comes about primarily from a performance perspective, since solutions like vector, string, wstring, etc. will generally work even for interacting with C APIs. I recommend learning how to use those and how to use them efficiently. If you really need it, you can even write your own memory allocator to make them super fast. If you are sure they’re not what you need, there’s still no excuse for you to not write a simple wrapper to handle these string buffers with RAII for the dynamic cases.

    With that out of the way:

    Is passing the buffer as &buffer[0]
    better programming style than passing
    buffer? (I prefer &buffer[0].)

    No. I would consider this style to be slightly less useful (admittedly being subjective here) as you cannot use it to pass a null buffer and therefore would have to make exceptions to your style to pass pointers to arrays that can be null. It is required if you pass in data from std::vector to a C API expecting a pointer, however.

    Is there a maximum size that is
    considered safe for stack allocated
    buffers?

    This depends on your platform and compiler settings. Simple rule of thumb: if you’re in doubt about whether your code will overflow the stack, write it in a way which can’t.

    Is a static buffer (static char
    buffer[N];) faster? Are there any
    other arguments for or against it?

    Yes, there is a big argument against it, and that is that it makes your function no longer re-entrant. If your application becomes multithreaded, these functions will not be thread safe. Even in a single-threaded application, sharing the same buffer when these functions are recursively called can lead to problems.

    What about using static char * buffer
    = new char[N]; and never deleting the buffer? (Reusing the same buffer each
    call.)

    We still have the same problems with re-entrancy.

    I understand that heap allocation
    should be used when (1) dealing with
    large buffers or (2) maximum buffer
    size is unknown at compile time. Are
    there any other factors that play in
    the stack/heap allocation decision?

    Stack unwinding destroys objects on the stack. This is especially important for exception-safety. Thus even if you allocate memory on the heap within a function, it should generally be managed by an object on the stack (ex: smart pointer). ///@see RAII.

    Should you prefer the sprintf_s,
    memcpy_s, … variants? (Visual Studio
    has been trying to convince me of this
    for a long time, but I want a second
    opinion :p )

    MS was right about these functions being safer alternatives since they don’t have buffer overflow problems, but if you write such code just as is (without writing variants for other platforms), your code will be married to Microsoft since it will be non-portable.

    When using static buffers you can use
    return type const char *. Is this
    (generally) a good or a bad idea? (I
    do realize that the caller will need
    to make his own copy to avoid that the
    next call would change the previous
    return value.)

    I’d say in almost every case, you want to use const char* for return types for a function returning a pointer to a character buffer. For a function to return a mutable char* is generally confusing and problematic. Either it’s returning an address to global/static data which it shouldn’t be using in the first place (see re-entrancy above), local data of a class (if it’s a method) in which case returning it ruins the class’s ability to maintain invariants by allowing clients to tamper with it however they like (ex: stored string must always be valid), or returning memory that was specified by a pointer passed in to the function (the only case where one might reasonably argue that mutable char* should be returned).

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

Sidebar

Ask A Question

Stats

  • Questions 527k
  • Answers 527k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Types with that kind of name are generated when you… May 16, 2026 at 10:43 pm
  • Editorial Team
    Editorial Team added an answer repr(obj) calls obj.__repr__ the purpose of __repr__ is that it… May 16, 2026 at 10:43 pm
  • Editorial Team
    Editorial Team added an answer This code seems to work for me var sys =… May 16, 2026 at 10:43 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm a former classic ASP programmer and sometimes PHP programmer writing my first ASP.NET
Sometimes it would be useful to name variables like no programmer should name his
I am looking for a better solution than what we currently have to deal
I'm working with websites written in PHP, along with many other programmers, and sometimes
Im working on an iPhone app, not using IB, and programmatically created a UITabbar
This has to be a common question that all programmers have from time to
Let me start by saying that I'm not a power-JavaScript/jQuery programmer, I do very
I am tired of asking unanswered questions when using many outdated plugins / gems,
As a programmer, I have bought whole-heartedly into the TDD philosophy and take the
My personal experience is that regexs solve problems that can't be efficiently solved any

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.