buffer sizes

B

bob

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?
 
G

Guest

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.
 
P

Paul N

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.
 
J

Juha Nieminen

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.)
 
J

Jorgen Grahn

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
 
N

Nobody

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top