hi guys , i got some problem with understanding 2 functions in C/C++ source.
i'm php programmer and i'm not so good with C/C++ codes . i just know some basic of it .
here are 2 functions i try to figured it out .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
staticinlineunsignedchar decode(char** p, unsignedint* l) {
unsignedchar c;
if (*l == 0) {
zend_bailout();
}
c = **p;
(*p)++;
(*l)--;
return c;
}
staticinlineunsignedint decode32(char** p, unsignedint* l) {
unsignedint i = decode(p, l);
i += ((unsignedint)decode(p, l)) << 8;
i += ((unsignedint)decode(p, l)) << 16;
i += ((unsignedint)decode(p, l)) << 24;
return i;
}
double ** are bad practice mostly when not applied to the right projects you have to know what your doing .as for beginners its best not practice double pointers could cause era it is best to use /* *\ for comments.
What? Why are pointers-to-pointers "bad?" (I'm assuming that's what you mean, because he doesn't have any pointers to doubles).
If you've got nothing to say except "double* are bad" (which isn't necessarily try), don't say anything at all.
On topic:
========
Without know what "zend_bailout()" does, I can't really tell. Didn't the original author of that code write a comment at the top, explaining what the function does? I hate lazy people... Personally, whever I write a function, I write a comment before it. Example:
1 2 3 4 5 6 7 8 9 10 11 12
/**
* die: print a given message to the screen, and then call exit with status @status
* @message: the message to be printed
* @status: the status to exit with.
*
* die does not return.
*/
void die(constchar* message, int status) {
printf("%s\n", message);
exit(status);
}
1) a stream of bytes
2) a size or 'count' of how many bytes are remaining in that stream
decode does this:
- contrary to what its name implies, it doesn't actually decode anything
- if there are no more bytes remaining in the stream, it calls zend_bailout (whatever that is)
- it returns the next byte in the stream
- it then increments the stream pointer to point to the next byte, and decrements the remaining count so the next call will return the next byte in the stream.
Example:
1 2 3 4 5 6 7 8 9 10 11 12
char buffer[] = {100,50,25};
char* myptr = buffer; // pointer to the buffer
int size = sizeof(buffer); // number of bytes in the buffer
cout << decode( &myptr, &size ) << "\n" // outputs '100'
// myptr is now += 1
// size is now -= 1
cout << decode( &myptr, &size ) << "\n" // outputs '50'
cout << decode( &myptr, &size ) << "\n" // outputs '25'
cout << decode( &myptr, &size ) << "\n" // calls zend_bailout() (and returns garbage?)
decode32 simply calls decode 4 times and combines all 4 returned values into one 32-bit number (in little endian form)