O
ormax3
... is it an undefined behaviour?? You would expect this code to this
code ti give a segmentation fault but it doesnt.. Why??
Sample Code:
---------------
typedef struct {
char name[10];
int x;
} Employee;
int main()
{
// allocate and fill e1
Employee **e1 = (Employee**) malloc( 5 * sizeof(Employee*) );
for(i=0 ; i<5 ; ++i) {
e1 = (Employee*) malloc( sizeof(Employee) );
fillEmployee(e1);
}
// make shallow copy
Employee **e2 = (Employee**) malloc( 5 * sizeof(Employee*) );
for(i=0 ; i<5 ; ++i) {
e2 = e1;
}
// free the copy
for(i=0 ; i<5 ; ++i) {
free(e2);
}
free(e2);
e2 = NULL;
// free the original one
for(i=0 ; i<5 ; ++i) {
free(e1);
}
free(e1);
e1 = NULL;
return 0;
}
Thanks
code ti give a segmentation fault but it doesnt.. Why??
Sample Code:
---------------
typedef struct {
char name[10];
int x;
} Employee;
int main()
{
// allocate and fill e1
Employee **e1 = (Employee**) malloc( 5 * sizeof(Employee*) );
for(i=0 ; i<5 ; ++i) {
e1 = (Employee*) malloc( sizeof(Employee) );
fillEmployee(e1);
}
// make shallow copy
Employee **e2 = (Employee**) malloc( 5 * sizeof(Employee*) );
for(i=0 ; i<5 ; ++i) {
e2 = e1;
}
// free the copy
for(i=0 ; i<5 ; ++i) {
free(e2);
}
free(e2);
e2 = NULL;
// free the original one
for(i=0 ; i<5 ; ++i) {
free(e1);
}
free(e1);
e1 = NULL;
return 0;
}
Thanks