5 Common Mistakes Beginners make with Pointers in C | Experts-Linked
Printable View
He's kinda off on a few points.
We had a thread a couple of years ago probably arguing this point. Given an array of X size, you cannot point to array + X for anything other than checking to see that you have gone too far. There was lengthy debate as to what you were actually allowed to do by the standard. I believe he's falling into the same pit.Quote:
Originally Posted by some dude
Sure, he acknowledges that you will have a problem when you dereference it, but C goes further than that in the standard by telling you not to point off the end of your array at all. While it is "legal" (your compiler will let you do it) to point wherever you want, you actually shouldn't, and should only ever point to an object that you know you have full knowledge of. You can point to an int you make, because you've made the integer. You can point at something malloc gives you, because it knows specifically about it. You can't point to "array + 1000", because you have no idea what that is.
It's funny that he points out dangling pointers, as he calls them, as a problem, and then goes on to say that you can do "array + 1000".
That may or may not be true. See:Quote:
Originally Posted by some dude
He's assuming that an int * and a float * are pointing to different sized objects, but it doesn't have to. (That was my whole point in that long quote.)Quote:
Originally Posted by C: A Reference Manual, 5th ed, p. 133
* My note, not the book's: They are talking about the difference between float, double, long double, and complex-type-specifier. Not the difference between integer types and floating point types.
Anyway, here's the gist of his article:
1. Invalid pointers (null, or uninitialized)
2. Dangling pointers
3. Pointer Arithmetic (doing your math wrong)
4. Typecasting pointers. (should have been typedef-ing pointers)
5. memory leak
Not bad, but I'm pedantic, so a couple of points bugged me.
Quzah.
The problem with listing common mistakes made by beginners is that they are written by experts (whether recognised or self-claimed) who neglect to consider there may be a list of "Common mistakes made by people who think they are experts".
Here's a list made by someone who likes to pretend he's an expert (me):
1. Overthinking them. Forgetting that pointers are just like every other variable. They store a value, that's all. Just like everything else in C.
2. Confusing pointers and arrays. They aren't the same. They're similar. Like twins, they are similar, but not the same person.
3. Making them hard to read. (typedefing pointers) It seems like a neat thing to do, but it's really not. It confuses point #1.
4. Not freeing what you allocate. Sure, your compiler will probably take care of it. It doesn't have to, but it probably will. But it makes your code ugly, and it bugs me.
The using them uninitalized really goes for any variable, it's not just a "pointer thing", so I'm not including it in my list. But I agree with freeing what you allocate. I've never seen dangling pointers as a problem, simply because I stop using them when I know I've freed them. I also don't consider returning the address of local variables a "dangling pointer". Also, pointer maths are not really a common thing to mix up in my use of it. Most people don't randomly add or subtract them. You tend to increment or decrement by one, and that's that. It's uncommon for you to apply random math to them.
But for a quick go, that's probably my list. The main thing I see people have a problem with is simply understanding that pointers are just variables. They just hold a value.
Quzah.
Anyone know how the original poster of this thread was deleted?
I just posted a good link; since the OP posted a bad link.
Tim S.
I didn't even know there was a post other than yours.
Quzah.
> Anyone know how the original poster of this thread was deleted?
Yeah, that was me.
Their link was broken, and I didn't feel like fixing it.
Plus it looks for all the world like a 1-poster with a "visit my site" link.
Not really, it's not valid.Quote:
In general, you can make a pointer point anywhere. For example, arr + 1000 is valid, even if the array has only ten elements. arr - 1000 is also valid. That is, you can compute it, and it won't core dump.
This is not granteed by standard.
There's no grantee it will work.Code:p = array + 1000;
if( p > array)
It's already mentioned here.
Perhaps expert forgot to read faq. ;)