Thread: realloc segfaults.

  1. #16
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Are you sure you are needing to use realloc, is this some realloc exercise? As calling this over and over for each new student roll to be entered is costly.

    I would prefer you change over to strictly using an array for this and is much faster to insert.

    Code:
     StdInfo students[10];
    
    ...
    
     do
        {
          printf ("Enter Roll of Student %d :\t", i);
          scanf ("%d", &(students[i++].roll));
          printf ("\n\nEnter another ? (1/0):\t");
          scanf ("%d", &s);
        }
      while (s == 1);
    Remove your realloc and any malloc calls, you do no need them then!


    Then do double check that they have been inserted try something like this:

    Code:
      int z = 0;
    
      for (; z < i; z++)
        {
          printf ("Students rolls are:  student %d, roll = %d\n", z,
                  students[z].roll);
        }

  2. #17
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Quote Originally Posted by slingerland3g View Post
    Are you sure you are needing to use realloc, is this some realloc exercise?
    You can assume something like this.I was showing some of my friends about when we might need to use realloc. suddenly I got stuck and more than 8 hours past still trying to find the answer. I must solve it by using malloc and realloc only.

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The OP has already been told that he can't scanf into a short, here: http://www.gamedev.net/community/for...opic_id=515880
    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"

  4. #19
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Quote Originally Posted by iMalc View Post
    The OP has already been told that he can't scanf into a short, here: http://www.gamedev.net/community/for...opic_id=515880
    Currently there is no short in my code I've changed all of them to normal int

  5. #20
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    %d pulls in an integer.
    s is a short.

    Can you spell O-V-E-R-L-A-Y?
    Mainframe assembler programmer by trade. C coder when I can.

  6. #21
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by noobcpp View Post
    Currently there is no short in my code I've changed all of them to normal int
    Then what's the problem? (and none of the code you've posted has s as an int.)
    Mainframe assembler programmer by trade. C coder when I can.

  7. #22
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    What Dino said. Your variable assignment for
    Code:
     unsigned short int s = 1
    should be

    Code:
    int s = 1;

  8. #23
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Doubling the size of the array on each entry will make it exceed 4 gigabytes within 32 iterations...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #24
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Quote Originally Posted by slingerland3g View Post
    What Dino said. Your variable assignment for
    Code:
     unsigned short int s = 1
    should be

    Code:
    int s = 1;
    Ya I've already done it and it gives the same result no change.
    Well this is my current code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct StudentInfo{
    	//char* name;
    	unsigned int roll;
    };
    
    typedef struct StudentInfo StdInfo;
    
    int main(int argc, char** argv){
    	StdInfo* students;
    	unsigned int size = 3, i=0, j;
    	int s = 1;
    	char* dump_name;
    
    	students = malloc( size*sizeof(StdInfo) );
    
    	do{
    		//StudentInfo.name removed
    
    		printf("Enter Roll of Student %d :\t", i);
    		//fflush(stdin);
    		int dmp_roll;
    		scanf("%d\n", &dmp_roll);
    		students[i].roll = dmp_roll;
    		printf("\n\nEnter another ? (1/0):\t");
    		//fflush(stdin);
    		scanf("%d\n", &s);
    		//fflush(stdin);
    		if(s == 1){
    			size += 1;
    			void* dump = realloc( students, size*sizeof(StdInfo) );
    			if(dump != 0x0){
    				students = dump;
    			}else{
    				printf("realloc Failed\n");
    				return 2;
    			}
    		}
    		i++;
    	}while(s == 1);
    
    	free(students);
    	students = 0x0;
    
    	return 0;
    }
    It also makes scanf problem.

    Quote Originally Posted by brewbuck View Post
    Doubling the size of the array on each entry will make it exceed 4 gigabytes within 32 iterations...
    I've already changed it to size += 1 So its not doubling the size.

  10. #25
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    That code does not crash, but it's question asking does get out of sync with my input. Being one who has practically never used scanf (I program real-world apps ), I'll refrain from trying to help you there.
    It is a bit odd that you'd allocate space for two more items than you are actually using though.
    Those commented out fflush's of stdin in better stay that way. Putting them in would be a bad thing to do as it's UB. Best remove those lines.
    I could also comment about many style issues that bug me. For example dump != NULL would be more appropriate than dump != 0x0.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. did i understood right this explantion of realloc..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 10-24-2008, 07:26 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM
  4. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 04:40 PM
  5. Realloc inappropriate for aligned blocks - Alternatives?
    By zeckensack in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 02:10 PM