Thread: unknown fault

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    22

    unknown fault

    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>

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    2
    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.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    22

    unknown fault with code

    This is part of my code where I guess that the problem is here:
    **************************************************
    Code:
    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);
    }
    Tagged by Salem

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    22
    I'm sorry that I don't understand your reply

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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;
    }

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    22
    There is no 'Seg fault.' printed out.
    What does it indicate?
    How to test the return value of malloc?

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    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}
    Code:
    prob.y = Malloc(double,prob.l);
    if (prob.y == NULL)
    {
      fprintf(stderr,"failed to allocate memory prob.y\n");
      exit(1);
    }
    Also, are you sure you are using Malloc() correctly? Normal usage of the standard function would be:
    Code:
    prob.y = malloc(sizeof(double) * prob.l);
    /* followed by error checking... */
    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...

    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
    -----------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unknown seg fault after malloc
    By seaking1 in forum C Programming
    Replies: 4
    Last Post: 02-25-2009, 07:51 PM
  2. unknown seg fault reason.
    By egoveneror2 in forum C Programming
    Replies: 16
    Last Post: 04-15-2008, 02:30 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM