Click here to Skip to main content
15,902,492 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Thats why i hate c++ Pin
Vasily Tserekh27-Apr-12 5:14
Vasily Tserekh27-Apr-12 5:14 
GeneralRe: Thats why i hate c++ Pin
krsmichael27-Apr-12 3:53
krsmichael27-Apr-12 3:53 
GeneralRe: Thats why i hate c++ Pin
Vasily Tserekh27-Apr-12 5:30
Vasily Tserekh27-Apr-12 5:30 
GeneralRe: Thats why i hate c++ Pin
krsmichael27-Apr-12 6:46
krsmichael27-Apr-12 6:46 
GeneralRe: Thats why i hate c++ Pin
krsmichael27-Apr-12 6:50
krsmichael27-Apr-12 6:50 
GeneralRe: Thats why i hate c++ Pin
Vasily Tserekh27-Apr-12 7:33
Vasily Tserekh27-Apr-12 7:33 
GeneralRe: Thats why i hate c++ Pin
CDP180227-Apr-12 8:19
CDP180227-Apr-12 8:19 
GeneralRe: Thats why i hate c++ Pin
krsmichael27-Apr-12 10:07
krsmichael27-Apr-12 10:07 
Inline asm allows a developer to access hardware that the c/c++ could not access. An example of this is the CPUID machine instructions. Before C#, if you wanted information about your hardware, you would write something like this.

C++
unsigned long value;
_highestFunction = 0;

union VendorUnion
{
    unsigned long vendorLong;
    unsigned char vendorArray[4];
} vendorTail;

unsigned char temp;

__asm
{
    mov eax, 0x00
    cpuid

    mov value, eax
    mov vendorTail.vendorLong, ecx
}

_highestFunction = value;

// swap some values so that the switch statement will be more readable...
temp = vendorTail.vendorArray[0];
vendorTail.vendorArray[0] = vendorTail.vendorArray[3];
vendorTail.vendorArray[3] = temp;

temp = vendorTail.vendorArray[2];
vendorTail.vendorArray[2] = vendorTail.vendorArray[1];
vendorTail.vendorArray[1] = temp;

switch (vendorTail.vendorLong)
{
    case 'ter!': _vendorID = eAMDK5; break;
    case 'cAMD': _vendorID = dAMD; break;
    case 'auls': _vendorID = eCentaur; break;
    case 'tead': _vendorID = eCyrix; break;
    case 'ntel': _vendorID = eIntel; break;
    case 'Mx86': _vendorID = eTransmeta; break;
    case 'aCPU': _vendorID = eTransmeta; break;
    case ' NSC': _vendorID = eNationalSemiconductor;    break;
    case 'iven': _vendorID = eNexGen; break;
    case 'Rise': _vendorID = eRise; break;
    case ' SIS': _vendorID = eSiS; break;
    case ' UMC': _vendorID = eUMC; break;
    case ' VIA': _vendorID = eVIA; break;
}

unsigned long _eax, _ebx, _ecx, _edx;

__asm
{
    mov eax, 0x01
    cpuid

    mov _eax, eax;
    mov _ebx, ebx
    mov _ecx, ecx
    mov _edx, edx
}


// setting eax to 3 will get the cpu id
if (_highestFunction >= 3)
{
    __asm
    {
        mov eax, 0x03
        cpuid

    }
}


Writing that same code in pure ASM would be a pain but in this case, you can use the higher level language when appropriate but use the assembly to access the hardware. The ASM being platform dependent is irrelevant at this point because that is the point. The only time that C# or Java can outperform C++ is when a loop is being performed and the VM can rearrange the byte codes to predict execution. C++ is statically built so the speed is constant. Other than that, I am aware of no instances of C# or Java being faster than well written C++.
GeneralRe: Thats why i hate c++ Pin
Gerry Schmitz27-Apr-12 12:02
mveGerry Schmitz27-Apr-12 12:02 
GeneralRe: Thats why i hate c++ Pin
krsmichael27-Apr-12 14:34
krsmichael27-Apr-12 14:34 
GeneralRe: Thats why i hate c++ Pin
CDP180227-Apr-12 7:32
CDP180227-Apr-12 7:32 
GeneralRe: Thats why i hate c++ Pin
Vasily Tserekh27-Apr-12 7:51
Vasily Tserekh27-Apr-12 7:51 
GeneralRe: Thats why i hate c++ Pin
CDP180227-Apr-12 8:36
CDP180227-Apr-12 8:36 
GeneralRe: Thats why i hate c++ Pin
Al_Brown29-Apr-12 20:59
Al_Brown29-Apr-12 20:59 
GeneralRe: Thats why i hate c++ Pin
patbob27-Apr-12 12:28
patbob27-Apr-12 12:28 
GeneralRe: Thats why i hate c++ Pin
KP Lee27-Apr-12 13:26
KP Lee27-Apr-12 13:26 
GeneralRe: Thats why i hate c++ Pin
Member 869706828-Apr-12 9:01
professionalMember 869706828-Apr-12 9:01 
GeneralRe: Thats why i hate c++ Pin
mandyedi28-Apr-12 9:03
mandyedi28-Apr-12 9:03 
GeneralRe: Thats why i hate c++ Pin
Charles Oppermann27-Apr-12 2:34
Charles Oppermann27-Apr-12 2:34 
GeneralRe: Thats why i hate c++ Pin
KP Lee27-Apr-12 13:46
KP Lee27-Apr-12 13:46 
GeneralRe: Thats why i hate c++ PinPopular
OriginalGriff24-Apr-12 9:20
mveOriginalGriff24-Apr-12 9:20 
GeneralRe: Thats why i hate c++ Pin
jeron124-Apr-12 10:15
jeron124-Apr-12 10:15 
GeneralRe: Thats why i hate c++ Pin
CDP180224-Apr-12 10:51
CDP180224-Apr-12 10:51 
GeneralRe: Thats why i hate c++ Pin
KP Lee27-Apr-12 14:24
KP Lee27-Apr-12 14:24 
GeneralRe: Thats why i hate c++ Pin
Jörgen Andersson24-Apr-12 11:42
professionalJörgen Andersson24-Apr-12 11:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.