I'm running my C program on Linux. An error message is come out:
Segmentation fault
Can anyone help me to suggest any possible reasons which cause the error>
I'm running my C program on Linux. An error message is come out:
Segmentation fault
Can anyone help me to suggest any possible reasons which cause the error>
this error is reported when u are reading acroos the boundary of memory allocated. See if u are accessing past any array or any other memory allocation u have made.
This is part of my code where I guess that the problem is here:
**************************************************
Tagged by SalemCode:void read_problem(char *filename) { int elements, max_index, i, j, index, ips, ops; vec_t **ip_x; vec_t **op_x; PATTERN *pat; unsigned long num_patfiles, total_num_pats; FILE *f = fopen(filename,"r"); if(f == NULL) { fprintf(stderr,"can't open input file %s\n",filename); exit(1); } pat = (PATTERN *) get_pattern(filename, &num_patfiles,&total_num_pats); ip_x = (vec_t**)extract_ip_patterns(pat, num_patfiles, total_num_pats); op_x = (vec_t**)extract_op_patterns(pat, num_patfiles, total_num_pats); prob.l = (int)total_num_pats; elements = (int)pat[1].num_ips*total_num_pats; prob.y = Malloc(double,prob.l); prob.x = Malloc(struct svm_node *,prob.l); x_space = Malloc(struct svm_node,elements); max_index = 0; j=0; ips=0; ops=0; ips=pat[1].num_ips; ops=pat[1].num_ops; for(i=0;i<prob.l;i++) { prob.x[i] = &x_space[j]; prob.y[i] =(double)op_x[i+1][1]; for (index=1;index<=ips;index++) { x_space[j].index=index; x_space[j].value=ip_x[i+1][index]; ++j; } if(j>=1 && x_space[j-1].index > max_index) max_index = x_space[j-1].index; x_space[j++].index = -1; } if(param.gamma == 0) param.gamma = 1.0/max_index; for (i=0;i<j;i++) printf("label[%i]= %lf index[%i]= %d value[%i]= %lf\n",i,prob.y[i],i,x_space[i].index,i,x_space[i].value); fclose(f); }
I'm sorry that I don't understand your reply
Hard to say. Perhaps you are manipulating NULL pointers?
maybe try:
Code:pat = (PATTERN *) get_pattern(filename, &num_patfiles,&total_num_pats); ip_x = (vec_t**)extract_ip_patterns(pat, num_patfiles, total_num_pats); op_x = (vec_t**)extract_op_patterns(pat, num_patfiles, total_num_pats); if(!pat || !ip_x || !op_x) printf("Seg fault.");
Also, you should *always* test the return value of malloc.
More Code tagged by Salem
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
There is no 'Seg fault.' printed out.
What does it indicate?
How to test the return value of malloc?
You can test malloc() in much the same way that you test the return from fopen() - {which by the way is superfluous in your code cos you never use f as far as I can see}
Also, are you sure you are using Malloc() correctly? Normal usage of the standard function would be:Code:prob.y = Malloc(double,prob.l); if (prob.y == NULL) { fprintf(stderr,"failed to allocate memory prob.y\n"); exit(1); }
I also note that you don't free() any of the malloc()ed memory. You must do this. What about memory allocated by the functions: get_pattern(), extract_ip_patterns(), extract_op_patterns()? I assume that they also allocate memory...Code:prob.y = malloc(sizeof(double) * prob.l); /* followed by error checking... */
p.s. Salem was trying to tell you to use code tags, which would really help in trying to understand what your code is doing.
DavT
-----------------------------------------------