This operation makes no sense to me. Why does the value in students not take?
Here's the code: (same as above with printfs)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct StudentInfo{
unsigned int roll;
};
typedef struct StudentInfo StdInfo;
int main(int argc, char** argv){
StdInfo* students;
unsigned int size = 3, i=0 ;
unsigned short int s = 1;
// char* dump_name;
students = malloc( size*sizeof(StdInfo) );
printf("students @ = %p\n", students) ;
printf("Size of StdInfo = %d\n", size*sizeof(StdInfo)) ;
do{
printf("Enter Roll of Student %d :\t", i);
scanf("%d", &(students[i].roll));
printf("\n\nEnter another ? (1/0):\t");
scanf("%d", &s);
if(s == 1){
size *= 2;
printf("New Size of StdInfo = %d\n", size*sizeof(StdInfo)) ;
printf("students @ %p being realloced...\n", students) ;
students = realloc( students, size*sizeof(StdInfo) );
printf("students new @ = %p\n", students) ;
}
i++;
}while(s == 1);
free(students);
students = 0x0;
return 0;
}
and here's the output. Note the original address of 0x100000 does not change!!
Code:
[Session started at 2008-11-25 10:40:40 -0600.]
students @ = 0x100180
Size of StdInfo = 12
Enter Roll of Student 0 : 1
Enter another ? (1/0): 1
New Size of StdInfo = 24
students @ 0x100000 being realloced...
students new @ = 0x100000
Enter Roll of Student 1 : 1
Enter another ? (1/0): 1
New Size of StdInfo = 48
students @ 0x100000 being realloced...
students new @ = 0x100150
Enter Roll of Student 2 : 1
Enter another ? (1/0): 1
New Size of StdInfo = 96
students @ 0x100000 being realloced...
c_misc3(41686) malloc: *** error for object 0x100000: double free
*** set a breakpoint in malloc_error_break to debug
students new @ = 0x1001d0
Enter Roll of Student 3 : 1
Enter another ? (1/0): 1
New Size of StdInfo = 192
students @ 0x100000 being realloced...
c_misc3(41686) malloc: *** error for object 0x100000: double free
*** set a breakpoint in malloc_error_break to debug
students new @ = 0x100230
Enter Roll of Student 4 :