473,756 Members | 3,655 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
74 4589
Eric Sosman wrote:
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.
Yes, this is an alternative... But being able to access them as strings at
the same time (such as for printing) remains desirable.
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."
The strings come from database, actually, and have to be memcpy-ied once
anyway. Treating them as 4-byte integers would be a nice convenience not
only for comparisions (of which the code does a lot indeed), but also for
future assignments (`trade1.cur.iC ur = trade2.cur.iCur ' vs.
`strcpy(trade1. cur, trade2.cur)').

Keeping an internal table of numbers vs. strings is rather inconvenient when
debugging (can't just look at the string), and also wasteful -- the four
bytes is already an overkill (2^32 possible currencies, when the maximum is
really under a thousand)...

As so often happens, the really cool things C can do are "not supported" and
frowned upon by the authoritatively-sounding gurus...

Thanks,

-mi
Aug 14 '06 #11
"Mikhail Teterin" <us****@aldan.a lgebra.comwrote in message
news:1953472.hI qUUy82Sv@misha. ..
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;
....
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...
That'll work on most modern systems, but it's not portable and it won't work
everywhere. Use strcmp(); that's guaranteed to work.

The implementation is likely to have a very, very clever strcmp() that will
perform at least as well as your code (possibly doing the same thing
internally, if it's known to be safe) and likely even better if the compiler
is reasonably modern due special knowledge and treatment of common
functions/idioms.

Remember, premature optimization is the root of all evil. Avoid the
temptation until profiling shows the clear, less "clever" option isn't fast
enough -- and then consider using a better algorithm, if possible, before
using unportable code.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

--
Posted via a free Usenet account from http://www.teranews.com

Aug 14 '06 #12
Flash Gordon wrote:
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;
<snip>
It definitely cannot. Apart from the fact that int could be only 16 bits
<snip>

Oops. I missed that it was int32_t! Obviously that will be 32 bits if it
exists, but it might not exist.

My comments about optimisation stand though. Don't do it. Experts only
consider micro-optimisation if they *know* they have a problem,
otherwise they write the code to be understandable.
--
Flash Gordon
Still sigless on this computer.
Aug 14 '06 #13


Mikhail Teterin wrote On 08/14/06 15:34,:
Eric Sosman wrote:
>>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.


Yes, this is an alternative... But being able to access them as strings at
the same time (such as for printing) remains desirable.
printf ("Currency = %s\n" CurrencyName[index]);

or maybe

printf ("Currency = %s\n", Currency[index].name);
>>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."


The strings come from database, actually, and have to be memcpy-ied once
anyway. Treating them as 4-byte integers would be a nice convenience not
only for comparisions (of which the code does a lot indeed), but also for
future assignments (`trade1.cur.iC ur = trade2.cur.iCur ' vs.
`strcpy(trade1. cur, trade2.cur)').

Keeping an internal table of numbers vs. strings is rather inconvenient when
debugging (can't just look at the string), and also wasteful -- the four
bytes is already an overkill (2^32 possible currencies, when the maximum is
really under a thousand)...
I think that simply adds support to my suggestion: Instead of
maintaining a lot of four-byte strings, you could be storing a lot of
two-byte (probably) `short' table indices.
As so often happens, the really cool things C can do are "not supported" and
frowned upon by the authoritatively-sounding gurus...
Yeah, well. One might equally opine that only the spoiled rotten
button-clicking GUI-besotted drooling whining spaced-out brainless --
oh, sorry, didn't mean to offend -- um, only "a few extraordinary
people" consider such trickery "really cool."

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

Aug 14 '06 #14
"Lew Pitcher" <lp******@sympa tico.cawrites:
Walter Roberson wrote:
>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".

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"
There are several possible strategies for allocating objects in nested
and/or parallel blocks. For example:

void foo(void)
{
int outer;
{
int inner1;
}
{
int inner2a;
int inner2b;
}
}

A simple-minded compiler might allocate space for all 4 objects on
entry to the function.

Or the compiler might recognize that the two inner scopes cannot be
active at the same time, and overlap their allocations, but still
allocate everything on function entry (requiring space for 3 ints).

Or the compiler might allocate space for each block's local objects
only on entry to the block, and deallocate it on leaving the block.
In this case, the compiler (actually the generated code) would
allocate space for one int on entry to foo().

A compiler following the first or second strategy can freely rearrange
the allocated objects to minimize gaps, even across block boundaries.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 14 '06 #15
Mikhail Teterin <us****@aldan.a lgebra.comwrite s:
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...
Even if all your assumptions are valid, strcmp() doesn't do the same
thing as your int32_t comparison. Comparing two int32_t values
compares all the bits; strcmp() only compares up to the '\0' that
terminates each string.

For example, you could have c1->acCUR equal to
{ 'X', 'Y', '\0', 'A' }
and c2->acCUR equal to
{ 'X', 'Y', '\0', 'B' }
Not all the corresponding array elements are equal, but strcmp() will
ignore the 'A' and 'B' characters.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 14 '06 #16
Eric Sosman wrote:
Yeah, well. šOne might equally opine that only the spoiled rotten
button-clicking GUI-besotted drooling whining spaced-out brainless --
oh, sorry, didn't mean to offend -- um, only "a few extraordinary
people" consider such trickery "really cool."
Give me an example, of trickery, that YOU consider really cool, that is also
portable...

-mi
Aug 15 '06 #17
Keith Thompson wrote:
For example, you could have c1->acCUR equal to
{ 'X', 'Y', '\0', 'A' }
and c2->acCUR equal to
{ 'X', 'Y', '\0', 'B' }
Not all the corresponding array elements are equal, but strcmp() will
ignore the 'A' and 'B' characters.
I know. Currencies, however, are all 3-character strings (plus the
terminating '\0'). Thus they are perfectly suited to be treated as int32_t,
when convenient.

That it is not 100% portable is already rammed into me by the friendly folks
on this board. I'd like to know an example of the actual hardware/compiler
combo, where it would not work, though...

Thanks,

-mi
Aug 15 '06 #18
Mikhail Teterin wrote:
Eric Sosman wrote:

>>Yeah, well. One might equally opine that only the spoiled rotten
button-clicking GUI-besotted drooling whining spaced-out brainless --
oh, sorry, didn't mean to offend -- um, only "a few extraordinary
people" consider such trickery "really cool."


Give me an example, of trickery, that YOU consider really cool, that is also
portable...
Straying from topicality, hence the change in Subject ...

Once upon a time, in my rambunctious youth, I would have had
no trouble answering your question. It was cool to use `x&(x-1)'
to zero the lowest-order one-bit, it was cool to use "horizontal
addition" to count the one-bits or compute parity, it was cool to
use `POP PC' instead of `RETURN' (yes! it was faster!), it was
cool to use a computed GOTO instead of an IF (as you may deduce,
my R.Y. was some time ago ...)

But a funny thing happened: Progress.

The first computer I used had forty thousand decimal digits
of memory, of which one hundred locations were reserved for the
table that allowed it to add and subtract integers. All other
arithmetic -- integer multiplication and division, all kinds of
floating-point -- were done via subroutines. I no longer recall
the instruction timings for that machine, but I do remember that
when it was replaced by a newer system that could execute some
kinds of instructions in LESS THAN TWO MICROSECONDS it seemed
like an onrush of unthinkable speed.

On that machine, in my R.Y., "cool tricks" were a necessity,
a staple of daily existence. They were the difference between
a program that ran and a program that failed (sorry: your code
is twenty digits bigger than memory). Moving a few instructions
out of an inner loop was a big deal -- and the compilers of the
day were not very good at such things. After all, they had only
the resources of the same slow small machine at their disposal,
and were already stressed simply to get the translations done.
(It was not unusual for a compiler to be a program of five or
so sequential "passes" communicating intermediate results via
temporary files, sometimes on reels of magnetic tape or even on
decks of punched cards.) In my R.Y. the compilers needed all the
help we could give them.

Ah, but there's been Progress.

Not quite twenty years ago I bought my first x386 machine,
and it wasn't long before I had a sort of magical realization:
My very modest 640x480 display was being driven by a bare-bones
low-end video card with SIXTEEN TIMES THE MEMORY CAPACITY OF MY
ENTIRE COLLEGE CAMPUS! In a PERIPHERAL, for Crissakes!

It sort of brought home to me the degree to which progress
had changed my world -- and the sobering fact that the cool tricks
I once employed to squeeze three more instructions into the size
of one disk sector were simply no longer relevant. The economic
facts that once ruled my profession no longer held; a different
dynamic was loose in the world.

The world's finest maker of buggy whips goes to the poorhouse
when the automobile comes along. The careful choice of leathers
for different parts of the whip, the judicious use of oak for
stiffness and ash for springiness that gives the handle its
inimitable feel, the consummate craftsmanship in the double-looped
stitching to prevent water and snow from penetrating and rotting
the interior ... Irrelevant. Useless. Outmoded. Inane.

As one can admire the skills of the buggy whip makers and the
inordinate amount of labor they would expend on a single whip, so
one can admire the "cool tricks" of the programmer-craftsmen and
their perseverance at desk-checking to avoid messing up even one
of their couple of compiles per day. (I remember once poring over
a core dump trying to see whether a failed program had at least
computed the right intermediate results before dying, doing pencil-
and-paper long division IN HEXADECIMAL to check the results.) As
I say, one can admire the ingenuity, cleverness, sneakiness, and
sweat -- but the proper venue for such admiration is the same as
for admiring buggy whips: In a museum of antiquities, not in code
written for the present day.

Can you make a fire by rubbing two sticks together? It's
doable -- I've seen it done -- but do you feel any less "able"
if you cannot do it yourself?

Do you know how to flake flint to make a knife edge? If not,
do you find your daily life impeded by the loss of that cool trick?

What kind of sapling will make a good bow? What animal's
entrails will you wind to make a good bowstring, and how do you
get them out of the carcase intact, and how do you prevent them
from rotting -- how, for that matter, do you catch and kill the
animal?

All these once-essential skills are now become irrelevant.
Interesting to antiquarians, perhaps, and in that limited sense
still cool -- but of no serious consequence any more. You could
be the world's greatest fire-maker, the best flint-flaker ever
to "Ouch!" his thumb, and the best bowyer who ever bent birch,
and you would be ... what? Not chatting on Usenet, I imagine.

I have (figuratively) lived in caves and eaten tasty nematodes
scrabbled from the dirt (there's a trick to finding the good ones,
I'll show ya how it's done). Nowadays I live in a house and get my
food from a supermarket -- and y'know? I really don't miss the
nematodes all that much.

And *that's* cool.

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 15 '06 #19
On Mon, 14 Aug 2006 22:41:38 -0400, Mikhail Teterin
<us****@aldan.a lgebra.comwrote in comp.lang.c:
Keith Thompson wrote:
For example, you could have c1->acCUR equal to
{ 'X', 'Y', '\0', 'A' }
and c2->acCUR equal to
{ 'X', 'Y', '\0', 'B' }
Not all the corresponding array elements are equal, but strcmp() will
ignore the 'A' and 'B' characters.

I know. Currencies, however, are all 3-character strings (plus the
terminating '\0'). Thus they are perfectly suited to be treated as int32_t,
when convenient.

That it is not 100% portable is already rammed into me by the friendly folks
on this board. I'd like to know an example of the actual hardware/compiler
combo, where it would not work, though...

Thanks,

-mi
Almost every single DSP in existence, for starters.

I routinely work on a TI DSP these days where CHAR_BIT is 16 and
sizeof(int) is 1.

So your union of one int (and unsigned int would be better for type
punning) and an array of four chars would be four bytes long, and
comparing the int member of two unions would only compare the first
character of the array in each.

Even if you have a <stdint.h>, which TI's compiler did not provide but
I did (for the types actually supported), and have int32_t typedef'ed
to signed long (and unit32_t typedef'ed to unsigned long), the size of
the (un)signed long at the beginning of union is only two bytes.

There are some 32 bit DSPs where all of char, short, int, and long
have 32 bits.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Aug 15 '06 #20

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

Similar topics

4
2559
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
3861
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
6145
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
1659
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
2727
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
2438
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
1880
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
2282
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
10034
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
9872
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
9713
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...
1
7248
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
6534
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
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
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
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.