Thread: Using pointer defined in a header function

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    2

    Question Using pointer defined in a header function

    Hi there,

    I have two files, func2.h and main.c. In func2.h I have a function, network_construction, where I dynamically allocate and assign values to a pointer, veins, which pretends to be a two dimensional matrix with rows of different length. In main.c there is the declaration of this pointer.

    I am finding problems in dealing with the allocation of memory and filling the values only inside the function network_construction. I want to use veins in main.c for later calculations, but before calling the function in main.c ,it shouldn't appear any reference to veins, only the declaration. The files are below, but so far I have been obtaining Segmentation faults. How do I overcome these errors ? What am I doing wrong ?

    main.c:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "func2.h"
    
    void main(){
        
        int num_veins[NN];
        int **veins;
        
        network_construction(&veins, num_veins);
        printf("\n%d\n", veins[2][2]);
    
        for (i = 0; i < NN; i++) { 
            free(veins[i]);
        }
        free(veins);
    
    
    return;
    }
    func2.h
    Code:
    #define NN 20
    
    
    void network_construction(int ***veins, int *num_veins){
    
            int i, j;
    
    
            for(i=0;i<NN;i++){
                num_veins[i] = NN/2;
            }
    
    
            *veins = malloc(NN * sizeof(*veins));
            for (i = 0; i < NN; i++) { 
                *veins[i] = malloc(num_veins[i] * sizeof(**(veins[i]))); 
            }
                
            for (i = 0; i < NN; i++) { for (j = 0; j<num_veins[i];j++) { *veins[i][j] = -1; } }
            
        return;
        }
    Any solution would be really appreciated. Thank you in advance !

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well the first thing I see wrong is:

    Code:
    void main()
    The function main() should be defined to return an int, and you should return an int from this function.

    The second thing I see wrong is executable code inside a header file. Header files should not contain function implementations, these implementations should be in source files (.c).

    The third thing I see wrong is that in main i is not defined.

    And finally the segmentation fault appears to be happening in your malloc() calls.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    2
    Hi Jim,

    thank you for your quick answer.
    So, I used the changes you have proposed and the files are now

    main.c:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "func2.c"
    
    
    
    
    int main(){
        
        int num_veins[NN];
        int **veins;
        int i;
        
        
        network_construction(&veins, num_veins);
        printf("\n%d\n", veins[2][2]);
    
        for (i = 0; i < NN; i++) { 
            free(veins[i]);
        }
        free(veins);    
    
    
    return 0;
    }
    and func2.c:
    Code:
    #define NN 20
    
    
    void network_construction(int ***veins, int *num_veins){
    
            int i, j;
    
    
            for(i=0;i<NN;i++){
                num_veins[i] = NN/2;
            }
    
    
            *veins = malloc(NN * sizeof(*veins));
            for (i = 0; i < NN; i++) { 
                *veins[i] = malloc(num_veins[i] * sizeof(**(veins[i]))); 
            }
                
            for (i = 0; i < NN; i++) { for (j = 0; j<num_veins[i];j++) { *veins[i][j] = -1; } }
    
        return;
        }
    I build the codes reading stuff and taking examples on the Internet, my knowledge about pointers is not very broad, so I do not know exactly what is going on. You tell me that Segmentation is due to malloc call, but if I comment those lines in func2.c, and main.c only calls the function, the error is still there, so my guess is that I'm doing something wrong in the definition of network_construction, but I do not what.

    What can I do ?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    ou tell me that Segmentation is due to malloc call, but if I comment those lines in func2.c, and main.c only calls the function, the error is still there,
    Did you also comment out the free calls in main?

    The first thing to do is to check that malloc() didn't return NULL from one of the calls. And because you're working with an "extra" level of indirection in the function I would start by moving the code to main(), once you get the code working in main() it'll be easier to move it to the function.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 35
    Last Post: 12-01-2011, 08:31 PM
  2. Replies: 2
    Last Post: 12-06-2010, 01:28 PM
  3. User defined Header
    By $l4xklynx in forum C++ Programming
    Replies: 33
    Last Post: 12-01-2008, 06:09 PM
  4. user defined header files
    By sidu in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2008, 06:40 AM
  5. Replies: 14
    Last Post: 03-02-2008, 01:27 PM

Tags for this Thread