473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

static char arrays size

Hello ,
I always had the feeling that is better to have char arrays with the
size equal to a power of two.

For example:
char a_str[128]; // feels ok
char b_str[100]; //feels not ok.

Is that true , and if is true then what is the reason behind this? ( I
suppose that is something related to memory alignment but it's not
very clear in my mind ).

A detailed explanation would be very useful.

Thx,
Daniel.
Aug 11 '08 #1
7 4447

"daniel" <da***********@gmail.comwrote in message
news:69**********************************@79g2000h sk.googlegroups.com...
Hello ,
I always had the feeling that is better to have char arrays with the
size equal to a power of two.

For example:
char a_str[128]; // feels ok
char b_str[100]; //feels not ok.

Is that true , and if is true then what is the reason behind this? ( I
suppose that is something related to memory alignment but it's not
very clear in my mind ).
You're thinking about array element sizes, where the calculation of the
address of a[i] (or the offset from a[0]) can be simpler when the size of
each element of a is a power of two.

(Although alignment can also come into it, when the element is a struct for
example. But this should be taken care of for you.)

--
Bartc

Aug 11 '08 #2
daniel wrote:
Hello ,
I always had the feeling that is better to have char arrays with the
size equal to a power of two.

For example:
char a_str[128]; // feels ok
char b_str[100]; //feels not ok.

Is that true , and if is true then what is the reason behind this? ( I
suppose that is something related to memory alignment but it's not
very clear in my mind ).

A detailed explanation would be very useful.
An array should always be big enough to hold the biggest thing that you
plan to put in it. Making it any bigger than that is, in general,
wasteful. Therefore, you only have a choice to make if you're still at
the point in the design process where you get to decide what you plan to
put into that array, and where the answer to that question is not forced
upon you by other considerations.

In that case, a length that is a power of 2 does indeed provide a minor
benefit. Many computer systems push around data more efficiently when it
has a size which is a multiple of a certain number of bytes. There's no
guarantees that any such sizes exist, and there's no guarantees that
they will be powers of 2. However, as a practical matter the special
sizes are almost always powers of 2.

Since every power of 2 is a multiple of all smaller powers of 2,
rounding an array size up to the next power of 2 is a choice that in
general won't hurt, an on many systems will slightly improve the
efficiency of your code. However, I would strongly recommend not making
a big deal about this. Just about any other reason for choosing a
particular size for the array is going to be more important than this
one. Use this criterion only when all other constraints still leave you
with a choice to make.

Please note that this doesn't apply just to arrays of char; it applies
to all arrays.

Aug 11 '08 #3

"James Kuyper" <ja*********@verizon.netwrote in message
news:rxVnk.739$7N1.719@trnddc06...
daniel wrote:
>Hello ,
I always had the feeling that is better to have char arrays with the
size equal to a power of two.

For example:
char a_str[128]; // feels ok
char b_str[100]; //feels not ok.

Is that true , and if is true then what is the reason behind this? ( I
suppose that is something related to memory alignment but it's not
very clear in my mind ).

A detailed explanation would be very useful.
In that case, a length that is a power of 2 does indeed provide a minor
benefit. Many computer systems push around data more efficiently when it
has a size which is a multiple of a certain number of bytes.
I don't quite understand how having an unused 28 bytes on the end of a
100-byte array is going to help.

If a machine can do 8-byte moves then there might be a case for a 104-byte
array size instead of 100, because of the fiddly 4 bytes at the end, but no
need for 128. That's assuming C can move around entire arrays, which it
can't directly. And anyway this is a matter for the compiler to worry about.
Since every power of 2 is a multiple of all smaller powers of 2, rounding
an array size up to the next power of 2 is a choice that in general won't
hurt, an on many systems will slightly improve the efficiency of your
code.
Rounding a 1048577-byte array up to 2097152 bytes I think would definitely
hurt.

--
Bartc

Aug 11 '08 #4
Bartc wrote:
>
"James Kuyper" <ja*********@verizon.netwrote in message
news:rxVnk.739$7N1.719@trnddc06...
>daniel wrote:
>>Hello ,
I always had the feeling that is better to have char arrays with the
size equal to a power of two.

For example:
char a_str[128]; // feels ok
char b_str[100]; //feels not ok.

Is that true , and if is true then what is the reason behind this? ( I
suppose that is something related to memory alignment but it's not
very clear in my mind ).

A detailed explanation would be very useful.
>In that case, a length that is a power of 2 does indeed provide a
minor benefit. Many computer systems push around data more efficiently
when it has a size which is a multiple of a certain number of bytes.

I don't quite understand how having an unused 28 bytes on the end of a
100-byte array is going to help.
It won't. The only case where you should ever use this criterion for
deciding for deciding the length of the array, is if you're still at the
point of deciding whether to design the program to work with a maximum
of 100 bytes, or a maximum of 128 bytes. I'm assuming that, regardless
of what decision is made, there will be cases which use the remaining 28
bytes; cases which, at that point of the design cycle, you're still free
to decide whether or not the program should handle those cases as
normal, or as exceptions to be handled by some other method.

Prime example: fields meant to hold people's names. There's no
reasonable fixed length that can deal with every possible name. If
variable-length fields are not acceptable (which is often the case),
then the exact length you should use is fairly arbitrary, and might as
well be chosen based in part upon the minor efficiencies of power-of-2.
If a machine can do 8-byte moves then there might be a case for a
104-byte array size instead of 100, because of the fiddly 4 bytes at the
end, but no need for 128.
You're assuming 8-bytes is the special number, and for some systems, for
some purposes, it is. However, there's many different sizes that carry
special advantages, for different purposes. I've known the technical
details of systems where sizes of 16 bytes, 128 bytes, 256 bytes and
1024 bytes have been relevant, for a variety of different reasons. When
I consider the small number of machines which I'm personally familiar
with, compared to the huge variety of real-world machines for which C
code is written, I wouldn't recommend ignoring the possibility that any
particular power of 2 might be relevant on some system, somewhere. On
the flip side, I wouldn't recommend attaching any great significance to
that possibility, either.
That's assuming C can move around entire
arrays, which it can't directly.
I'm not sure what you mean by that comment. I use memcpy() frequently
for that purpose. Some versions of memcpy() do work more efficiently on
objects which have particular alignments, and a size which is a multiple
of that alignment, which is usually a power of 2.
... And anyway this is a matter for the
compiler to worry about.
The issue I'm thinking about cannot be decided by the compiler; it is
inherently exclusively in the domain of the designer of a program. You
may be thinking of a different issue than I am.
>Since every power of 2 is a multiple of all smaller powers of 2,
rounding an array size up to the next power of 2 is a choice that in
general won't hurt, an on many systems will slightly improve the
efficiency of your code.

Rounding a 1048577-byte array up to 2097152 bytes I think would
definitely hurt.
If you know that 1048577 bytes is guaranteed to be sufficient, that's
the size you should use. The requirements for reasonable use of this
reason for selecting the size of an array to be pow(2,N) are as follows:
the chance that a size greater than 'n' will be needed is unacceptably
high for n==pow(2,N-1), and is acceptably low at n==pow(2,N+1), and the
transition point between "unacceptably high" and "acceptably low" is
sufficiently poorly known to justify estimating it as pow(2,N).

To use my example above; if I had a complete list of every name that
might ever be entered in my database, or at least a statistical summary
of the characteristics of such a list, and sufficient knowledge of how
those names might be used, I could put together a mathematical model
that calculates the costs and benefits of using different lengths for
the name field, and solve that model to determine the optimal length. If
the issue was sufficiently important, that is precisely what I would do,
and I would set the field length to that optimum. However, it's
relatively rare to have that kind of information available for free-form
text fields, and it's not unusual to have a similar lack of information
for other kinds of arrays. Even if it is technically feasible to collect
the relevant information, it might be unacceptably expensive. Under such
circumstances, rounding a rough estimate of the space needed to the next
power of 2 is entirely reasonable.
Aug 11 '08 #5
Bartc wrote, On 11/08/08 11:57:
>
"daniel" <da***********@gmail.comwrote in message
news:69**********************************@79g2000h sk.googlegroups.com...
>Hello ,
I always had the feeling that is better to have char arrays with the
size equal to a power of two.

For example:
char a_str[128]; // feels ok
char b_str[100]; //feels not ok.

Is that true , and if is true then what is the reason behind this? ( I
suppose that is something related to memory alignment but it's not
very clear in my mind ).

You're thinking about array element sizes, where the calculation of the
address of a[i] (or the offset from a[0]) can be simpler when the size of
each element of a is a power of two.
I've not come across that being an issue.
(Although alignment can also come into it, when the element is a struct
for example. But this should be taken care of for you.)
Indeed.

The one (and only) time where I seriously go for a power of two size is
when I'm implementing a circular buffer. Then I know that my "modulus
buffer_size" operation can be reduced to applying a simple mask. I still
use the modulus operator and leave it up to the compiler to optimise it
so that if someone changes the buffer size it all continues to work even
if the efficiency is reduced.
--
Flash Gordon
Aug 11 '08 #6
"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message
news:bg************@news.flash-gordon.me.uk...
Bartc wrote, On 11/08/08 11:57:
>>
"daniel" <da***********@gmail.comwrote in message
news:69**********************************@79g2000 hsk.googlegroups.com...
>>Hello ,
I always had the feeling that is better to have char arrays with the
size equal to a power of two.

For example:
char a_str[128]; // feels ok
char b_str[100]; //feels not ok.

Is that true , and if is true then what is the reason behind this? ( I
suppose that is something related to memory alignment but it's not
very clear in my mind ).

You're thinking about array element sizes, where the calculation of the
address of a[i] (or the offset from a[0]) can be simpler when the size of
each element of a is a power of two.

I've not come across that being an issue.
It can mean the difference between having to multiply to get at an array
element, and simply shifting (or, sometimes, just using a scale factor in
the address mode of the output code).
The one (and only) time where I seriously go for a power of two size is
when I'm implementing a circular buffer. Then I know that my "modulus
buffer_size" operation can be reduced to applying a simple mask. I still
use the modulus operator and leave it up to the compiler to optimise it so
that if someone changes the buffer size it all continues to work even if
the efficiency is reduced.
I sometimes do the same thing for hashtables.

But unless there's a good reason for it, there's no particular need for
power-of-two array sizes.

In the past I did do this a lot, for good reasons (for example an array of
4096 bytes would exactly fit into one page of my pseudo-virtual memory
system, or I had exactly N bits available for indexing so might as well use
a full 2**N elements).
--
Bartc

Aug 11 '08 #7
On Aug 11, 9:44*pm, "Bartc" <b...@freeuk.comwrote:
It can mean the difference between having to multiply to get at an array
element, and simply shifting (or, sometimes, just using a scale factor in
the address mode of the output code).
The size of the array wouldn't make a difference to the code needed to
access an array element. Where it makes a difference is two-
dimensional arrays, or an array of structs where you can by design
influence whether the struct size is a power of two or not.

HOWEVER, there are processors around that really dislike consecutive
memory accesses where the distance between consecutive accesses is a
large power of two. An example that I found was one platform where I
measured the time for a straightforward matrix multiplication, and
multiplying two matrices of size 128 x 128 took seven times longer
than the case 127 x 127 or 129 x 129.
Aug 12 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
5
by: Crimzon | last post by:
I am using MSVC++ 6.0 compiler. I am declaring an array char ch. Program works fine for these arbitrary sizes. But if I make the size of the array bigger like ch, the program gives me an error...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: leonard.guillaume | last post by:
Hi guys, I use dynamic char arrays and I'm trying to get rid of the garbage in it. Let me show you the code and then I'll explain more in details. ...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
19
by: arnuld | last post by:
i am trying to understand arrays and char. Stroustrup says, this a string literal: "this is a string literal" he says it is of type /const char/. he also says, because of keeping...
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
11
by: rep_movsd | last post by:
Hi I program primarily in C++ , but once in a while one is forced to use the odd strcpy or call API functions that dump results into char* buffers. I believe that most security exploits that...
6
by: lazy | last post by:
Hi, Im trying to define a hashtable thats static(meaning the table is initialised at compile time itself) It takes a char and has 2 other ints as values For eg: arr={1,2}...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.