Wednesday, April 6, 2011

Getting first byte in a char* buffer

I have a char* buffer and I am interested in looking at the first byte in the char* buffer, what is the most optimal way to go about this.

EDIT: Based on the negative votes I might want to explain why this question, I am aware of methods but in the code base that I have been looking for getting first byte people do all kinds of crazy things like do a copy of the buffer , copy it to a stream and then do a get.

From stackoverflow
  • Just use

    char firstByte = buffer[0];
    
    Crashworks : But is it optimum? That costs a whole cycle! ;)
    Eclipse : It could be more if buffer has been paged out - that could be hundreds of cycles. Just to be sure, you should probably lock that memory into RAM so no paging is going to happen.
    Albert : It's possible to ensure some memory doesn't get paged out?
    Johannes Weiß : at least not portable
    Eclipse : In Windows, you can look at VirtualLock (http://msdn.microsoft.com/en-us/library/aa366895.aspx) - but my comment about paging was sarcastic and I don't recommend this as an actual performance enhancing technique.
  • char* c_ptr;
    char first_char;
    
    first_char = c_ptr[0];
    
  • Or this:

    char firstByte = *buffer;
    

    For clarification, there's no difference between *buffer and buffer[0], since the latter is really just shorthand for *(buffer + 0*sizeof(char)), and any compiler is going to be smart enough to replace that with *(buffer+0) and then *buffer. So the choice is really whichever is clearest in the context you are using it, not how efficient each one is.

  • char first = someCharPtr[0];
    

    or

    char first = *someCharPtr;
    
  • char *buffer = {'h','e','l','l','o','\0'};
    

    or:

    char *buffer = "hello";
    

    or:

    char buffer[6] = {'h','e','l','l','o','\0'};
    

    and to get the first byte:

    char firstChar = buffer[0];
    

    or:

    char firstChar = *buffer; // since the buffer pointer points to the first element in the array
    
    Chuck : {'h','e','l','l','o','\0'} has six elements.
    Luca Matteis : Eheh, yep, idiot typo.
    Eclipse : There is quite the diffence between char *buffer = "hello"; and char buffer[] = "hello";
    Luca Matteis : Right, I fixed "is equal" to "or".
  • If you're determined to micro-optimize, you should know that every compiler made in this millennium should produce exactly the same machine code for "c = *buffer" and "c = buffer[0]".

    Eclipse : Even my toy class-project compiler can do this optimization ;)
    David Thornley : Since buffer[0] is defined as *(buffer + 0 * sizeof(char)), it isn't a difficult optimization. It's a straight transform, followed by a little precalculation. I'd be shocked at a publicly available compiler that missed this one.
  • Good for x86 platforms...

    char firstByte;
    
    __asm {
       mov al, [buffer]
       mov [firstByte], al
    }
    
  • Just as a clarification of what several people have mentioned--that:

    buffer[0]
    

    is equivalent to

    *(buffer + 0*sizeof(char))
    

    That's not technically true if you assume that's literal C code (i.e. not pseudo code), although that's what the compiler is doing for you.

    Because of pointer arithmetic, when you add an integer to a pointer, it is automatically multiplied by sizeof(*pointer), so it should really be:

    *(buffer + 0)
    

    Although, since sizeof(char) is defined to be 1, it is actually equivalent in this case.

0 comments:

Post a Comment