473,597 Members | 2,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4486

"daniel" <da***********@ gmail.comwrote in message
news:69******** *************** ***********@79g 2000hsk.googleg roups.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*********@ve rizon.netwrote in message
news:rxVnk.739$ 7N1.719@trnddc0 6...
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*********@ve rizon.netwrote in message
news:rxVnk.739$ 7N1.719@trnddc0 6...
>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 "unacceptab ly 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******** *************** ***********@79g 2000hsk.googleg roups.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.ukwro te 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******* *************** ************@79 g2000hsk.google groups.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.co mwrote:
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
3672
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 static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
5
2004
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 message pertaining to the array size and doesnt work. Does it mean that the total size of the array allocated (10000 * 20) should be less than 65535 or something like that ? The compiler help file tells that the size of data type character is 1...
5
3953
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 variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
12
9562
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. ---------------------------------------------------------------------------------------------------- CFile oFile("User.tcx", CFile::modeRead); CRijndael oDecrypt; long size; char* buffer,* buf;
6
3548
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 uncaught exception. How to catch an exception that happened before main() try { ... } catch (...) { ... } block? Is there a way?
19
2118
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 compatibilities with previous definitions of C & C++, it is also a CHAR POINTER a.k.a /char*/. however it is an error, which can not be caught at untill runtime, to modify a string literal using such pointer:
13
6572
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 trying to use from an executable, compile using gcc 4.1.2. Everything works fine until I try specializing one of the static member function templates in a non-template class. I have a feeling I'm messing up something obvious so before I post a...
11
2045
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 work by thrashing the stack to overwrite the return address, allowing arbitrary code execution. I have now fallen into the habit of declaring temporary buffers as static char arrays.
6
482
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} arr={0xaabbff,0xabcdef} so its a 2D array with n rows and 2 columns. I know n, at compile time itself. but I cant do int arr bcos the first index(or rownumber)
0
7971
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7893
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8040
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6698
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3889
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1495
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.