Thread: Stuck in segmentation fault(core dumped).Could anyone help me?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Harrison, New Jersey, United States
    Posts
    9

    Post Stuck in segmentation fault(core dumped).Could anyone help me?

    My program are divided into 4 files.My object is to calculate betas of a series of stocks.
    The stats program,which contains 6 calculation functions:
    Code:
    #include <math.h>
    static double sum(double *p,int length){
    double sum=0;
    int i=0;
    for(;i<length;i++)
    sum=sum+*(p++);
    return sum;
    }
    static double avg(double *p,int length){
    double sum=0;
    int i=0;
    double avg;
    for(;i<length;i++)
    sum=sum+*(p++);
    avg=sum/length;
    return avg;
    }
    static double sumSq(double *p,int length){
    double sumSq=0;
    int i=0;
    for(;i<length;i++)
    sumSq=sumSq+pow(*(p++),2);
    return sumSq;
    }
    static double sumxy(double *p1,double *p2,int length){
    double sumxy=0;
    int i=0;
    for(;i<length;i++)
    sumxy=sumxy+*(p1++)**(p2++);
    return sumxy;
    }
    void regression(double *x,double *y,int length,double *beta,double *intercept,double *RSquare){
    *beta=(length*sumxy(x,y,length)-sum(x,length)*sum(y,length))/(length*sumSq(x,length)-sum(x,length)*sum(x,length));
    *intercept=avg(y,length)-*beta*avg(x,length);
    double *x1,*y1; //Build two arrays:x-avg(x),y-avg(y) to calculate R
    x1=(double *)malloc(length*sizeof(double));
    y1=(double *)malloc(length*sizeof(double));
    int i=0;
    for(;i<length;i++){
    x1[i]=x[i]-avg(x,length);
    y1[i]=y[i]-avg(y,length);
    }
    *RSquare=sumxy(x1,y1,length)*sumxy(x1,y1,length)/(sumSq(x1,length)*sumSq(y1,length));
    }
    Next is a file defines a function to get prices data of stocks:

    Code:
    #include <stdio.h>
    #include "stock.h"
    #include <string.h>
    #include <stdlib.h>
    stock *getData(int *size){ //Size refers to the number of stocks.
    stock *stockarray=(stock*)malloc(*size*sizeof(stock));
    int i=0;
    int j=0;
    for(;i<*size;i++){
    scanf("%s %d",stockarray[i].name,&stockarray[i].length); //Read the name and the number of closing prices.
    stockarray[i].price=(double *)malloc(stockarray[i].length*sizeof(double));
    stockarray[i].retu=(double *)malloc(stockarray[i].length*sizeof(double));
    for(;j<stockarray[i].length;j++){
    scanf("%f",&stockarray[i].price[j]);
    printf("price%f",stockarray[i].price[j]);
    }
    }
    return stockarray;
    }
    And the following is the main function and two other functions:
    Code:
    #include <stdio.h>
    #include "stock.h"
    #include <stdlib.h>
    void setReturn(stock stock);
    void printStock(stock mystock,int printPrice,int printReturns);
    stock *getData(int *size);
    void regression(double *x,double *y,int length,double *slope,double *intercept,double *RSquare);
    
    int main(){
    int *stockno; //Define a pointer pointing to an interger denoting the number of stocks.
    int size;
    scanf("%d",&size);
    stockno=&size;
    printf("%d",*stockno);
    stock* stockarray;
    stockarray=getData(stockno);
    int i=0;
    for(;i<*stockno;i++){
    setReturn(stockarray[i]);
    } //Set return array in every structure
    int j=0;
    for(;j<*stockno-1;j++){
    regression(stockarray[i].retu,stockarray[*stockno-1].retu,stockarray[i].length,&stockarray[i].beta,&stockarray[i].intercept,&stockarray[i].RSquare);
    printStock(stockarray[i],0,0);
    }
    return 0;
    }
    void setReturn(stock stock){ //The argument is a pointer to a structure.
    int i=0;
    for(;i<stock.length-1;i++){
    *stock.retu=(*(stock.price+1)-(*stock.price))/(*stock.price);
    printf("return1=%f",stock.price[i]);
    stock.retu++;
    stock.price++;
    }
    }
    void printStock(stock mystock,int printPrice,int printReturns){
    int i=0;
    printf("The Stock Name\t\tBeta\t\tR-Squared\t\tIntercept\n");
    printf("%s%\t\t%f\t\t%f\t\t%f\n",mystock.name,mystock.beta,mystock.RSquare,mystock.intercept);
    if (printPrice==1&mystock.price[0]!='\0'){
    printf("The stock price series:\n");
    for(;i<mystock.length;i++)
    printf("%f\n",mystock.price[i]);
    }
    if (printReturns==1&mystock.retu[0]!='\0'){
    printf("The return series:\n");
    for(;i<mystock.length-1;i++)
    printf("%f%%\n",100*mystock.retu[i]);
    }
    }
    Finally the header file:
    Code:
    #ifndef STOCK_H_
    #define STOCK_H_
    
    
    #endif /* STOCK_H_ */
    typedef struct stock{
    char *name;
    int length;
    double beta;
    double RSquare;
    double intercept;
    double *price; //define the array of closing price
    double *retu; //define the array of return
    } stock;
    I have been working on debuging for a whole day but never figured out what is wrong with my program.Hope somebody could help me.Thank you very much!

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    << !! Posting Code? Read this First !! >>
    More specifically, the code is completely devoid of any indentation whatsoever. Until that is fixed, you'll be very lucky if anyone takes a look at the problem.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    As stated so eloquently here, seg faults are not particularly difficult to diagnose if you know how to use a debugger, which you should learn to do if you don't already.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Harrison, New Jersey, United States
    Posts
    9
    Sorry as a freshman in programming,I was not aware of the lack of indentation.I have corrected this problem.
    Code:
    #include <math.h> static double sum(double *p,int length){
          double sum=0;
          int i=0;
          for(;i<length;i++)
                sum=sum+*(p++);
          return sum;
    }
    static double avg(double *p,int length){
          double sum=0;
          int i=0;
          double avg;
          for(;i<length;i++)
               sum=sum+*(p++);
          avg=sum/length;
          return avg;
    }
    static double sumSq(double *p,int length){
          double sumSq=0;
          int i=0;
          for(;i<length;i++)
               sumSq=sumSq+pow(*(p++),2);
          return sumSq;
    }
    static double sumxy(double *p1,double *p2,int length){
         double sumxy=0;
         int i=0;
         for(;i<length;i++)
              sumxy=sumxy+*(p1++)**(p2++);
         return sumxy;
    }
    void regression(double *x,double *y,int length,double *beta,double *intercept,double *RSquare){
         *beta=(length*sumxy(x,y,length)-sum(x,length)*sum(y,length))/(length*sumSq(x,length)-sum(x,length)*sum(x,length));
         *intercept=avg(y,length)-*beta*avg(x,length);
         double *x1,*y1;                            //Build two arrays:x-avg(x),y-avg(y) to calculate R
         x1=(double *)malloc(length*sizeof(double));
         y1=(double *)malloc(length*sizeof(double));
         int i=0;
         for(;i<length;i++){
              x1[i]=x[i]-avg(x,length);
              y1[i]=y[i]-avg(y,length);
         }
        *RSquare=sumxy(x1,y1,length)*sumxy(x1,y1,length)/(sumSq(x1,length)*sumSq(y1,length));
    }
    Next is a file defines a function to get prices data of stocks:
    Code:
    #include <stdio.h>
    #include "stock.h"
    #include <string.h>
    #include <stdlib.h>
    stock *getData(int *size){                           //Size refers to the number of stocks.
          stock *stockarray=(stock*)malloc(*size*sizeof(stock));
          int i=0;
          int j=0;
          for(;i<*size;i++){
               scanf("%s %d",stockarray[i].name,&stockarray[i].length);    //Read the name and the number of closing prices.
               stockarray[i].price=(double *)malloc(stockarray[i].length*sizeof(double));
               stockarray[i].retu=(double *)malloc(stockarray[i].length*sizeof(double));
               for(;j<stockarray[i].length;j++){
                    scanf("%f",&stockarray[i].price[j]);
               printf("price%f",stockarray[i].price[j]);
               }
          }
        return stockarray;
    }
    And the following is the main function and two other functions:
    Code:
    #include <stdio.h>
    #include "stock.h"
    #include <stdlib.h>
    void setReturn(stock stock);
    void printStock(stock mystock,int printPrice,int printReturns);
    stock *getData(int *size);
    void regression(double *x,double *y,int length,double *slope,double *intercept,double *RSquare);
     
    int main(){
         int *stockno;                            //Define a pointer pointing to an interger denoting the number of stocks.
         int size;
         scanf("%d",&size);
         stockno=&size;
         printf("%d",*stockno);
         stock* stockarray;
         stockarray=getData(stockno);
         int i=0;
         for(;i<*stockno;i++){
              setReturn(stockarray[i]);
         }                                        //Set return array in every structure
         int j=0;
         for(;j<*stockno-1;j++){
              regression(stockarray[i].retu,stockarray[*stockno-1].retu,stockarray[i].length,&stockarray[i].beta,&stockarray[i].intercept,&stockarray[i].RSquare);
              printStock(stockarray[i],0,0);
         }
         return 0;
    }
    void setReturn(stock stock){               //The argument is a pointer to a structure.
        int i=0;
        for(;i<stock.length-1;i++){
             *stock.retu=(*(stock.price+1)-(*stock.price))/(*stock.price);
             printf("return1=%f",stock.price[i]);
             stock.retu++;
             stock.price++;
         }
    }
    void printStock(stock mystock,int printPrice,int printReturns){
          int i=0;
          printf("The Stock Name\t\tBeta\t\tR-Squared\t\tIntercept\n");
          printf("%s%\t\t%f\t\t%f\t\t%f\n",mystock.name,mystock.beta,mystock.RSquare,mystock.intercept);
          if (printPrice==1&mystock.price[0]!='\0'){
              printf("The stock price series:\n");
              for(;i<mystock.length;i++)
                   printf("%f\n",mystock.price[i]);
          }
          if (printReturns==1&mystock.retu[0]!='\0'){
              printf("The return series:\n");
              for(;i<mystock.length-1;i++)
                   printf("%f%%\n",100*mystock.retu[i]);
          }
    }
    Finally the header file:
    Code:
    #ifndef STOCK_H_
    #define STOCK_H_
     
    
    #endif /* STOCK_H_ */
    typedef struct stock{
          char *name;
          int length;
          double beta;
          double RSquare;
          double intercept;
          double *price;                       //define the array of closing price
          double *retu;                     //define the array of return
    } stock;

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Harrison, New Jersey, United States
    Posts
    9
    I am a freshman in programming.I searched online and obtain several methods to debug,like gdb(I am using cygwin).Nonetheless none of them work.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    stock.retu++;
    stock.price++;
    You're mucking about with the pointers to your arrays.
    When you come to either use these arrays again, or free them, you're up the creek without the paddle.

    Use array subscripting.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    That looks beautiful now, great job on the indentation there.
    Yep, I agree with Salem's diagnosis and suggested fix.

    This line and another similar also look a little suspect.
    Code:
          if (printPrice==1&mystock.price[0]!='\0'){
    I expect that you mean to use && rather than &, and I suggest surrounding it by some whitespace.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Harrison, New Jersey, United States
    Posts
    9

    Post

    Thanks to iMalc!The corrected code:
    Code:
    if ( (printprice==1) && (mystock.price[0]!=0) ){
    In stock.h
    I should specify the size of the char array when initializing it.
    Code:
    char *name[20];
    Now I have eliminated the segmentation fault,but the program still fail to work out as my intention.The displayed results are a series of "nan".I don't know how come.

    Salem,I have no idea about what you mean by "paddle".Sorry,I am a freshman in programming.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by cxwcxw2001 View Post
    In stock.h
    I should specify the size of the char array when initializing it.
    Code:
    char *name[20];
    Umm, no, because now you have an array of 20 uninitialized char *s, not a C-string. Given the complexity of this program you should know how a C-string is declared.

    Salem,I have no idea about what you mean by "paddle".Sorry,I am a freshman in programming.
    It's got nothing to do with programming...you've never heard the phrase "Up the creek without a paddle?" It means you're in deep trouble.

    I am a freshman in programming.I searched online and obtain several methods to debug,like gdb(I am using cygwin).Nonetheless none of them work.
    They work, you probably just don't want to put the time into learning to use gdb. http://www.unknownroad.com/rtfm/gdbtut/gdbtoc.html

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Yo dudes, I heard of this thing called "the Internet" where you can look up all sorts of weird stuff.
    up the creek - Wiktionary
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Location
    Harrison, New Jersey, United States
    Posts
    9
    Quote Originally Posted by rags_to_riches View Post
    Umm, no, because now you have an array of 20 uninitialized char *s, not a C-string. Given the complexity of this program you should know how a C-string is declared.


    Yep!I should delete "*" before name[20].However all I get is still a series of "nan".Oops.



    Quote Originally Posted by rags_to_riches View Post
    They work, you probably just don't want to put the time into learning to use gdb. RMS's gdb Tutorial
    Hmm,it is a good reference tutorial.Thanks !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 07:50 AM
  2. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 01:18 PM
  3. Replies: 7
    Last Post: 03-17-2011, 10:55 AM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM