454,446 Members | 1,317 Online Need help? Post your question and get tips & solutions from a community of 454,446 IT Pros & Developers. It's quick & easy.

# Segmentation Fault

 P: 3 Let me state my problem. This is my main.c Expand|Select|Wrap|Line Numbers   #include  #include "includes/first_order_local_matrix.h"   int main() {         float eps = 1;           struct vector *x, *y, *rho, *q;         struct matrix *P;           initialize_vector(x,3);         initialize_vector(y,3);         initialize_vector(rho,3);           x->values = 0;         x->values = 2;         x->values = 0;         y->values = 0;         y->values = 0;         y->values = 1;         rho->values = 3;         rho->values = 3;         rho->values = 3;           first_order_local_matrix(x,y,rho,eps,P,q);   }     and I have declared the structure vector as follows: Expand|Select|Wrap|Line Numbers   struct vector {         float *values;         int length; };     and the definition for the initialize_vector() function is as follows: Expand|Select|Wrap|Line Numbers   void initialize_vector(struct vector *vector_to_be_initialized, int length) {         printf("Initializing Vector\n");           vector_to_be_initialized -> length= length;         vector_to_be_initialized -> values = malloc(length * sizeof(float));           printf("Initialized Vector\n"); }      and finally this is the code for the first_order_local_matrix() function: Expand|Select|Wrap|Line Numbers   void first_order_local_matrix(struct vector *x, struct vector *y, struct vector *rho, float epsilon, struct matrix *P, struct vector *q) {           struct vector *b, *c;           float area;         int ii,jj,dim;           dim = x->length;           initialize_vector(b, dim);         initialize_vector(c, dim);         initialize_vector(q, dim);         initialize_matrix(P, dim,dim);           area = triangle(x,y,b,c);           for(ii=0; iivalues[ii][jj] = epsilon*area*(b->values[ii]*b->values[jj] + c->values[ii]*c->values[jj]);                   q->values[ii] = area*(2*rho->values[ii] + rho->values[mod(ii,3)+1] + rho->values[mod(mod(ii,3)+1,3)+1])/12;         } }     The problem is that the first 3 calls for the initialize_vector() from main() goes well. But when the initalize_vector() is called from first_order_local_matrix() I get a segmentation fault. Any clues on what I am doing wrong here? Thanks in advance. Apr 27 '07 #1
5 Replies

 Expert 100+ P: 181 Let me state my problem. This is my main.c Expand|Select|Wrap|Line Numbers   #include  #include "includes/first_order_local_matrix.h"   int main() {         float eps = 1;           struct vector *x, *y, *rho, *q;         struct matrix *P;           initialize_vector(x,3);         initialize_vector(y,3);         initialize_vector(rho,3);           x->values = 0;         x->values = 2;         x->values = 0;         y->values = 0;         y->values = 0;         y->values = 1;         rho->values = 3;         rho->values = 3;         rho->values = 3;           first_order_local_matrix(x,y,rho,eps,P,q);   }     and I have declared the structure vector as follows: Expand|Select|Wrap|Line Numbers   struct vector {         float *values;         int length; };     and the definition for the initialize_vector() function is as follows: Expand|Select|Wrap|Line Numbers   void initialize_vector(struct vector *vector_to_be_initialized, int length) {         printf("Initializing Vector\n");           vector_to_be_initialized -> length= length;         vector_to_be_initialized -> values = malloc(length * sizeof(float));           printf("Initialized Vector\n"); }      and finally this is the code for the first_order_local_matrix() function: Expand|Select|Wrap|Line Numbers   void first_order_local_matrix(struct vector *x, struct vector *y, struct vector *rho, float epsilon, struct matrix *P, struct vector *q) {           struct vector *b, *c;           float area;         int ii,jj,dim;           dim = x->length;           initialize_vector(b, dim);         initialize_vector(c, dim);         initialize_vector(q, dim);         initialize_matrix(P, dim,dim);           area = triangle(x,y,b,c);           for(ii=0; iivalues[ii][jj] = epsilon*area*(b->values[ii]*b->values[jj] + c->values[ii]*c->values[jj]);                   q->values[ii] = area*(2*rho->values[ii] + rho->values[mod(ii,3)+1] + rho->values[mod(mod(ii,3)+1,3)+1])/12;         } }     The problem is that the first 3 calls for the initialize_vector() from main() goes well. But when the initalize_vector() is called from first_order_local_matrix() I get a segmentation fault. Any clues on what I am doing wrong here? Thanks in advance. Fault 1: struct vector *x, *y, *rho, *q; struct matrix *P; initialize_vector(x,3); initialize_vector(y,3); initialize_vector(rho,3); your not intializing pointer here. use x = (struct vector *)malloc(sizeof(struct vector)); before using pointer x. this holds good for other pointers as well Fault 2: your are doing same mistake in function first_order_local_matrix() as well. i don't know why its not crashing at first place itself. :( if any one know do tell me. Apr 27 '07 #2

 P: 3 Thanks for the reply budy. I fixed the problem. I was in Java for quite sometime and these subtle things in C are screwing me up grandly [:D] I have one more question: You said I should use x = (struct vector *)malloc(sizeof(struct vector)); But should it not be x = (struct vector *)malloc(sizeof(struct vector *)) since x is a pointer. But both of them seem to work fine. So which one is technically correct? Plus why is it required to coerce it by typecasting as (struct vector *). Isn't it something redundant? Thanks in advance. Prathap Apr 27 '07 #3

 100+ P: 256 But should it not be x = (struct vector *)malloc(sizeof(struct vector *)) since x is a pointer. you want enough memory for your structure not enough memory for a pointer to your structure Plus why is it required to coerce it by typecasting as (struct vector *). Isn't it something redundant? malloc just returns a void* to your new memory (is just a chunk and says nothing abut how it is to be used/divided up). malloc knows nothing about what the memory is for, only how big the chunk is. You use the typecast to define how that memory is used (how its shaped or divided up) - your saying that the memory is memory for your vector structure. Apr 27 '07 #4

 P: 3 In that case, what is the differnce between Expand|Select|Wrap|Line Numbers x = (struct vector *)malloc(sizeof(struct vector *))   and Expand|Select|Wrap|Line Numbers x = (struct vector *)malloc(sizeof(struct vector))   i.e. under what circumstance would you want to allocate space corresponding to a pointer? Apr 27 '07 #5

 100+ P: 256 under what circumstance would you want to allocate space corresponding to a pointer? I can't think of a time when I malloc'd space for a pointer. There may be a situation where this is desired, I just can't think of one. If your still foggy on the subject, try this: Expand|Select|Wrap|Line Numbers printf( "sizeof struct vector: %d\n", sizeof( struct vector)); printf( "sizeof struct vector *: %d\n", sizeof( struct vector*)); The pointer is only large enough to hold an address. The structure is large enough to hold what it's made up of (an int, and a pointer for floats). May 3 '07 #6 