CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Threaded View

  1. #10
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Switch case or something better!

    Quote Originally Posted by Rigsby View Post
    [...] the case system was really easy to build in Excel and then copy and paste LOL.
    Using VBA you may even have Excel write C++ code for you... (Of course that would only really pay if you need that multiple times.)

    Also, remember that an array would then have to be of type column* so would hold pointers to column objects! So I would physically be creating almost 300 pointers, whereby in the switch case a single pointer to an object is returned.
    And what's so scary about something like:

    Code:
    column c_01, c_02, c_19;
    column *pointer_array[] = { &c_01, &c_02, &c_19, &c_01, &c_02, &c_19 };
    ? As you already said yourself: You'd be instantiating about 300 cheap pointers, not 300 column objects of whatever complexity.

    [...] Yes, I know I could switch on char, and I might be wrong but I believe there is some additional overhead in doing this; I seem to remember reading about some kind of conversion that takes place specifically on switch case statements [...].
    Yes, there would be a conversion overhead involved, but probably that would merely be that of a MOVZX (or MOVSX, see below) instruction versus an ordinary MOV, or three clock cycles versus one. (The figures are taken from a 17 years old Pentium 4 manual that is the only reference about that I have at hand here right now, but that probably hasn't changed much since then, and at least the principle will hold true.) This difference actually is quite small and it may even get hidden entirely by the CPU-internal runtime optimization mechanisms. The numerical difference in clock ticks is three-fold which looks much, but probably is rather irrelevant comapred to the clock ticks consumed by the rest of your code.

    Also, that conversion is by no means specific to switch statements in any way. It's an ordinary type conversion between types of different bitness.

    [...] And in fact I am entering a char directly into the ptr2col(unsignedint intin)[/FONT][FONT=Calibri]function. So my call is similar to:

    Code:
     ptr2col(mystring[0])->addnode(n);
    
    An int takes a char without any problem and is an excellent way to access char variables by their ASCII value.
    In that case you need to be aware that the standard C++ char type is signed, which, in such a scenario, may get you into trouble with characters above the standard ASCII range from 0x00 to 0x7F and code like what you've posted. (At least for VC++ this can be changed using the command line option /J, but of course that way your code would not be standard-conformant, i.e. not portable.)
    Last edited by Eri523; March 26th, 2012 at 03:09 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured