Segmentation fault
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Segmentation fault

  1. #1
    Join Date
    Nov 2009
    Posts
    4

    Segmentation fault

    This question is probably very stupid but my C is very rusty...

    Basically I have this piece of code:
    int* test;

    int N = 3;
    size_t size = sizeof(int) * N;
    int *t = (int*)malloc(size);

    for(int i=0;i<N;i++)
    {
    t[i] = i;
    }

    test = t;
    printf("%d,%d,%d \n",test[0],test[1],test[2]);

    Now I want to refactor the allocation code into a method:
    void arrayTest(int * t, int N)
    {
    size_t size = sizeof(int) * N;
    t = (int*)malloc(size);

    for(int i=0;i<N;i++)
    {
    t[i] = i;
    }
    }

    int* test;
    arrayTest(test,3);
    printf("%d,%d,%d \n",test[0],test[1],test[2]);

    But I always get a segmentation fault. I thought arrays would be call by reference. So the allocation should have an effect outside of the method.

    Thanks!

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Segmentation fault

    Quote Originally Posted by !pjk! View Post
    I thought arrays would be call by reference. So the allocation should have an effect outside of the method.
    C doesn't have call-by-reference like C++ does. The best C can do is to simulate it using pointers; which is the reason why arrays may appear to behave this way. What they're pointing to, the actual array contents, is (effectively, if not technically) being passed by reference. But the array pointer itself is *not*---the pointer is being copied by value.

    In order to make a method as you are attempting, you either need to make the parameter an int**, or you need to make the return value of the function be the new pointer. The second is the approach taken by the realloc() method, incidentally.

  3. #3
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Segmentation fault

    You pass the array to arrayTest() by value. That way you just modify a copy of the pointer.
    You have to pass by reference
    e.g.
    Code:
    void arrayTest(int ** t, int N) {
        size_t size = sizeof(int) * N;
        *t = malloc(size); /* dont cast when using C */
    
        for(int i=0;i<N;i++) {
            (*t)[i] = i;
        }
    }
    Kurt

  4. #4
    Join Date
    Nov 2009
    Posts
    4

    Re: Segmentation fault

    This is what I was suspecting but why is this working?

    void arrayTest(int * t, int N)
    {
    //size_t size = sizeof(int) * N;
    //t = (int*)malloc(size);

    for(int i=0;i<N;i++)
    {
    t[i]++;
    }

    }


    int main(int argc, char ** argv)
    {
    int test[3] = {0,0,0};

    printf("&#37;d,%d,%d \n",test[0],test[1],test[2]);

    arrayTest(test,3);

    printf("%d,%d,%d \n",test[0],test[1],test[2]);
    }

    It looks like the array is passed by reference?

  5. #5
    Join Date
    Nov 2009
    Posts
    4

    Re: Segmentation fault

    Quote Originally Posted by !pjk! View Post
    This is what I was suspecting but why is this working?

    void arrayTest(int * t, int N)
    {
    //size_t size = sizeof(int) * N;
    //t = (int*)malloc(size);

    for(int i=0;i<N;i++)
    {
    t[i]++;
    }

    }


    int main(int argc, char ** argv)
    {
    int test[3] = {0,0,0};

    printf("%d,%d,%d \n",test[0],test[1],test[2]);

    arrayTest(test,3);

    printf("%d,%d,%d \n",test[0],test[1],test[2]);
    }

    It looks like the array is passed by reference?
    My question qas probably not well explained: This code produces

    0 0 0
    1 1 1

    as output. So it seems that the array is passed as reference (effectively not technically as I understand)?
    But you said that arrays are passed as value. This is why I am confused...

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Segmentation fault

    You shouldn't talk about an array being passed to the function. The array is *not* what's being passed. Instead, your are passing a pointer to the array.

    The pointer itself is passed by value, so modifications to the pointer inside the function are not reflected afterwords. But the address held by the pointer is the same whether you're working from a copy of the pointer or not; so you're free to modify that memory at will, and it will be reflected outside the function.

    Think of it like a dog on a leash. You can put as many leashes as you like on the same dog, and changes made to one leash won't affect any of the others. But they're all attached to the same dog. If you attach one of the leashes to a second dog, that doesn't affect anything relating to any of the leashes attached to the first. In this analogy, leashes are pointers and dogs are the arrays they're directed at.
    Last edited by Lindley; November 8th, 2009 at 11:57 AM.

  7. #7
    Join Date
    Nov 2009
    Posts
    4

    Re: Segmentation fault

    Thank you! I got it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center