Thread: can you spot the error in my code?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    can you spot the error in my code?

    hi, i am trying to sort a file and i keep getting a run failed when i call the method sortList.I have gone through my code but i cant find any error
    Code:
    void sortList(FILE *p,int noofchil){
        struct child *temp3;
        struct child *temp1;
        struct child *temp2;;
        int in;
        int out;
        printf("dd");
        
               
        	 for(out=noofchil-1;out>1;out--)
             {
        		 for(in=0;in<out;in++)
                     {
                         fseek(p,sizeof(struct child)*in ,SEEK_SET);
                         fread(temp1,sizeof(struct child),1,p);
                         fseek(p,sizeof(struct child)* (in +1) ,SEEK_SET);
                         fread(temp2,sizeof(struct child),1,p);
        			 if(compareChilds(temp1,temp2)==1)
                             {
        				 temp3=temp2;
                                     temp2=temp1;
                                     temp1=temp3;
                                     fseek(p,sizeof(struct child)*in ,SEEK_SET);
                                     fwrite(temp1,sizeof(struct child),1,p);
                                     fseek(p,sizeof(struct child)*(in+1) ,SEEK_SET);
                                     fwrite(temp1,sizeof(struct child),1,p);
                                     
        			 }
                            
        				
        		 }
        	 }
        
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
        struct child *temp3;
        struct child *temp1;
        struct child *temp2;;
    You NEVER allocated any space for the above pointers!
    (Likely you should NOT be using pointers.)
    You NEVER set them to a valid address; so, you are randomly over writing the memory space.

    Tim S.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by stahta01 View Post
    Code:
        struct child *temp3;
        struct child *temp1;
        struct child *temp2;;
    You NEVER allocated any space for the above pointers!
    (Likely you should NOT be using pointers.)
    You NEVER set them to a valid address; so, you are randomly over writing the memory space.

    Tim S.
    i cant believe i made such a mistake

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by stahta01 View Post
    Code:
        struct child *temp3;
        struct child *temp1;
        struct child *temp2;;
    You NEVER allocated any space for the above pointers!
    (Likely you should NOT be using pointers.)
    You NEVER set them to a valid address; so, you are randomly over writing the memory space.

    Tim S.
    Just observed an erro my sort algorithm is not working properly..certain values are being repeated

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by sigur47 View Post
    Just observed an erro my sort algorithm is not working properly..certain values are being repeated
    How many different items do you write out?

    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-30-2011, 11:55 AM
  2. spot the error
    By razza in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 09:21 AM
  3. can someone help spot the error
    By blight2c in forum C++ Programming
    Replies: 6
    Last Post: 04-07-2002, 12:29 PM
  4. Can anyone spot the error in my code
    By korbitz in forum C Programming
    Replies: 2
    Last Post: 01-05-2002, 02:05 AM
  5. returning to a spot in the code
    By Shadow in forum C Programming
    Replies: 4
    Last Post: 09-22-2001, 05:24 AM

Tags for this Thread