Thread: array on heap, pointers to and from functions, Bus error: 10

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    80

    array on heap, pointers to and from functions, Bus error: 10

    Hello,

    Any ideas what's wrong with this?:

    Code:
    typedef unsigned char * test;
    
    void init_test(test *t);
    void insert(test *t);
    void free_test(test *t);
    
    
    int main() {
        test t;
    
    
        init_test(&t);
        insert(&t);
        
        printf("%d\n", t[0]);
    
        free_test(&t);
        
        return 0;
    }
    
    
    
    
    void init_test(test *t) {
        *t = malloc(100 * sizeof(*t));
    }
    
    
    void insert(test *t) {
        *t[0] = 200;
        *t[1] = 201;
        *t[2] = 202;
        *t[3] = 203;
    }
    
    
    void free_test(test *t) {
        free(*t);
        *t = NULL;
    }
    I know you're not really supposed to put *'s in typedefs but I really want to just for this example.

    The bus error is happening, I think, in the insert function.

    Any suggestions much appreciated.
    Last edited by BpB; 04-04-2017 at 01:37 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    According to this helpful page, array subscripting is of higher precedence than dereference. What this means is that in insert() you try to access 4 different array pointers while you have only one.

    Your compiler should have warned you about this error if you had warnings enabled.
    ( Or rather, it would warn you about trying to assign an integer value to a pointer )
    Last edited by GReaper; 04-04-2017 at 02:01 PM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    My immediate first thought from your reply was

    Code:
    void insert(test *t) {
        (*t)[0] = 200;
        (*t)[1] = 201;
        (*t)[2] = 202;
        (*t)[3] = 203;
    }
    That seems to have done it

    Thanks.

    edit: the compiler did not warn me. I was compiling with gcc -Wall test.c -o test , -Wall is warning all on right?
    gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.9.00)
    Last edited by BpB; 04-04-2017 at 02:16 PM.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    There won't be a warning since what you're attempting in the broken code is perfectly legal. Your fix is correct. And the malloc should use sizeof(**t). (That's kind of hidden by having the * in the typedef.)

    BTW, full warnings require -Wall -W -pedantic (-W is short for -Wextra)

  5. #5
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    Right, got ya. Thanks for pointing out sizeof(**t).

    Just as a matter of interest, is there a pointer arithmetic version of (*t)[2] = 202; rather than indexes? Just curious. Just tried a few guesses and none worked.

    Thanks.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    There's always a pointer arithmetic version of indexing since indexing is just "syntactic sugar" for pointer arithmetic.
    a[i] means *(a + i)
    So (*t)[2] would be *((*t) + 2) or just *(*t + 2)

  7. #7
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    I see. Thanks very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers to functions
    By lmanukyan in forum C Programming
    Replies: 6
    Last Post: 02-01-2016, 03:42 AM
  2. array of pointers to functions
    By lmanukyan in forum C Programming
    Replies: 3
    Last Post: 01-27-2016, 07:47 AM
  3. Array of Pointers to Functions in Structs...
    By yaya in forum C++ Programming
    Replies: 10
    Last Post: 12-21-2008, 06:14 PM
  4. array of pointers to functions
    By moowy in forum C++ Programming
    Replies: 8
    Last Post: 10-20-2006, 06:50 AM
  5. Sending array's to functions by reference or pointers
    By homeyg in forum C++ Programming
    Replies: 16
    Last Post: 12-27-2004, 02:55 PM

Tags for this Thread