Why would C automatically process the next available pointer if the designated pointer is closed? -
i have question file pointer operations in c; specifically, happens when command uses closed file pointer.
here example
file *fp1; fp1 = fopen(file1,"r"); function1(fp1); fclose(fp1); file *fp2; fp2 = fopen(file2,"r"); function2(fp1); fclose(fp2);
so found out is: since fp1 closed, code automatically use next open/linked pointer, in case, fp2. however, if code changed
file *fp1; //fp1 = fopen(file1,"r"); //function1(fp1); fclose(fp1); file *fp2; fp2 = fopen(file2,"r"); function2(fp1); fclose(fp2);
there segmentation fault. in addition, if there open file pointer fp3 in between fp1 , fp2, , fp1 closed, code select fp3.
i not sure way of c dealing deleted/closed file pointer language specific fail-safe mechanism, or has way these pointer variables lie in physical memory? , happens these pointers after fclose()?
eg. if file pointers stored consecutively in memory , fclose delete memory current pointer uses, c access next available memory chunk value.
ps: not sure if file pointers stored separately other variables tested inserting new int pointer assignment between above fp1 , fp2 segments. not interfere file pointer "inherit" process. file pointers stored separately?
eg. however, if flcose() make current pointer null pointer, should command using current pointer produce error?
thank you!
performing action on file
pointed fp1
after calling fclose
on results in undefined behavior. in likelihood, what's happening that, since memory pointed fp1
freed reuse, fp2
happens have same value fp1
had prior becoming invalid, , when implementation dereferences fp1
, accesses memory pointed fp2
.
Comments
Post a Comment