buffer sizes

Discussion in 'C++' started by bob, May 14, 2012.

  1. bob

    bob Guest

    Okay. So, I have some code like so:

    void clear_directory(void)
    {
    char folder[100];

    To me it feels a little awkward choosing a semi-arbitrary size like 100.

    Also, I was thinking about the performance of it… Is it true that the performance would be the same if I made the size 1000 like so:

    char folder[1000];

    I think so, since it is using stack space which is already allocated, right?
     
    bob, May 14, 2012
    #1
    1. Advertising

  2. bob

    Guest

    On Monday, May 14, 2012 3:06:24 PM UTC+1, bob wrote:

    > Okay. So, I have some code like so:
    >
    > void clear_directory(void)
    > {
    > char folder[100];


    this isn't very "C++ like". Why not use a std::string or std::vector that will grow as necessary?

    > To me it feels a little awkward choosing a semi-arbitrary size like 100.


    yes. Which with careful use the STL will generally avoid...

    > Also, I was thinking about the performance of it… Is it true that the performance would be the same if I made the size 1000 like so:
    >
    > char folder[1000];
    >
    > I think so, since it is using stack space which is already allocated, right?


    depends what you mean by "performance". Usually "performance" means "how long it takes". Unless you put a really looney value in the performance in this sense will be similar.

    But you seem to mean "will it use more memory". Well it depends. On a typical contempory machine the total memory for the stack will /not/ be allocated until needed. So the one with the larger buffer will use more memory. From habbit I try not to use unnecessary memory but on a typical contempory machine 900 bytes is nothing. Your machine will likely have a few /billion/ bytes to spare.
     
    , May 14, 2012
    #2
    1. Advertising

  3. bob

    Paul N Guest

    On May 14, 3:06 pm, bob <> wrote:
    > Okay.  So, I have some code like so:
    >
    > void clear_directory(void)
    > {
    >     char folder[100];
    >
    > To me it feels a little awkward choosing a semi-arbitrary size like 100.


    Does your system provide a value for the maximum folder length? For
    instance, VC++ has MAX_PATH.
     
    Paul N, May 14, 2012
    #3
  4. wrote:
    > On Monday, May 14, 2012 3:06:24 PM UTC+1, bob wrote:
    >
    >> Okay. So, I have some code like so:
    >>
    >> void clear_directory(void)
    >> {
    >> char folder[100];

    >
    > this isn't very "C++ like". Why not use a std::string or std::vector that will grow as necessary?


    Because if that function gets called a lot, then std::string and std::vector
    will be approximately a hundred times slower.

    (You could make them static to alleviate that problem. However, it would
    still be a bit slower, and it would not be thread-safe.)
     
    Juha Nieminen, May 15, 2012
    #4
  5. bob

    Jorgen Grahn Guest

    On Tue, 2012-05-15, Juha Nieminen wrote:
    > wrote:
    >> On Monday, May 14, 2012 3:06:24 PM UTC+1, bob wrote:
    >>
    >>> Okay. So, I have some code like so:
    >>>
    >>> void clear_directory(void)
    >>> {
    >>> char folder[100];

    >>
    >> this isn't very "C++ like". Why not use a std::string or
    >> std::vector that will grow as necessary?

    >
    > Because if that function gets called a lot, then
    > std::string and std::vector
    > will be approximately a hundred times slower.


    clear_directory() doesn't sound like such a function. This (avoiding
    arrays with all their problems) is IMHO one of those things you should
    do by default, and deviate only when there are good reasons.

    (I also cannot take "approximately a hundred times slower" seriously.
    You don't know what the function does, so you cannot possibly tell.)

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, May 15, 2012
    #5
  6. bob

    Nobody Guest

    On Mon, 14 May 2012 12:20:33 -0700, Paul N wrote:

    > Does your system provide a value for the maximum folder length? For
    > instance, VC++ has MAX_PATH.


    On Unix, the maximum length of a filename or pathname aren't guaranteed to
    be compile-time constants, may vary between filesystems, and may not exist
    (i.e. there isn't guaranteed to be a limit). You're supposed to use
    pathconf() or fpathconf() with _PC_NAME_MAX or _PC_PATH_MAX to query the
    limit.
     
    Nobody, May 16, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raja
    Replies:
    12
    Views:
    24,436
    John Harrison
    Jun 21, 2004
  2. Replies:
    2
    Views:
    607
    sergejusz
    Mar 26, 2007
  3. Giampaolo Rodola'
    Replies:
    3
    Views:
    551
    Greg Copeland
    Nov 15, 2008
  4. Neal Becker

    buffer creates only read-only buffer?

    Neal Becker, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    416
    Neal Becker
    Jan 8, 2009
  5. xingye
    Replies:
    9
    Views:
    276
    Michael Lu
    Apr 19, 2004
Loading...

Share This Page