Thread: 5 Common Mistakes Beginners Make With Pointers C

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4,183

    Smile 5 Common Mistakes Beginners Make With Pointers C


  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He's kinda off on a few points.
    Quote Originally Posted by some dude
    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.
    However, dereferencing pointers to invalid memory causes problems. Thus, *( arr - 1000 ) core dumps because you are trying to access the address.
    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.

    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".

    Quote Originally Posted by some dude
    So When I try to execute it, I get core dump. What is wrong with this code. Can we not type cast an int pointer to a double type?
    The answer is I am not casting an int to a double, I am casting an int * to a double *. That's not safe if sizeof(double) and sizeof(int) aren't the same.
    That may or may not be true. See:
    Quote Originally Posted by C: A Reference Manual, 5th ed, p. 133
    "C does not dictate the sizes used for the floating-point types or even that they be different.* The programmer can assume that the values representable in type float are a subset of those in type double, which in turn are a subset of those in type long double. Some C programs have depended on the assumption that double can accurately represent all values of long--that is, converting an object of type long to type double and then back to type long results in exactly the original long value. Although this is often true, it cannot be depended on.
    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.)


    * 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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I didn't even know there was a post other than yours.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    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.
    Not really, it's not valid.
    This is not granteed by standard.
    Code:
    p = array + 1000;
    if( p > array)
    There's no grantee it will work.

    It's already mentioned here.
    Perhaps expert forgot to read faq.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 Common Controls in C++, how do i make and use them?
    By C+noob in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2006, 11:53 AM
  2. Just a few minor mistakes
    By HAssan in forum C Programming
    Replies: 4
    Last Post: 01-09-2006, 05:26 AM
  3. Newbie Mistakes
    By RoD in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2003, 09:22 PM
  4. Spelling mistakes
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 03-05-2002, 08:24 PM