Why are pointers frightening?

Started by
42 comments, last by Beer Hunter 18 years, 11 months ago
Quote:why are pointers scary?



  int array[10];  int * intptr = array;  int value = ask_user_for_item();  printf( "You chose item %d: %d\n", value, intptr[value] );  printf( "Setting it to 0.\n" );  intptr[value] = 0;



Now, if your routine ask_user_for_item() may return values < 0, or > 9, you have a bug in the program, where an arbitrary memory location can be first read, and then set to 0, and there's really no error checking that the compiler can do.

The basic scare with pointers is that a pointer doesn't come with a "minimum" and "maximum" value that you can compare/assert/validate against. If you have a bug that causes a pointer value (or offset) to go out of whack, arbitrary data in your program, entirely un-related to what the pointer is supposed to be doing, will get blasted. This makes for hard-to-find bugs.
enum Bool { True, False, FileNotFound };
Advertisement
Things not to do with pointers.

#1, point to an arbitrary location in memory.
int *ptr;
ptr = 10000; //i'm not sure, but I think you might have to cast in order for this to work.

#2, not setting a pointer to NULL (or zero) when you create it, before you allocate memory.
#3, not setting a pointer to NULL after you destroy it.
#4, not checking for NULL before you use it.
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
pointers can save a lot of memory - for example a pointer to the users current weapon ( in a games example) uses 4 bytes, without them it would be harder to see which current weapon there is etc.

In games i have found them invlauable for dynamics, bu cant really see much use in console apps
JUST-CODE-IT.NETManaged DirectX & C# TutorialsForumsArticlesLinksSamples
void function(int **param[])

how the hell do you read that anyway? is it: an array of pointer addresses? and how would you use it?

Beginner in Game Development?  Read here. And read here.

 

Pointers merely depend on the situation, and I rather like them.
You would use the double pointer to an array of pointers for linked lists, as they have been previously stated. Linked lists are good if you are going to manipulating the order, size, etc., of an array of objects. If you move or delete something, instead of having to shift the whole slew of objects following, you would merely shift the 32-bit (for Windows) integral value instead.
In other words, the pointers can be used for simplicity, or efficiency.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Quote:Original post by Alpha_ProgDes

void function(int **param[])

how the hell do you read that anyway? is it: an array of pointer addresses? and how would you use it?


I'm assuming it's a vector with the last item in the array a null.
Hi,

Pointers are frightening because people fear what they do not know.

Pointers are just an extension of basic Assembly language. In assembly you reference data using a base memory position where your data is stored (the DS segment) and you access a specific point using an offset ([DS]+5 for example). Almost all data access is done this way though memory directions. Pointers are just an extension of that. They are basic elements that help you to talk with the computer in its own language. Denying this, is just limiting yourself.

Even languages that self-proclaims as 'no pointer' languaje as Java (or .Net) use pointers. Why do you think you have to use the 'new' keyword? You are allocating memory for your sctructures and memory allocation requires a pointer to hold the address for that memory. They just mask if so it looks like no pointers are used... but its always the same.

Pointers aren't evil. That affirmation is just like saying a gun is evil. They are just a tool that placed in the right hands can create Doom3 or HalfLife2. Placen in wrong hands they can format your HDD.

Anyway, once you get used to them, they are a tool so useful that its almost impossible to live without them.

Luck!
Guimo

Basically level 1 pointers can be handle without much problems, but when you start using higher level mean more dereferencing, or mixing with OOP and complex data structure then that's when problems can arrive. Mostly what everyone probably experienced is prob dereferencing a bad pointer and pretty much crash your program.
Quote: how the hell do you read that anyway? is it: an array of pointer addresses? and how would you use it?


its an array of pointers to pointers to ints, which is the same thing as ***int (if you know you are using the first pointer to access an array)

just like:

int main(int argc, char **argv){

is the same as

int main(int argc, char *argv[]){

with the slowest varying dimension represented by the []'s and being of size argc. argv is an array of arrays of characters, its just that C doesn't let you do argv[][].

void function(int **param[]) is exactly the same thing as void function(int ***param)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement