I have found this forum to be very helpful in my (young) quest for learning C++. I have the following functions that I dont quite understand, what I do understand, I have commented, the rest I am lost on:
unsigned int get_this(unsigned int a,unsigned int b, unsigned char c)
{
unsigned char* ptr = (unsigned char*) &a; //storing a location in ptr
unsigned int ret_val;
//what is the significance of this? if the int is not equal to 4 bytes set it to 0? why? I thought sizeof allocates enough space so that the object may fit.
if (b >= sizeof(unsigned int)) b = 0;
*(ptr + b) = c; //dereferencing pointer to go to the contents of (ptr + b), and store whats in c in that location
ret_val = *((unsigned int*) ptr); //this one lost me.....
return ret_val;
}
First, Please use the code tags when posting code.
Second, why are you working/looking at these functions?
As something learning C++ this isn't typically the sort of code you would be coming across. It's not especially common to be learning byte/bit manipulation as a newbie to C++.
unsignedint get_this(unsignedint a,unsignedint b, unsignedchar c)
{
unsignedchar* ptr = (unsignedchar*) &a; // By using a char pointer instead
// of a int pointer, you limit what
// you have access to, to one byte
unsignedint ret_val; // We're going to return an int, not
// a char
// b is the offset into the 4 bytes of the unsigned int so if its more than 4 just
// set it to the first byte
if (b >= sizeof(unsignedint)) b = 0;
// as you say
*(ptr + b) = c; //dereferencing pointer to go to the contents of (ptr + b),
// and store whats in c in that location
// cast the one byte pointed to by pointer back to an int pointer.
// Then set the full 4 bytes of ret_val to the value stored in the single byte
// pointed to by ptr
ret_val = *((unsignedint*) ptr);
return ret_val;
}
I hope this explains it. If not ask the questions here.
This is extracting the value of one of the four bytes of the unsignedint
Close :)
This code is modifying one of the bytes in A, at offset B to become value C. And returning the value. So not modifying the original A (as it was never passed in by reference)
Sorry for not posting in code tags, I will take care of it from now on. As for byte and bit manipulation, I would like to get a hang of it by looking at functions that are of bit and byte manipulation nature, it may seem odd, but I am perhaps just a wee bit curious?
I would really appreciate if some insight would be given on this function.
When we think of an unsigned int, we think of a 4 byte value that looks like 0x44332211. 11 being lower in value than 44. When it's stored in memory it looks like 0x11223344. Backwards to how we'd think about it.
What this code does is take a value in c, and place if into the byte (1-4) given by the offset+1.
If we did get_this(1, 0, 0x10). The return value would be 0x01000000. This to us is 0x10 (16). The initial 1 we put in as a is over-written by 0x10.
An offset can be regarded as an adjustment value.
So in bnbertha's example, *(ptr + b) = c; b is the offset because it is used to adjust the value of ptr that is dereferenced.
So if b=0 you access *ptr, b=1 you access *(ptr+1), etc.
It is a term often used with pointers and arrays, as you often take a base pointer value (or pointer to an array) and an offset to access the elements of the array.
Edit: Clicked submit to see 2 other answers appear - all slightly different, hope one of them explanis it for you:-)
To your post as to what insight I am after, yes, you are right, I want to know what it is doing. I could put this program into a C++ debugger and see whats happening, but that would not make me understand anything, I need to understand conceptually.
The answers that I have come across on this forum have been helpful in making me see code such as this in a different way, it is perplexing to me and I feel that if I get to know more of whats happening under the hood, I could be a good C/C++ programmer someday.
@guestgulkan
No it isn't, I got it backwards. It's as Zaita said, changing one of the bytes to c.
ptr is added to the offset and the byte is modified in one go (line 13 on my post).
Your just lucky I have spent the past few weeks doing byte/bit arithmetic and manipulation. I am writing a protocol handler for an embedded devices own protocol. Everything is in bytes, with strange data types I need to manipulate as bits.
@Zaita
Have you ever worked with the military datalink called Link 16 JTIDS? All the messages it exchanges are 9 bytes long and full of bitfields. One of the worst combinations imaginable. At times I could have happily shot the guy who designed it.
EDIT: My advice, if anyone offers you work on this (and just about every US ally uses it), run :)
@bnbertha. God no lol, I try to avoid as much Govt stuff as possible. I was asked to write a new OS for a handheld laser GPS device that was being sold to USA Army. But they wanted to bring it into C#.NET and I didn't really want that to be my primary development language.
atm, I work for a science/research company. The work I am doing is a new interface into a logging device (climate logging, river water levels etc). Their protocol is pretty well defined, but they leave little bits out. This is in C#, but at byte/bit manipulation level i ts 90% the same as C++.
I was the same, I am not really fond of Microsoft and their business practices (or quality of products *cough*360 broke 2x now*cough*).
But, I don't mind C#. .NET is a bit of a PITA but C# is actually a fairly good language. They have a few strange things I miss but it's exceptional for creating web front-end applications. Especially with Web-Services.
J2EE offers the same functionality, but in a much larger and more complicated architecture. So .NET/C# is allowing grad/newbie programmers to create Web Front-end applications easily. Not to mention, schools/unis get Visual Studio pretty much free.
C++ needs a standard GUI Toolkit. One thats multi-platform. Personally, I use WxWidgets on any platform I am developing on, but C++ lacks this standard one that .NET/Java/Delphi offer. Thats a big feature of .NET/C#. Instant GUI
Edit: It also has garbage collection, so every edit can use 'new' without having to worry about 'delete'. Yet memory leaks still exist.. go figure :P
You have to hand it to Microsoft, give Visual Studio, Office etc to the schools and Uni's and get all the students and grads used to using them. Then commercial sector goes with them cos there is a ready supply of users and developers.
Yup. It's population brainwashing straight up. But thats how MS became who they are today. Taking this is thread a bit OT lol.
To sum C# up in 1 sentence. "it's like an easy version of C++, complete rip of Java made a bit simpler that most idiots (or monkeys) can code in". Nothing magic about it.