Hello question about the infamous pointers

This is a discussion on Hello question about the infamous pointers within the C Programming forums, part of the General Programming Boards category; Hello I am kind of stuck on this idea of how memory works in C. Is the tmpPtr pointing to ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    Hello question about the infamous pointers

    Hello I am kind of stuck on this idea of how memory works in C.
    Is the tmpPtr pointing to the 3rd element in the array or the 7th in the second if statement. If the later is that why I would use free()??? Thank you for any help given it is greatly appreciated.
    Code:
    int array[10], *ptrToArr;
    ptrToArr = &array[0];
    
    if(some condition){
        int *tmpPtr;
        tmpPtr = ptrToArr;
        printf("%d", *tmpPtr+4);
    }
    
    if(this is also true){
        int *tmpPtr;
        tmpPtr = ptrToArr;
        printf("%d", *tmpPtr+2)
    }
    
    ptrToArr++;

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Quote Originally Posted by seanfmglobal View Post
    Is the tmpPtr pointing to the 3rd element in the array or the 7th in the second if statement.
    Neither, still at the first.

    Quote Originally Posted by seanfmglobal View Post
    If the later is that why I would use free()???
    Never, repeating, never use free with static or stack/temporary memory!
    Devoted my life to programming...

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    A better question would be why people think things like this help anyone understand pointers. Your questions:

    s the tmpPtr pointing to the 3rd element in the array or the 7th in the second if statement.
    Think of it in terms of distance from the starting element.

    However in the section you are referencing, the code may be incorrectly written, since it dereferences tmpPtr first, then offsets that value by +2. It's no different from x + 2, if x were for example an integer. With pointers involved, you need to group the addition so it's performed first if what you mean is to dereference element 2. tmpPtr[2] is almost always clearer.

    If the later is that why I would use free()???
    You only need to free the pointers that malloc(), calloc() or realloc() returns.

    The reason why you free() is because eventually the pointer to memory will fall out of scope, and be destroyed. Unless you call free() the memory you pointed to has no active references, and it just sits there being wasted, waiting to be released.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenCV and Visual Studio 2010: Infamous namespace error.
    By Lundbergteam in forum C++ Programming
    Replies: 5
    Last Post: 01-09-2012, 07:00 AM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. The infamous Carwash Queue example...
    By honda882000 in forum C Programming
    Replies: 4
    Last Post: 03-25-2007, 09:07 AM
  4. Pointers to pointers question
    By mikahell in forum C++ Programming
    Replies: 10
    Last Post: 07-22-2006, 12:54 PM
  5. Pointers Question.....Null Pointers!!!!
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2001, 10:13 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21