Thread: Debugging help :(

  1. #16
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Don;t think my idea are gonna help somehow I tried it and got compilation errors

  2. #17
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    OK so I change it back apart from he argv thing and it seems to work.

  3. #18
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    OK I compiled it and it ran OK.

  4. #19
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    This code works
    I moved the malloc inside the loop
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
     
     
    typedef double real;
     
     
    typedef struct
    {
        real xpos;
        real ypos;
        real zpos;
         
        real uvel;
        real vvel;
        real wvel;
         
        real omegax;
        real omegay;
        real omegaz;
         
        real radius;
        real mass;
         
    } sphere;
     
     
    void update_position(sphere *ball, real dt)
    {
       //(*ball).xpos
       ball->xpos=ball->xpos + dt*ball->vvel;
       //ypos,zpos  
    }
     
     
    void detect_ball(sphere *ball1, sphere *ball2, real *pdist)
    {
         
        *pdist=sqrt((ball2->xpos-ball1->xpos)*(ball2->xpos-ball1->xpos)+
                   (ball2->ypos-ball1->ypos)*(ball2->ypos-ball1->ypos));
                    
    //        if ( dist<= (ball1->radius + ball2->radius) +0.0075 )
    //        {
    //            if ( dist>= ball1->radius + ball2->radius -0.0075   )
    //            {  
    //            *ptime=0;
    //            }
    //        }
     
     
    }
     
     
    int main(int argc, char *argv)
    {
        real dt=0.01;
        real t, dist1, tmax=1, mlimit, firstvvel, gap=1.5,  dist[2], ballmatxpos[101][2];
        int nballs=1 ;
        int i,j,m,k;
        sphere *ball;
          
      
        mlimit=tmax/dt;
        //initialise positions
        for(i=0;i<=nballs;i++)
        {
     
    ball=(sphere *)malloc(nballs*sizeof(sphere));
           ball[i].radius=0.5;
             ball[i].ypos=0.0;  
           ball[i].ypos=0.0; 
            
           ball[i].uvel=1.0;
           ball[0].vvel=1.0;
           ball[1].vvel=0.0;
        }
         
        //pendulum to be released
       // ball[0].xpos=0;
        //ball[0].ypos=0;
      //  firstvvel=0;
         
        //timestep loop
        t=0.0;
        dist1=0.0;
        while(t<tmax)
        {
            for(m=0;m<=mlimit;m++)
           {
           //update position
            for(i=0;i<=nballs;i++)
            {
                  update_position(&(ball[i]), dt); 
            }
           //collision detection
            for(i=0;i<=nballs;i++)
            {
                for(j=0;j<=nballs;j++)
                {
                    detect_ball(&(ball[i]),&(ball[j]), &dist1);     
                                  
                  if ( dist1 <= (ball[i].radius + ball[j].radius) +0.075 )
                    {
                       if ( dist1 >= (ball[i].radius + ball[j].radius) -0.075   )
                        {
                 //  ball2->uvel=ball1->vvel;
                    //yes collision, modify velocities and rotation rates  
                        t=0;
                         }
                    }
                }
            }
             
            ballmatxpos[m][0]=t;
            ballmatxpos[m][1]=ball[0].xpos;
     
     
       printf("In this case, initial velocity= %3.4lf with t=%3.4lf \n", ballmatxpos[m][0], t );  
                                    
            t+=dt;   
            }
        }
         
     
     
    //    ---------------//-------------------------
          FILE *pFile1;
             FILE *pFile2;
             pFile1 = fopen("graphing3.txt", "w");
             pFile2 = fopen("graphing4.txt", "w");
             if (pFile1 != NULL)
            {
                 for(m=0;m<=mlimit;m++)
                 {
             fprintf( pFile1, " %3.4lf \n ", ballmatxpos[m][0]);
                  }
          fprintf( pFile1,"\n");
          fclose(pFile1);
            }
          if (pFile2 != NULL)
            {
                 for(m=0;m<=mlimit;m++)
                 {
             fprintf( pFile2, " %3.4lf  \n", ballmatxpos[m][1]);
             }
           fprintf( pFile2,"\n");
          fclose(pFile2);     
           } 
                     else
            {
                printf("inda dapat print bro");
           }
     
     
     
     
     
     
    //  ----------------------------------------------------- 
     
     
     
     
         
        getch();
        return 0;
    }

  5. #20
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by esbo View Post
    This code works
    I moved the malloc inside the loop
    Stop hijacking/spamming this thread with one sentence replies. Your code doesn't work, or at least, you introduced a big memory leak by doing so. As I said, the declaration and malloc call were fine, just the loop did one too many iterations.

  6. #21
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    This line causes a problem I accidental change it from

    Code:
    ball[i].xpos=(real) i * (2*ball[i].radius + gap);
    to

    Code:
    ball[i].ypos=0.0;
    OK I said y not x but the issue is in the line I mention so it seems.

  7. #22
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by anduril462 View Post
    Stop hijacking/spamming this thread with one sentence replies. Your code doesn't work, or at least, you introduced a big memory leak by doing so. As I said, the declaration and malloc call were fine, just the loop did one too many iterations.
    The code I posted runs OK, if providing a solution or a partial solution or something helpful counts a 'hijacking then I guess I am guilty.

  8. #23
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The code I posted runs OK
    I ran it and got a segmentation fault.

    Furthermore, the spamming comment was in reference to posting multiple times in a row, all within a few minutes of each other. We're already halfway down the second page of this thread, and it shouldn't be that long. There's no reason you couldn't have added further comments to your previous posts with an edit.

  9. #24
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Matticus View Post
    I ran it and got a segmentation fault.

    Furthermore, the spamming comment was in reference to posting multiple times in a row, all within a few minutes of each other. We're already halfway down the second page of this thread, and it shouldn't be that long. There's no reason you couldn't have added further comments to your previous posts with an edit.

    Thanks for you opinion but I disagree with all of it,.

    My opinion is that if you have nothing more constructive to do than criticise people who are trying to help them the most helpful thing you could do would be to shut

    If you have nothing constructive to say say nothing.

    I post in my style not yours, you don't own me.

    If you don't like my posts there is a simple solution do not read them. You have free will don't you?

    You mind is not so inflexible that such petty matters bother you surely?
    Last edited by esbo; 01-11-2013 at 04:21 PM.

  10. #25
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    For anyone who wants a comedy read, just search for esbo's past posts.
    esbo has been here on and off for a long while, but seems to have learnt sweet FA about programming in that time.
    Best regarded as a troll IMO.
    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. #26
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Actually, it was earlier today I stumbled upon the famous "pointer" thread by said poster. I was trying to find it again, but to no avail.

  12. #27
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
      
      
    typedef double real;
      
      
    typedef struct
    {
        real xpos;
        real ypos;
        real zpos;
          
        real uvel;
        real vvel;
        real wvel;
          
        real omegax;
        real omegay;
        real omegaz;
          
        real radius;
        real mass;
          
    } sphere;
      
      
    void update_position(sphere *ball, real dt)
    {
       //(*ball).xpos
       ball->xpos=ball->xpos + dt*ball->vvel;
       //ypos,zpos 
    }
      
      
    void detect_ball(sphere *ball1, sphere *ball2, real *pdist)
    {
          
        *pdist=sqrt((ball2->xpos-ball1->xpos)*(ball2->xpos-ball1->xpos)+
                   (ball2->ypos-ball1->ypos)*(ball2->ypos-ball1->ypos));
                     
    //        if ( dist<= (ball1->radius + ball2->radius) +0.0075 )
    //        {
    //            if ( dist>= ball1->radius + ball2->radius -0.0075   )
    //            { 
    //            *ptime=0;
    //            }
    //        }
      
      
    }
      
      
    int main(int argc, char *argv)
    {
        real dt=0.01;
        real t, dist1, tmax=1, mlimit, firstvvel, gap=1.5,  dist[2], ballmatxpos[101][2];
        int nballs=1 ;
        int i,j,m,k;
        sphere *ball;
           
       
        mlimit=tmax/dt;
        //initialise positions
        for(i=0;i<=nballs;i++)
        {
      
    ball=(sphere *)malloc(nballs*sizeof(sphere));
           ball[i].radius=0.5;
    //         ball[i].ypos=0.0; 
      //      ball[i].xpos=(real) i * (2*ball[i].radius + gap);
    
    //		ball[i].xpos=(real) i * (2*ball[i].radius + gap);
          ball[i].xpos=0.0;
           ball[i].ypos=0.0;
             
    
    
    
           ball[i].uvel=1.0;
           ball[0].vvel=1.0;
           ball[1].vvel=0.0;
        }
          
        //pendulum to be released
       // ball[0].xpos=0;
        //ball[0].ypos=0;
      //  firstvvel=0;
          
        //timestep loop
        t=0.0;
        dist1=0.0;
        while(t<tmax)
        {
            for(m=0;m<=mlimit;m++)
           {
           //update position
            for(i=0;i<=nballs;i++)
            {
                  update_position(&(ball[i]), dt);
            }
           //collision detection
            for(i=0;i<=nballs;i++)
            {
                for(j=0;j<=nballs;j++)
                {
                    detect_ball(&(ball[i]),&(ball[j]), &dist1);    
                                   
                  if ( dist1 <= (ball[i].radius + ball[j].radius) +0.075 )
                    {
                       if ( dist1 >= (ball[i].radius + ball[j].radius) -0.075   )
                        {
                 //  ball2->uvel=ball1->vvel;
                    //yes collision, modify velocities and rotation rates 
                        t=0;
                         }
                    }
                }
            }
              
            ballmatxpos[m][0]=t;
            ballmatxpos[m][1]=ball[0].xpos;
      
      
       printf("In this case, initial velocity= %3.4lf with t=%3.4lf \n", ballmatxpos[m][0], t ); 
                                     
            t+=dt;  
            }
        }
          
      
      
    //    ---------------//-------------------------
          FILE *pFile1;
             FILE *pFile2;
             pFile1 = fopen("graphing3.txt", "w");
             pFile2 = fopen("graphing4.txt", "w");
             if (pFile1 != NULL)
            {
                 for(m=0;m<=mlimit;m++)
                 {
             fprintf( pFile1, " %3.4lf \n ", ballmatxpos[m][0]);
                  }
          fprintf( pFile1,"\n");
          fclose(pFile1);
            }
          if (pFile2 != NULL)
            {
                 for(m=0;m<=mlimit;m++)
                 {
             fprintf( pFile2, " %3.4lf  \n", ballmatxpos[m][1]);
             }
           fprintf( pFile2,"\n");
          fclose(pFile2);    
           }
                     else
            {
                printf("inda dapat print bro");
           }
      
      
      
      
      
      
    //  -----------------------------------------------------
      
      
      
      
          
        getch();
        return 0;
    }
    The above code works I changed this line

    Code:
    //		ball[i].xpos=(real) i * (2*ball[i].radius + gap);
          ball[i].xpos=0.0;
    Worked on my machine anyway so an issue with that line in some way.


    It produced two files with data in anyway
    graphing4.txt
    Code:
     NaN  
     NaN  
     NaN  
     NaN  
     NaN  
     NaN  
     NaN  
     NaN  
     NaN  
     NaN  
     NaN  
     NaN  
     NaN

    graphing3.txt

    Code:
    0.0000 
      0.0100 
      0.0200 
      0.0300 
      0.0400 
      0.0500 
      0.0600 
      0.0700 
      0.0800 
      0.0900 
      0.1000 
      0.1100 
      0.1200 
      0.1300 
      0.1400 
      0.1500 
      0.1600 
      0.1700 
      0.1800 
      0.1900 
      0.2000 
      0.2100 
      0.2200 
      0.2300 
      0.2400 
      0.2500 
      0.2600 
      0.2700 
      0.2800 
      0.2900 
      0.3000 
      0.3100 
      0.3200 
      0.3300 
      0.3400 
      0.3500

  13. #28
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Salem View Post
    For anyone who wants a comedy read, just search for esbo's past posts.
    esbo has been here on and off for a long while, but seems to have learnt sweet FA about programming in that time.
    Best regarded as a troll IMO.
    I regard you as a one letter less.

    I think people are quite capable of forming their own opinions, when someone wants your opinion on me
    they are well capable of asking for it.

    They didn't take a hint from that.
    Last edited by esbo; 01-11-2013 at 04:32 PM.

  14. #29
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by esbo View Post
    If you have nothing constructive to say say nothing.
    Then how do you explain your post #14? Nothing constructive there, so why say it? For that matter, posts 11-14 and 16-18 are basically you just thinking "out loud" on the forum, and post #11 is more likely to confuse somebody than help, with you just throwing around random syntax. Take the time to gather your thoughts, try out your ideas and make one meaningful post. All those little, one sentence, non-constructive posts just help derail the thread faster. That may be a matter of opinion, but I would wager a fair sum of money that it is an opinion agreed upon by the majority of the forum regulars.

    Furthermore, Matticus' seg fault statement, at least, is not an opinion, but a pure fact. It is also a sign that your code definitely does not work. Again, test your code thoroughly before

    We're not trying to be a-holes about this, we're just trying to keep the thread more on track of the OP's actual problem, instead of a thread for you to write your thoughts out as they happen.

    Yes, this post is itself off-topic, but hopefully explains where we are coming from enough that we can all just drop this side-issue and return to helping people with programming, hopefully in a more productive manner.
    Last edited by anduril462; 01-11-2013 at 04:37 PM.

  15. #30
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by anduril462 View Post
    Then how do you explain your post #14? Nothing constructive there, so why say it? For that matter, posts 11-14 and 16-18 are basically you just thinking "out loud" on the forum, and post #11 is more likely to confuse somebody than help, with you just throwing around random syntax. Take the time to gather your thoughts, try out your ideas and make one meaningful post. All those little, one sentence, non-constructive posts just help derail the thread faster. That may be a matter of opinion, but I would wager a fair sum of money that it is an opinion agreed upon by the majority of the forum regulars.

    Furthermore, Matticus' seg fault statement, at least, is not an opinion, but a pure fact. It is also a sign that your code definitely does not work. Again, test your code thoroughly before

    We're not trying to be a-holes about this, we're just trying to keep the thread more on track of the OP's actual problem, instead of a thread for you to write your thoughts out as they happen.

    Yes, this post is itself off-topic, but hopefully explains where we are coming from enough that we can all just drop this side-issue and return to helping people with programming, hopefully in a more productive manner.

    I provided code which produced the files he required, somewhat more than what you have managed.

    I don't know why Matticus got a segmentation fault, perhaps he made an error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help Debugging (ADT)
    By minidragon in forum C Programming
    Replies: 16
    Last Post: 08-08-2011, 06:39 PM
  2. Debugging Dev C++
    By tuurb046 in forum Tech Board
    Replies: 10
    Last Post: 08-16-2007, 12:51 PM
  3. debugging
    By St0rM-MaN in forum Tech Board
    Replies: 13
    Last Post: 07-06-2007, 02:03 PM
  4. help debugging
    By MB1 in forum C++ Programming
    Replies: 6
    Last Post: 11-03-2005, 01:48 PM
  5. VC++ Debugging
    By neandrake in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2003, 07:31 PM