Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > buffer sizes

Reply
Thread Tools

buffer sizes

 
 
bob
Guest
Posts: n/a
 
      05-14-2012
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?
 
Reply With Quote
 
 
 
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      05-14-2012
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.


 
Reply With Quote
 
 
 
 
Paul N
Guest
Posts: n/a
 
      05-14-2012
On May 14, 3:06*pm, bob <b...@coolfone.comze.com> 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.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      05-15-2012
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.)
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      05-15-2012
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 .
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      05-16-2012
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Win 7 changing font sizes without icon sizes? why? Computer Support 0 03-21-2010 11:32 AM
Re: Win 7 changing font sizes without icon sizes? why? Computer Support 0 03-21-2010 11:31 AM
Determine the best buffer sizes when using socket.send() andsocket.recv() Giampaolo Rodola' Python 3 11-15-2008 02:34 AM
The File Sizes of Pictures on my CDs Increased to Unreadable Sizes Marful Computer Support 11 03-08-2006 07:13 PM
Cisco 2514 - Changing controller RX and TX ring and buffer sizes David J Edgar Cisco 3 07-10-2003 12:46 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57