Essentially, the increase function takes a void* (I read this as "pointer to something"), converts the pointer to the appropriate type based on the parameter psize, and increments whatever the pointer is pointing to.
The important thing to note is with a void* you will need some knowledge of what that pointer points to in order to work with it. In this "tutorial", they chose to pass the size of the data type so that increase can make sense of the void*.
Here is an equivalent piece of code without the use of potentially dangerous pointer casting:
that didnt really help. im following the manual on this site dont know anything about <limits> or templates... i need it really broken down ;/ and how are the pointers potentially dangerous?
A void* is just like any other pointer, except it has no type. For increase() to know what type of data it is, "psize" is set along with it.
So: increase(&a, sizeof(a)); // pass the location of a with it's size
if (psize == sizeof(char)) // which it does, since we gave it sizeof(a), a being a char.
1 2 3 4
char* pchar; // declare another char*
pchar=(char*)data; // set it's value (the address) to the void* passed (which is being cast as a char*)
// the value of pchar is now the same as &a in main()
++(*pchar); // increment the value in the de-referenced pchar.
I had functions passing void* once, but in the end decided a simple overload/template was the way to go.
High (maybe low) level APIs might return a void*, in this case it would be your job to immediately cast it to the proper type.
//increase is a function that takes in data, a pointer to something, as well as the size of the
//data type pointed to by data (psize). Based on psize, this function converts
//data to the appropriate pointer type and increments the value pointed to by one.
//Currently this function assumes that data points either to a char or an int
void increase (void* data, int psize)
{
if ( psize == sizeof(char) )
{
//here we know that data points to a char, because the size of
//the datatype matches that of char
char* pchar; //instantiate a pointer to a char
pchar=(char*)data; //convert data to be a pointer to a char, store the pointer in pchar
++(*pchar); //increment what pchar points to by one
}
elseif (psize == sizeof(int) )
{
//here we know that data points to an int, because the size of
//the datatype matches that of int
int* pint; //instantiate a pointer to an int
pint=(int*)data; //convert data to be a pointer to an int, store the pointer in pint
++(*pint); //increment what pint points to by one
}
}
im following the manual on this site dont know anything about <limits> or templates...
Converting between pointer types is potentially dangerous because if the converted pointer type is wrong, you could end up doing undefined behaviour such as writing over memory you don't own.
Observe the following:
1 2 3 4 5 6 7 8 9
int main()
{
char aCharacter = 'a';
int pint = (int*) &aCharacter; //pint points to an int (say 4 bytes) but the char is only one byte
*pint = 1000000; //we have now just played with memory we potentially don't own (the extra three bytes beside where aCharacter is in memory
return 0;
}
OHHH I UNDERSTAND A LITTLE BIT MORE Maybe.
pchar=(char*)data;
so char points to data because its not defined as an integer or a character. so you can use it in both situations like this too
pchar=(int*) data;
right?
pchar=(char*)data;
This means: take the object named data, and pretend it is a char* (i.e. pretend it is a pointer to a char). Make pchar equal to that pointer (i.e. pchar is a char*, pointing at whatever data is pointing at).
pchar=(int*) data;
This means: take the object named data, and pretend it is a int* (i.e. pretend it is a pointer to a int). Make pchar equal to that pointer (i.e. pchar is a int*, pointing at whatever data is pointing at).
This kind of casting, using void*, is bad. Do not do it unless you really have no choice. There are far better ways to do this.
void increase (void* data, int psize) /*increases takes a type of data and a size. void* data is the
data type because the pointer changes the meaning of data stored at its memory address? */
{
if ( psize == sizeof(char) ) // if psize is equal to the size of the character entered
{ char* pchar; pchar=(char*)data; ++(*pchar); }// pchar is the value pointed by char.
// pchar = the value of what char is pointing to.
// increase the value of what pchar is pointing to by one.
elseif (psize == sizeof(int) ) // if psieze is equal to the size of the number entered
{ int* pint; pint=(int*)data; ++(*pint); } // pint is the value pointed by the number
// pint is the value of what int is pointing to
// increase the value of what pint is pointing to by one.