Thread: Seg Fault trying to assign a value to an array

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Seg Fault trying to assign a value to an array

    I create a function called define_array which takes a pointer as a parameter and makes it point to a new array allocated in the heap whilst also returning the new arrays size (in this case hard coded)

    Line 11 however gives a Seg Fault when trying to assign a value specifically to any element after 2. It works just fine on the first two elements. Any idea why?

    Code:
    #include <stdio.h>
    
    #define TEST_ARRAY_SIZE 100
    
    int define_array(int **new_ptr)
    {
        *new_ptr = malloc(sizeof(int) * TEST_ARRAY_SIZE);
        int array_size = 3;
        *new_ptr[0] = 40;
        *new_ptr[1] = 50;
        *new_ptr[2] = 20; // seg fault
        printf("%d\n%d", *new_ptr[0], *new_ptr[1]);
        return array_size;
    }
    
    int main ()
    {
        int *local_ptr = NULL;
        define_array(&local_ptr);
        return 0;
    }
    Last edited by Vespasian_2; 04-05-2016 at 10:29 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I can't duplicate your problem, probably because the code doesn't compile on my machine.

    main.c||In function ‘define_array’:|
    main.c|7|error: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]|
    main.c|7|warning: incompatible implicit declaration of built-in function ‘malloc’|
    It looks like you forgot to include a required file.


    However the problem is probably that you need to use parentheses when accessing the array elements.


    Code:
        (*new_ptr)[0] = 40;
    Jim

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by jimblumberg View Post
    I can't duplicate your problem, probably because the code doesn't compile on my machine.


    It looks like you forgot to include a required file.


    However the problem is probably that you need to use parentheses when accessing the array elements.


    Code:
        (*new_ptr)[0] = 40;
    Jim
    Tried your second solution and although there is no compile error, its printing garbage for elements 2 and 3.

    For me to try out your first proposed solution, what do I need to do? What do you mean forgot to include a required file?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Tried your second solution and although there is no compile error, its printing garbage for elements 2 and 3.
    Show your current code.

    For me to try out your first proposed solution, what do I need to do? What do you mean forgot to include a required file?
    Look up the documentation for the functions you're using and be sure to include the proper include files.

    Jim

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by jimblumberg View Post
    Show your current code.


    Look up the documentation for the functions you're using and be sure to include the proper include files.

    Jim
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    #define TEST_ARRAY_SIZE 100
    
    
    int define_array(int **new_ptr)
    {
        *new_ptr = malloc(sizeof(int) * TEST_ARRAY_SIZE);
        int array_size = 3;
        (*new_ptr)[0] = 40;
        (*new_ptr)[1] = 50; // prints garbage
        (*new_ptr)[2] = 20; // prints garbage
        printf("%d\n%d\n%d", *new_ptr[0], *new_ptr[1], *new_ptr[2]);
        return array_size;
    }
    
    
    int main ()
    {
        int *local_ptr = NULL;
        define_array(&local_ptr);
        return 0;
    }
    Ive included stdlib and stdio. Those are the only two libraries I need

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Quote Originally Posted by Vespasian_2 View Post
    Tried your second solution and although there is no compile error, its printing garbage for elements 2 and 3.
    Did you also apply the () to the expressions in the printf call, as in the assignments?

    > *new_ptr[0]
    This is index, then dereference.

    > (*new_ptr)[0]
    This is dereference, then index.
    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.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Why do you think you need the parentheses when assigning but not when printing? You need them in both places. Or, better yet, print the values in main.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Ive included stdlib and stdio. Those are the only two libraries I need
    You mean "I've included stdlib.h and stdio.h, those are the only two include files I need." Include files are not libraries.

    That is correct you need stdlib.h for malloc() and stdio.h for printf().

    Your printing "garbage" because you missed the parentheses in your printf() statement. The problem is being caused by operator precedence, so you need the parentheses for every occurrence.


    Jim

  9. #9
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    F sakes, I forgot to apply the parenthesis in the printf.

    Thanks all, it works


    Quote Originally Posted by jimblumberg View Post
    You mean "I've included stdlib.h and stdio.h, those are the only two include files I need." Include files are not libraries.

    That is correct you need stdlib.h for malloc() and stdio.h for printf().

    Your printing "garbage" because you missed the parentheses in your printf() statement. The problem is being caused by operator precedence, so you need the parentheses for every occurrence.


    Jim
    Im assuming the difference between the two is explained here:
    c++ - What&#39;s the difference between a header file and a library? - Stack Overflow

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Im assuming the difference between the two is explained here:
    Pretty much, yes. Or you could go with a compiler's documentation explanation.

    Libraries for use by C programs really consist of two parts: header files that define types and macros and declare variables and functions; and the actual library or archive that contains the definitions of the variables and functions.

    (Recall that in C, a declaration merely provides information that a function or variable exists and gives its type. For a function declaration, information about the types of its arguments might be provided as well. The purpose of declarations is to allow the compiler to correctly process references to the declared variables and functions. A definition, on the other hand, actually allocates storage for a variable or says what a function does.)
    Jim

  11. #11
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by Salem View Post
    Did you also apply the () to the expressions in the printf call, as in the assignments?

    > *new_ptr[0]
    This is index, then dereference.

    > (*new_ptr)[0]
    This is dereference, then index.
    I have tried doing the pointer arithmetic version of this code instead of accessing elements through an array notation. I've been bashing my brains out for hours, its just not working. Check this code (I simplified the array to take 2 elements not 3):

    Code:
    #include <stdio.h>#define TEST_ARRAY_SIZE 100
    
    
    int define_array(int **new_ptr)
    {
        *new_ptr = malloc(sizeof(int) * TEST_ARRAY_SIZE);
        int array_size = 2;
        int **iterator_pointer = new_ptr;
        (*new_ptr)[0] = 40;
        (*new_ptr)++;//this is the pointer arithmetic solution which doesn't work. This instruction moves the pointer to pointer to point to one element down the array
        **new_ptr = 50;//this then should assign the value of 50 to the next element which was "tagged" before.this is the pointer arithmetic solution which doesn't work. This for some reason DOES NOT update the second element but REPLACES the 40 and the second element is left as garbage values
        //(*new_ptr)[1] = 50; //this was the original array'd solution which works perfect. Thanks for that
        return array_size;
    }
    
    
    void print_array (int *pointer_to_first_element, int array_size)
    {
        int counter;
        for(counter = 0; counter < array_size; counter++)
        {
            printf("%d\n", *pointer_to_first_element);
            pointer_to_first_element++;
        }
    }
    
    
    int main ()
    {
        int *local_ptr = NULL;
        int array_size;
        array_size = define_array(&local_ptr);
        print_array (local_ptr, array_size);
        return 0;
    }
    You will see that your original array'd solution is in there and it works perfectly. But when I try the pointer arithmetic version it doesnt work:
    The statements on 10 and 11 for some reason DOES NOT update the second element but REPLACES the 40 and the second element is left as garbage values. WHY on earth is it doing this???
    Last edited by Vespasian_2; 04-08-2016 at 10:41 AM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > (*new_ptr)++;//this is the pointer arithmetic solution which doesn't work. This instruction moves the pointer to pointer to point to one element down the array
    The difference between what you wrote, and

    (*new_ptr)[1] = 50;
    is that it does NOT modify new_ptr, or what it points to.

    int *temp = *new_ptr;
    *temp++ = 40;
    *temp++ = 50;

    is another way.
    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.

  13. #13
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by Salem View Post
    > (*new_ptr)++;//this is the pointer arithmetic solution which doesn't work. This instruction moves the pointer to pointer to point to one element down the array
    The difference between what you wrote, and

    (*new_ptr)[1] = 50;
    is that it does NOT modify new_ptr, or what it points to.

    int *temp = *new_ptr;
    *temp++ = 40;
    *temp++ = 50;

    is another way.
    Oh I tried to manipulate with pointer to pointer but actually needed to manipulate with a pointer. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assign values 2d array to normal array
    By Gertjan Haan in forum C Programming
    Replies: 1
    Last Post: 10-29-2015, 11:10 PM
  2. assign the value of an array to a pointer c++
    By kox in forum C++ Programming
    Replies: 8
    Last Post: 10-29-2012, 11:33 AM
  3. Assign array to another.
    By boblettoj99 in forum C Programming
    Replies: 8
    Last Post: 12-03-2009, 04:32 PM
  4. how to assign value in two -dimensional array?
    By zcrself in forum C Programming
    Replies: 7
    Last Post: 11-26-2009, 11:00 PM
  5. assign value of pointer array
    By zcrself in forum C Programming
    Replies: 12
    Last Post: 08-17-2009, 02:12 PM