Click to See Complete Forum and Search --> : A few things...
Amon Ra
Apr 24th, 2001, 09:07 PM
I have found these in other threads:
casting with pointers.
function pointers.
What are these??
Thanks :)
HarryW
Apr 24th, 2001, 09:27 PM
Functions are just blocks of executable code in memory. When a function is called, basically the parameters for the function are placed on the stack, there is the equivalent of a goto moving control to the first line of code in the function, and the parameters are then taken from the stack. So, since functions are just blocks of code resident in memory, they also have addresses, just like anything else in memory. A pointer to a function points to the beginning of that block. Function pointers can be very useful in certain situations. For instance, if you have several different graphics rendering functions that all take the same parameters and return the same values, but are optimised for different graphics cards, you only want to make the decision once of which function to use. So you assign the address of the one you want to use to a function pointer, and then call the function using the function pointer, rather than deciding which function to use every time you want to call it.
Casting is just treating data as a particular type. A typical time in C++ when you would cast data is when you have a void pointer (of type void *) and you want to assign the value of the data at that address to some other variable. A void pointer is a pointer to any type, it is a sort of variant datatype, and in C++ (not C) you must cast it to a specific type before you can use it. That means you have to tell the compiler what kind of data you want to treat it as. Also, if you have a float variable and want to get the integer part of it, you can cast that variable to an integer data type to get at the whole number value, ignoring all data after the decimal point (or binary point I suppose :rolleyes: ).
They are both quite simple. Function pointers are simple in theory but can be complicated in practice, using C/C++ at least. They are a common cause of problems.
Amon Ra
Apr 24th, 2001, 10:06 PM
Do you mind giving me an example for both? Thanks:)
HarryW
Apr 25th, 2001, 01:29 PM
Okay..... I'll try..
int (*pRenderGraphicsFunc)(void *pVidMem, int iColourDepth); //function pointer
// I am not sure that this declaration is correct though
int RenderGraphicsATI(void *pVidMem, int iColourDepth)
{
// function optimised for ATI graphics card
}
int RenderGraphicsMatrox(void *pVidMem, int iColourDepth)
{
// function optimised for Matrox graphics card
}
int RenderGraphicsNvidia(void *pVidMem, int iColourDepth)
{
// function optimised for Nvidia graphics cards
}
int RenderGraphicsGeneric(void *pVidMem, int iColourDepth)
{
// generic unoptimised function
}
int GetGraphicsCard()
{
// function returning a constant representing the card manufacturer
}
int main()
{
switch(GetGraphicsCard())
{
case GGC_ATI:
pRenderGraphicsFunc = RenderGraphicsATI;
break;
case GGC_MATROX:
pRenderGraphicsFunc = RenderGraphicsMatrox;
break;
case GGC_NVIDIA:
pRenderGraphicsFunc = RenderGraphicsNvidia;
break;
default:
pRenderGraphicsFunc = RenderGraphicsGeneric;
}
// other code....
// now when you want to render something:
RetVal = pRenderGraphicsFunc((void *)pVidMem, ColourDepth);
// you don't have to decide which function to use every time you
// want to render something
return(0);
}
That's an example of function pointer use. Casting pointers is also in there - where the function is called from the function pointer, the pVidMem variable, which is a pointer to the first pixel in memory is cast to void*. This is because the type of variable it points to might be 8-bit, 16-bit, 24-bit or 32-bit, depending on the colour depth that it's being rendered at. So we don't want to pass a pointer of any specific type, just a generic pointer containing the address of the first location in video memory which we can then cast back to, for instance, a 16-bit integer if we wanted a colour depth of 16 bits.
Since a pointer is always just a 32-bit integer containing the address of another variable which could be of any size, we can pass this data without encoding the type that it points to and still have some meaningful data (the address), so long as we have some mechanism for knowing the kind of data it points to later on, when we want to use or alter that data. So we cast from [/b]void*[b], a generic pointer, to a different pointer type, so that the compiler knows how big the block of data at the location the pointer points to is, and knows how to deal with it.
Amon Ra
Apr 25th, 2001, 06:51 PM
:) :) :) :)
Thanks a lot! I think i understand the function pointer pretty well now:)
and the casting is a bit confusing:)
HarryW
Apr 25th, 2001, 06:58 PM
Well its all to do with C and C++ being strongly-typed languages, which means type errors can be trapped at compile time rather than run time, since run time errors are harder to trace.
Casting a variable just means you have some data as one type and you want to use that data as a different type.
Amon Ra
Apr 25th, 2001, 07:23 PM
Yep, thanks a lot :):)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.