Thread: True or False

  1. #1
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693

    True or False

    A pointer is passed to a function, the data the pointer is pointing to is changed, then the actual address that the pointer holds is changed. After the function returns, the data the pointer points to is modified and the pointers address is not modified.
    The answer is true, I thought it was false. Will someone help me grasp this concept, my thought process was that say the pointer points to a pointer that was malloced in the function, and the function returns, wouldn't the pointer now have a new address instead of the old one? Due to the malloc returning an address on the heap.

    here is an example

    Code:
    void function(int *p)
    {
    int*y=malloc(4);
    p=y;
    
    return;
    }
    Last edited by camel-man; 02-13-2013 at 05:35 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I think what you're looking at is something like:
    Code:
    #include <stdio.h>
    
    void foo(char *str)
    {
        *str = 'T';  // change the data pointed to by the pointer
                     // make it a 'T' -- this change is visible in main
        str++;  // change the address the pointer points to
                // str now points the the second char in buf -- this change will be lost when the function returns
    }
    
    int main(void)
    {
        char buf[] = "this is a test";
        printf("buf = %s\n", buf);
        foo(buf);
        printf("buf = %s\n", buf);  // Now, with upper case 'T' at the start, but still the whole string
                                    // if the address pointed to were changed, this would print "his is a test" -- missing the initial 'T'
        return 0;
    }
    What's it's saying is that the address passed in a pointer parameter is "pass by value", i.e. changing what the pointer points to in foo wont change the pointer you passed in (e.g. buf), but you can change the data pointed to by buf.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    C is pass-by-value, which means that any changes to the values of the parameters of a function will not be visible to the calling function. There is some confusion about pass-by-reference using pointers in C. There is no pass-by-reference in C. You passed a pointer, whose value is an integer representing a memory address. You can change this value all you want inside the function, its impossible to make these changes visible outside the function. Either pass a pointer-to-pointer or return the pointer value.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I think what you are wanting to do is something like this:

    Code:
    void function(int **p)
    {
      int*y=malloc(4);
      *p=y;
     
      return;
    }
    What you have done is not unlike this:

    Code:
    void function(int p)
    {
      int y=4;
      p=y;
     
      return;
    }
    You if you need to modify a variable in the above scope, you need to use a pointer - Therefore, if what you wish to modify is a pointer, you need to create a pointer to it.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Ok thank you guys, apparently I have been thinking all wrong about pointers and heap memory, Ill have to let this soak in my small brain haha

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by camel-man View Post
    Ok thank you guys, apparently I have been thinking all wrong about pointers and heap memory, Ill have to let this soak in my small brain haha
    There is nothing wrong with your understanding with heap memory (or at least, I don't think there is) - Your confusion seems to be with the fact that when the function goes finishes, the variable "p" loses its value - And that value was the address of memory returned by malloc
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    struct Stack
    {
            int top;
            int capacity;
            ItemT *items;
    };
    
    
    void pushStack(StackP stack, ItemT item)
    {
            stack->top++;
            //if stack is full then use grow function to double capacity
            if(stack->top==stack->capacity)
                    grow(stack);
            // setting item on top of stack
            stack->items[stack->top]=item;
    }
    
    void grow(StackP stack)
    {
    /*Checking for valid memory in temp pointer, and making array grow*/
            ItemT *temp=malloc(sizeof(ItemT)*(stack->capacity*2));
            if(temp==NULL)
    	  {
    	   fprintf(stderr,"Failed to allocate memory for temp!\n");
    		 exit(1);
    	  }
            int i;
            for(i=0;i<stack->top;i++)
                    temp[i]=stack->items[i];
    
    	//Freeing memory back to system of old array
            free(stack->items);
            stack->items=temp;
            //changing stacks new capacity
            stack->capacity=stack->capacity*2;
    }
    This is where I have lost all concept of why pointers do not return the value that malloc has assigned.

    In my grow function I am basically making a temp pointer and doubling that size of the current stack. Then free the stack in my structure and point the stack to the new temp array. I guess I just do not really understand what is happening behind the scenes like I thought I did, because it still has the same array when I use grow it seems to work fine. P.S. //StackP is typedefed as a pointer to a struct stack

  8. #8
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by camel-man View Post
    [code]

    This is where I have lost all concept of why pointers do not return the value that malloc has assigned.

    In my grow function I am basically making a temp pointer and doubling that size of the current stack. Then free the stack in my structure and point the stack to the new temp array. I guess I just do not really understand what is happening behind the scenes like I thought I did, because it still has the same array when I use grow it seems to work fine. P.S. //StackP is typedefed as a pointer to a struct stack
    The stack code appears to be correct in its use of memory, although perhaps realloc() would be more efficient and appropriate in this case. What you are doing is to create a new buffer twice the size, copying the old values into the new buffer, and reassigning the stack's item pointer to this new buffer, but not "freeing the stack" itself. Every time you call grow, a new buffer is allocated, so it cannot "have the same array" after this call.

  9. #9
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    True, but I was getting the impression that when I am done calling grow then returning back to whatever function called it, the memory will no longer be pointed to by the items pointer. That is how I am perceiving this in my mind, see the example I posted in the original post, how does that one differ in memory management from the one I posted with the stack, I thought they were the same.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by camel-man View Post
    True, but I was getting the impression that when I am done calling grow then returning back to whatever function called it, the memory will no longer be pointed to by the items pointer. That is how I am perceiving this in my mind, see the example I posted in the original post, how does that one differ in memory management from the one I posted with the stack, I thought they were the same.
    The difference is that in your stack example you are not modifying the pointer you pass to the function (stack) but instead you are modifying a pointer within the struct (stack->items).
    You basically do something like:
    Code:
    void func(int *p)
    {
         *p = 123;
    }
    i.e. changing the value of the object the pointer points to. And this is visible to the caller.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. true or false
    By johngav in forum C Programming
    Replies: 4
    Last Post: 03-19-2009, 02:25 PM
  2. need help.. true/false
    By salmansalman in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2008, 10:10 AM
  3. True or False
    By noob programmer in forum C Programming
    Replies: 9
    Last Post: 10-29-2008, 10:15 AM
  4. True / False
    By goran00 in forum C Programming
    Replies: 13
    Last Post: 03-14-2008, 03:26 PM
  5. True or false
    By Eibro in forum Tech Board
    Replies: 9
    Last Post: 09-15-2002, 08:07 AM