tags:

views:

468

answers:

8

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.

+17  A: 

Just use

char firstByte = buffer[0];
Johannes Weiß
But is it optimum? That costs a whole cycle! ;)
Crashworks
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.
Eclipse
It's possible to ensure some memory doesn't get paged out?
Albert
at least not portable
Johannes Weiß
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.
Eclipse
A: 
char* c_ptr;
char first_char;

first_char = c_ptr[0];
schnaader
+12  A: 

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.

Eclipse
+1  A: 
char first = someCharPtr[0];

or

char first = *someCharPtr;
Matt Davison
+5  A: 
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
Luca Matteis
{'h','e','l','l','o','\0'} has six elements.
Chuck
Eheh, yep, idiot typo.
Luca Matteis
There is quite the diffence between char *buffer = "hello"; and char buffer[] = "hello";
Eclipse
Right, I fixed "is equal" to "or".
Luca Matteis
+2  A: 

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]".

Paul Tomblin
Even my toy class-project compiler can do this optimization ;)
Eclipse
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.
David Thornley
A: 

Good for x86 platforms...

char firstByte;

__asm {
   mov al, [buffer]
   mov [firstByte], al
}
spoulson
+1  A: 

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.

Matthew Crumley