473,725 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storage of char in 64 bit machine

Hi all,

I have a simple definitioin in a C file something like this.

main()
{
char a;
.......
int k;
}

Since character is 8 bit, how is it stored in the machine in a 64 bit
machine. If it is word aligned, what about the rest of the bytes. What
about the retrievel of the char c, will it be expensive. Is it
expensive w.r.t read or write.

Thanx and Regards,
Aruna

Aug 14 '06 #1
74 4574
ar**********@gm ail.com wrote:
Hi all,

I have a simple definitioin in a C file something like this.

main()
{
char a;
.......
int k;
}

Since character is 8 bit, how is it stored in the machine in a 64 bit
machine.
That's up to the compiler, and it depends what you mean by "a 64 bit
machine".
If it is word aligned, what about the rest of the bytes.
What about the rest of what bytes?
What about the retrievel of the char c, will it be expensive.
That depends on the compiler. I wouldn't /expect/ it to be expensive.
`c` might well be stored in a register, for example [1].
Is it expensive w.r.t read or write.
That depends.

Have you a problem for which this would be an explanation?

(Ignoring your `...`s, the compiler could arrange that `main` above
is implemented with no local variables at all ...)

--
Chris "seeker" Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

Aug 14 '06 #2

ar**********@gm ail.com wrote:
Hi all,

I have a simple definitioin in a C file something like this.

main()
{
char a;
.......
int k;
}

Since character is 8 bit,
Actually, a character isn't 8 bit. I'm simplifying a bit, but a
character is guaranteed to be /at least/ 8 bits wide, and is permitted
to be as wide as necessary. For all we (or you) know, a char might be
64bits wide on your platform.
how is it stored in the machine in a 64 bit
machine. If it is word aligned, what about the rest of the bytes. What
about the retrievel of the char c, will it be expensive. Is it
expensive w.r.t read or write.
Storage and alignment are the concerns of the compiler and the
"execution platform". It /could/ be that this compiler does as you
suspect, and uses 8 bits out of 64 to store a char entity, leaving the
remaining bits unused and unusable.

Alternatively, the compiler /may/ reorganize your allocations (at any
one level) such that all the small entities are grouped together in
storage, permitting other char values to occupy the "slack" space from
your allocation of "char a;".

This is entirely up to the implementation of the compiler; AFAICR, the
C standard doesn't require any specific behaviour in this regard. If
the compiler's code organization and optimization are a problem for
you, you'll either have to switch compilers or change your code to live
more optimally within the restrictions that the compiler places on you.
Thanx and Regards,
Aruna
You're welcome
--
Lew Pitcher

Aug 14 '06 #3
In article <11************ **********@b28g 2000cwb.googleg roups.com>,
Lew Pitcher <lp******@sympa tico.cawrote:
>Alternativel y, the compiler /may/ reorganize your allocations (at any
one level) such that all the small entities are grouped together in
storage, permitting other char values to occupy the "slack" space from
your allocation of "char a;".
>This is entirely up to the implementation of the compiler; AFAICR, the
C standard doesn't require any specific behaviour in this regard.
I'm not sure what you mean by "at any one level".

Note that compilers are not permitted to reorder fields in a struct,
only to put padding between the fields. (I would tend to think
that fields of any one struct are all at the same "level", provided
they are not aggregate types.)
--
Programming is what happens while you're busy making other plans.
Aug 14 '06 #4

Walter Roberson wrote:
In article <11************ **********@b28g 2000cwb.googleg roups.com>,
Lew Pitcher <lp******@sympa tico.cawrote:
Alternatively, the compiler /may/ reorganize your allocations (at any
one level) such that all the small entities are grouped together in
storage, permitting other char values to occupy the "slack" space from
your allocation of "char a;".
This is entirely up to the implementation of the compiler; AFAICR, the
C standard doesn't require any specific behaviour in this regard.

I'm not sure what you mean by "at any one level".
I wasn't too clear there, so let me elaborate

Assume the code fragment...

{
/* "level" A */
char aa; int ab, ac;
char ad;

{
/* "level" B */
char ae;

}
}

In the nesting level I've called "A", the compiler /may/ optimize the
allocations of aa, ab, ac, and ad so that aa and ad are adjacent in
"memory". For the OP's example of 8-bit char data items and 64bit
wordsizes, this could mean that 16bits of one 64bit word is occupied by
2 independant char data items, wasting only 48 bits of hidden padding
(assuming that the compiler word-aligns each allocation). The OPs
scenario would have each char data item (aa and ad) possible occupy 8
bits of unique 64bit words, leaving 112 bits (2 x 56) unused.

However, because variable ae is declared within a different "level" of
the code, I doubt that most compilers would "optimize" its allocation
to occupy another 8 bits within that 64bit allocation that aa and ad
potentially occupy.

That's what I meant by "at any one level"
Note that compilers are not permitted to reorder fields in a struct,
only to put padding between the fields. (I would tend to think
that fields of any one struct are all at the same "level", provided
they are not aggregate types.)
Up to this point, structure (and union) allocations haven't been part
of the discussion. Your point is taken; the compiler isn't permitted to
reorder fields within a structure, even in order to take advantage of
the potential space savings that such a reorganization might offer.

--
Lew Pitcher

Aug 14 '06 #5
Lew Pitcher wrote:
Actually, a character isn't 8 bit. I'm simplifying a bit, but a
character is guaranteed to be /at least/ 8 bits wide, and is permitted
to be as wide as necessary. For all we (or you) know, a char might be
64bits wide on your platform.
So, comparing, say, 4-char arrays (like currency codes) can NOT be done in
the following way?

typedef union {
char acCUR[4];
int32_t iCUR;
} xCUR;

int
CurEqual(xCUR *c1, xCUR *c2)
{
if (c1->iCUR == c2->iCUR)
printf("Same currency %s\n", c1->acCUR);
else
printf("%s and %s are different\n",
c1->acCUR, c2->acCUR);
}

Having to call a strcmp() in such cases seems like a bad waste to me, but I
don't see, how the compiler could possibly optimize such a code without the
trick above...

-mi
Aug 14 '06 #6
In article <1953472.hIqUUy 82Sv@misha>,
Mikhail Teterin <us****@aldan.a lgebra.comwrote :
>Lew Pitcher wrote:
>Actually, a character isn't 8 bit. I'm simplifying a bit, but a
character is guaranteed to be /at least/ 8 bits wide, and is permitted
to be as wide as necessary. For all we (or you) know, a char might be
64bits wide on your platform.
>So, comparing, say, 4-char arrays (like currency codes) can NOT be done in
the following way?
typedef union {
char acCUR[4];
int32_t iCUR;
} xCUR;
Note that int32_t is not certain to exist at all. There is an
int_least32_t that is certain to be at least 32 bits, and that will
exist on all C99 platforms.

sizeof int32_t will tell you how many "bytes" int32_t requires, and
by definition each char is exactly one byte long. However, it should
not be assumed that int32_t and char acCUR[sizeof int32_t] both
offer the same number of bits of "useable" storage, as the signed
int types are permitted to have internal non-value bits. When
you are trying to do type-punning via unions, you should use
unsigned char to be sure to be able to access all bits (including
the ones that the other fields might happen to treat as non-value bits.)
--
All is vanity. -- Ecclesiastes
Aug 14 '06 #7

Mikhail Teterin wrote:
Lew Pitcher wrote:
Actually, a character isn't 8 bit. I'm simplifying a bit, but a
character is guaranteed to be /at least/ 8 bits wide, and is permitted
to be as wide as necessary. For all we (or you) know, a char might be
64bits wide on your platform.

So, comparing, say, 4-char arrays (like currency codes) can NOT be done in
the following way?

typedef union {
char acCUR[4];
int32_t iCUR;
} xCUR;
Not portably, no.

int32_t isn't guaranteed to exist in every compliant compilation system

CHAR_BITS isn't guaranteed to be equal to 8 in every compliant
compilation system

char acCUR[4]; isn't guaranteed to contain 32 bits in every compliant
compilation system

Aug 14 '06 #8
Mikhail Teterin wrote:
Lew Pitcher wrote:
>Actually, a character isn't 8 bit. I'm simplifying a bit, but a
character is guaranteed to be /at least/ 8 bits wide, and is permitted
to be as wide as necessary. For all we (or you) know, a char might be
64bits wide on your platform.

So, comparing, say, 4-char arrays (like currency codes) can NOT be done in
the following way?

typedef union {
char acCUR[4];
int32_t iCUR;
} xCUR;

int
CurEqual(xCUR *c1, xCUR *c2)
{
if (c1->iCUR == c2->iCUR)
printf("Same currency %s\n", c1->acCUR);
else
printf("%s and %s are different\n",
c1->acCUR, c2->acCUR);
}
It definitely cannot. Apart from the fact that int could be only 16 bits
it could also have a trap representation, or two representations of the
same value (+0 and -0).
Having to call a strcmp() in such cases seems like a bad waste to me, but I
don't see, how the compiler could possibly optimize such a code without the
trick above...
It could optimise it very easily using strcmp (inline the function and
then optimise it in place with the rest of the code). Potentially it
could be *more* efficient since it might compare fewer bytes.

If you think something like that is an optimisation worth considering
then you should not attempt *any* optimisation since at best you are
likely to have no effect or make it slower, but you are just as likely
to break it. Write code to be simple and understandable and let the
optimisation phase of the compiler do its job, it's probably a lot
better at it than you.
--
Flash Gordon
Still sigless on this computer.
Aug 14 '06 #9


Mikhail Teterin wrote On 08/14/06 13:50,:
Lew Pitcher wrote:

>>Actually, a character isn't 8 bit. I'm simplifying a bit, but a
character is guaranteed to be /at least/ 8 bits wide, and is permitted
to be as wide as necessary. For all we (or you) know, a char might be
64bits wide on your platform.


So, comparing, say, 4-char arrays (like currency codes) can NOT be done in
the following way?

typedef union {
char acCUR[4];
int32_t iCUR;
} xCUR;

int
CurEqual(xCUR *c1, xCUR *c2)
{
if (c1->iCUR == c2->iCUR)
printf("Same currency %s\n", c1->acCUR);
else
printf("%s and %s are different\n",
c1->acCUR, c2->acCUR);
}

Having to call a strcmp() in such cases seems like a bad waste to me, but I
don't see, how the compiler could possibly optimize such a code without the
trick above...
The trick will work on many machines, but you're right:
it works "by chance, not by design."

But is the loss worth weeping over? Consider: You've
saved a strcmp() of two short strings, but at what cost?
If your currency codes are "naturally" strings, you've now
got to bundle them up into xCUR unions; you must actually
copy the string characters into the acCUR members. This
uglifies your code -- and avoiding a strcmp() at the cost
of two calls to strcpy() doesn't seem like a step in the
right direction!

If you're making "a lot" of these comparisons, it might
make more sense to convert the strings to numeric codes at
the point when they're read in or whatever. You're probably
going to validate the strings by looking them up in a table
of "known" currency codes or some such, right? Having done
the lookup, it's pretty easy to get the table to provide an
easily-manipulated numeric code that can be used elsewhere in
the program; you'd just deal with strings "on the periphery."
That'd be cleaner, probably faster, and certainly more portable.

--
Er*********@sun .com

Aug 14 '06 #10

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

Similar topics

4
2552
by: David Garamond | last post by:
Is it the 4+N (aka. same as VARCHAR(n)) or is it N? Sorry, it was 100% not clear for me after reading the docs, though the docs imply the first: "The storage requirement for data of these types is 4 bytes plus the actual string, and in case of character plus the padding." As a comparison, MySQL seems to do storage saving for fixed-length character (it doesn't store the length of the string). -- dave
5
3859
by: aneesh | last post by:
Hi all, I have a program, this works fine but if we declare static below "int i" it shows different storage class specifier. what will be the reason. #include <stdlib.h> static int i ; int i; int main()
7
6144
by: Jim Showalter | last post by:
I always thought that it is safe for a function to return a pointer to static storage. And the following code does compile quietly with: gcc -pedantic -Wall -o foo foo.c #include <stdio.h> static char *foo (int y) { static char s;
13
1655
by: S.Tobias | last post by:
I'm examining the existence of temporary objects by looking at their addresses. The trick is to create a structure that contains an array as its first member. In an expression the array rvalue is converted to a pointer to its first member. Since this address is also the address of the array, and that is the address of the structure, I conclude that this is also the address of the temporary storage for the structure (r)value. I'm...
3
2724
by: Bas Wassink | last post by:
Hello there, I'm having trouble understanding a warning produced by 'splint', a code-checker. The warning produced is: keywords.c: (in function keyw_get_string) keywords.c:60:31: Released storage Keywords.Keyword reachable from global A global variable does not satisfy its annotations when control is transferred. (Use -globstate to inhibit warning) keywords.c:60:11: Storage Keywords.Keyword released
9
2437
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers, strings for logging, and structures for data points. I want to use one function to read data from this block and one function to write data, for example: sram_read(OBJECT_IDENTIFIER) would return a pointer to the appriate object and
0
1879
by: Namratha Shah \(Nasha\) | last post by:
Hey Group, After a long week end I am back again. Its nice and refreshing after a short vacation so lets get started with .NET once again. Today we will discuss about Isolated Storage. This is one of the topics which I find interesting as I feel that it has a lot of practical usage or applicability. We all know that all applications need some storage space to archive certain
7
2274
by: lithiumcat | last post by:
Hi, I'm not yet very confident in my use of standard terminology, so please be kind if I'm mis-calling something, I will do my best no to make it again once pointed out. I'm wondering what is the lifetime or a compile-time string constant, I think that is what is called the storage duration of a string litteral.
0
8888
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
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9111
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8096
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
6702
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
4517
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
3221
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
2
2634
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.