portable ascii-hex conversion

A

Andrew Poelstra

Andrew said:
Here's a trick from /C Unleashed/, in a chapter (I believe) Richard
Heathfield wrote:

char *hex = "0123456789ABCDEF";

Actually, what you wrote was:

char Hex[] = "0123456789ABCDEF";

Sorry for misquoting you; I had written that, and then thought,
'Why would I want to modify that?', converted it from an array
to a pointer, and forgot to qualify it with const.

Can you explain how you got from "Why would I want to
modify that?", to converting it to a pointer ?

Instead of creating an array of chars, I'll just point to a
string literal, which I may not modify.
My preference would be:
const char Hex[] = "0123456789ABCDEF";

Indeed. Perhaps I should have found the quote in the book before
poorly paraphrasing it. :)
 
O

Old Wolf

Ben said:
Old Wolf said:
My preference would be:
const char Hex[] = "0123456789ABCDEF";

I'd add "static".

If it's outside of a function then I would agree, to prevent the
compiler making it an external symbol. If it is within the
function then the "static" keyword would seem to not change
the observable behaviour, so I guess you would only add it
if you didn't trust your compiler!
 
B

Ben Pfaff

Old Wolf said:
Ben said:
Old Wolf said:
My preference would be:
const char Hex[] = "0123456789ABCDEF";

I'd add "static".

If it's outside of a function then I would agree, to prevent the
compiler making it an external symbol. If it is within the
function then the "static" keyword would seem to not change
the observable behaviour, so I guess you would only add it
if you didn't trust your compiler!

In a function, there is an observable difference in that the
version without `const' has lifetime only during an execution of
the function. It's probably not important in this case because
it's unlikely that this string would be returned to a caller.
 
C

CBFalconer

Old said:
Ben said:
Old Wolf said:
My preference would be:
const char Hex[] = "0123456789ABCDEF";

I'd add "static".

If it's outside of a function then I would agree, to prevent the
compiler making it an external symbol. If it is within the
function then the "static" keyword would seem to not change
the observable behaviour, so I guess you would only add it
if you didn't trust your compiler!

The 'static' gets it initialized once and for all at compile time,
rather than by executing local code on each entry into the
routine. This reduces run time.
 
O

Old Wolf

CBFalconer said:
Old said:
Ben said:
My preference would be:
const char Hex[] = "0123456789ABCDEF";

I'd add "static".

If it's outside of a function then I would agree, to prevent the
compiler making it an external symbol. If it is within the
function then the "static" keyword would seem to not change
the observable behaviour, so I guess you would only add it
if you didn't trust your compiler!

The 'static' gets it initialized once and for all at compile time,
rather than by executing local code on each entry into the
routine. This reduces run time.

In theory, possibly. Since there's no difference in observable
behaviour, the compiler is quite entitled to do the same thing
in practice , regardless of whether 'static' is present or not.

In fact I'd wager that most compilers would not reinitialize
the non-static array every time the function is entered.
 
C

CBFalconer

Old said:
CBFalconer said:
Old said:
Ben Pfaff wrote:

My preference would be:
const char Hex[] = "0123456789ABCDEF";

I'd add "static".

If it's outside of a function then I would agree, to prevent the
compiler making it an external symbol. If it is within the
function then the "static" keyword would seem to not change
the observable behaviour, so I guess you would only add it
if you didn't trust your compiler!

The 'static' gets it initialized once and for all at compile time,
rather than by executing local code on each entry into the
routine. This reduces run time.

In theory, possibly. Since there's no difference in observable
behaviour, the compiler is quite entitled to do the same thing
in practice , regardless of whether 'static' is present or not.

In fact I'd wager that most compilers would not reinitialize
the non-static array every time the function is entered.

You'ld be wrong. Most compilers implement automatic storage on a
stack, and that content is unknown on function entry. Without
initialization code the operation would be wrong.
 
R

Random832

2006-11-13 said:
Old said:
CBFalconer said:
Old Wolf wrote:
Ben Pfaff wrote:

My preference would be:
const char Hex[] = "0123456789ABCDEF";

I'd add "static".

If it's outside of a function then I would agree, to prevent the
compiler making it an external symbol. If it is within the
function then the "static" keyword would seem to not change
the observable behaviour, so I guess you would only add it
if you didn't trust your compiler!

The 'static' gets it initialized once and for all at compile time,
rather than by executing local code on each entry into the
routine. This reduces run time.

In theory, possibly. Since there's no difference in observable
behaviour, the compiler is quite entitled to do the same thing
in practice , regardless of whether 'static' is present or not.

In fact I'd wager that most compilers would not reinitialize
the non-static array every time the function is entered.

You'ld be wrong. Most compilers implement automatic storage on a
stack, and that content is unknown on function entry. Without
initialization code the operation would be wrong.

It doesn't need "initialization code" if you can't tell whether it's
there - all it has to do is put it somewhere other than on the stack,
which it is perfectly entitled to do.
 
O

Old Wolf

CBFalconer said:
Old said:
CBFalconer said:
Old Wolf wrote:
Ben Pfaff wrote:

My preference would be:
const char Hex[] = "0123456789ABCDEF";

I'd add "static".

If it's outside of a function then I would agree, to prevent the
compiler making it an external symbol. If it is within the
function then the "static" keyword would seem to not change
the observable behaviour, so I guess you would only add it
if you didn't trust your compiler!

The 'static' gets it initialized once and for all at compile time,
rather than by executing local code on each entry into the
routine. This reduces run time.

In theory, possibly. Since there's no difference in observable
behaviour, the compiler is quite entitled to do the same thing
in practice , regardless of whether 'static' is present or not.

In fact I'd wager that most compilers would not reinitialize
the non-static array every time the function is entered.

You'ld be wrong. Most compilers implement automatic storage on a
stack, and that content is unknown on function entry. Without
initialization code the operation would be wrong.

There's no requirement for storage to be on a stack,
and it would seem sensible for an array that never
changes and only needs to be initialized at most once,
to not be placed on a stack.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top