Welcome, Guest. Please login or register.

Author Topic: multi-character character constants in C++  (Read 5180 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: multi-character character constants in C++
« on: April 06, 2003, 12:02:52 AM »
Hi dude,

This code isn't portable and makes some assumptions which are reasons for your compiler to complain

1) ints aren't always 32 bits (not such a big deal for most platforms)

2) Under C++, enumerations don't have to be ints - the implementation is free to decide the best size for a given enumeration - not really a problem in your code since the logical size would need to be 4-bytes in this case

3) Multicharacter constants are not uniformly supported. How they map to integers may additionally depend on endian issues that are obviously hardware dependent.

If the actual value of your constants isn't imporant to you (as long as theyre unique), just use a normal enumeration which ensures each constant has a unique value.
If you want to enforce an enumeration size to make sure it's 32-bits, you could use something like

enum {
   JOHN_NAME,
   FRED_NAME,
   MIKE_NAME,
   ANNE_NAME,
   DUMMY_NAME = 0xFFFFFFFF
};


Another option to generate a unique number from a string name is to use a hash function.

Here's one I made earlier :-)


unsigned long GetHashValue(const char* text)
{
   unsigned long v = 0;
   unsigned char* p = (unsigned char*)text;
   while(*p) {
      /* bitwise rotate and xor with byte */
      v = (v<<1) | ((v&0x80000000UL) ? 1UL : 0);
      v ^= (unsigned long) (*p++);
   }
   return v;
}


This generates different values for different strings and has lots of uses.

For a slightly off topic example, I use the above (in a class form)  to generate integers from filenames as part of a cacheing system - rather than looking through a list of names (using strcmp()), to see if something is loaded, I generate the hash for each loaded object and search by hash value. Much faster :-)
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: multi-character character constants in C++
« Reply #1 on: April 06, 2003, 12:48:30 AM »
Ah, I see your dilemma.

Maybe you can disable unsafe conversion warnings in the compiler options?
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: multi-character character constants in C++
« Reply #2 on: April 06, 2003, 02:38:59 AM »
Quote

elendil wrote:

What does " " do in your code? I have never seen any c-code with that. I always thought it was some htlm-thingie (that I dont know about either).



Pardon? I don't recall that anywhere. What browser are you using?

-edit-

Do you mean single quote as in 'c'? That's a character constant...
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: multi-character character constants in C++
« Reply #3 on: April 06, 2003, 02:52:50 AM »
Hi all,

Is anybody else seeing suspicious "" in my code? I'm not sure what elendil means...

Could it be browser nonsense?

I'd hate to mislead people with tons of nonsense syntax..
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: multi-character character constants in C++
« Reply #4 on: April 06, 2003, 03:01:26 AM »
Ah, that could be it, I suppose. The thing is that code without indentation is a pain in the ass to look at.

Ideas?
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: multi-character character constants in C++
« Reply #5 on: April 06, 2003, 03:08:59 AM »
Hmm....



void test()
{
   printf("How's it look now?\n");
}



Better? Or no change?
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: multi-character character constants in C++
« Reply #6 on: April 06, 2003, 11:06:19 AM »
Im using non breaking spaces to indent the code (reccomended by Tickly) - my guess is that it got screwed up somehow...

In any event, it all looks fine in IE5 (shameful admission of using that browser)...
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: multi-character character constants in C++
« Reply #7 on: April 06, 2003, 11:23:01 AM »
he he,

Maybe I'll waste some time today and go backwards through my posts to obliterate the broken spaces :-D

-edit-

One down...X to go :-D
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: multi-character character constants in C++
« Reply #8 on: April 06, 2003, 11:43:06 AM »
Quote

Sidewinder wrote:
You see, the problem is that it's not my code.  It's part of the POVRay ray tracing package which is supposed to be portable code.  There are litterally thousands of occurences of this error.  I guess I'll just ignore it and see what happens.


If you can be bothered, you could try an explicit cast to long - eg

JOHN_NAME = (long)'john'

That's still not particularly portable but it might just shut the compiler up...

-edit-

Just noticed: long john :lol:
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: multi-character character constants in C++
« Reply #9 on: April 06, 2003, 03:36:30 PM »
Quote

Vincent wrote:
I was reading this post yesterday, before the editing, and everything looked ok in Opera 7.01 then.  I was actually speaking to Karlos on ICQ when elendil posted asking about the " "s.  


:lol: Was he ever! What time did you hit the sack, mate?
int p; // A