Ownership, cleanup aur partial work
Checked file copy ki acquisition, short transfers, close failures, borrowing aur partial-output limits follow karein.
Is chapter mein8 sections
Task kai resources acquire karke beech mein fail ho sakta hai. Cleanup mein track karte hain ki kya acquire hua aur sahi paths par release karte hain. Resource release karna aur program ke bahar likha work undo karna bhi alag hain.
1. Function kya own karta hai, record karein
Cleanup path successful acquisitions ke resources release karta hai. Ownership batati hai release kaun kare; borrowing temporary use deti hai bina lifetime responsibility transfer kiye. Owned pointers NULL initialize, clear order mein acquire aur har acquired resource once release karein. Helper ko stdin/stdout pointer mila isliye use close kar dena correct ownership decision nahi.
- Nothing opened
- Close karne ko kuch nahi
- Input opened
- Output acquisition fail ho toh input close
- Both opened
- Har later exit par output aur input close
2. Checked copy ke failure paths follow karein
C17 program existing binary input ko newly created binary output mein copy karta hai. Wbx already existing destination refuse karta hai, including source path itself. Two command-line arguments source aur destination names hain. Core helper dono streams own karta hai; caller names choose aur status report karta hai.
#include <stdio.h>
#include <stdlib.h>
static int copy_file(const char *source_name, const char *target_name)
{
FILE *input = NULL;
FILE *output = NULL;
int status = EXIT_FAILURE;
input = fopen(source_name, "rb");
if (input == NULL) goto cleanup;
output = fopen(target_name, "wbx");
if (output == NULL) goto cleanup;
unsigned char block[256];
size_t count;
while ((count = fread(block, 1, sizeof block, input)) != 0) {
if (fwrite(block, 1, count, output) != count) goto cleanup;
}
if (ferror(input)) goto cleanup;
status = EXIT_SUCCESS;
cleanup:
if (output != NULL && fclose(output) == EOF) status = EXIT_FAILURE;
if (input != NULL && fclose(input) == EOF) status = EXIT_FAILURE;
return status;
}
int main(int argc, char *argv[])
{
if (argc != 3) return EXIT_FAILURE;
if (copy_file(argv[1], argv[2]) != EXIT_SUCCESS) {
fputs("Copy failed; output may be incomplete.\n", stderr);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Goto same function ke label par jump karta hai. Yahan failure exits ek cleanup section mein gather hote hain, taaki har operation ke baad releases duplicate na hon. Yeh variably modified scope mein jump nahi karta. First failure status EXIT_FAILURE rakhti hai; sirf completed read/write loop success set karta hai. Short write ya read error failure selected chhodta hai; har fclose provisional success ko failure bana sakta hai.
3. Close bhi operation hai, sirf tidying nahi
Successful buffered fwrite ke baad fclose mein failure aa sakti hai. Sirf writes check karne se final failure miss hogi. Har handle ka one close attempt hota hai aur phir use abandon karte hain; EOF result par bhi same handle dobara fclose nahi kar sakte. Remaining owned stream ka cleanup continue hota hai. Close ke baad non-null pointer target ab bhi open hai iska proof nahi.
Function combined failure status report karta hai. Larger application diagnostics ke liye first operation failure aur cleanup failure separately preserve kar sakti hai. Valid diagnostic details cleanup se replace hone se pehle capture karein. Operation guarantee na de toh errno details invent na karein. Main se return ya exit normal stream cleanup karta hai, par close failures observe/report karne ke liye explicit close chahiye.
4. Resources release karna partial work undo nahi karta
Output create hone ke baad copying fail ho toh partial file remain ho sakti hai. Close phir bhi required hai, par cleanup all-or-nothing file transaction promise nahi karti. Production replacement workflow temporary file, validated flush/close aur documented replacement operation use kar sakti hai; cross-platform atomic replacement aur physical durability ke liye additional platform guarantees chahiye. Teaching example un properties ka claim nahi karta.
5. Borrowed stream ka owner same rehta hai
#include <stdio.h>
#include <stdlib.h>
/* Borrow stream for this call; do not close it. */
static int write_heading(FILE *stream)
{
return fputs("CodedileRead\n", stream) == EOF ? -1 : 0;
}
int main(void)
{
if (write_heading(stdout) != 0) return EXIT_FAILURE;
return fflush(stdout) == EOF ? EXIT_FAILURE : EXIT_SUCCESS;
}
Helper heading write karta hai, stream close nahi aur buffered output final destination tak pahunchne ka claim nahi. Caller flush ya close ka timing decide karta hai. Separation helper ko stdout, file ya other suitable output stream ke saath reusable banati hai. Interface sirf FILE pointer ho tab bhi lifetime/access preconditions document karein.
Primary references aur aage ki reading
- WG14 N1570 — public C11 committee draft: 6.8.6.1, 7.21.3, 7.21.5.1, 7.21.5.3, 7.21.8, 7.22.4.4. C17 baseline ke stable core provisions ke liye; final ISO publication nahi.
Aage badhne se pehle
- Cleanup depend karti hai ki failure se pehle kaun-se resources acquire hue.
- Ownership release ki responsibility aur references ki validity batati hai.
- Resources close karne se external partial work undo ya durable storage prove nahi hota.
In ideas ko alag rakhein
- Cleanup / Rollback
- Cleanup owned resources release karti hai; rollback committed ya partial effects undo karne ki additional policy hai.
File copy beech mein fail ho toh kaun-se resources aur partial effects track karenge?
Optional: bolkar samjhein ya note rakhein. Tayyar hon toh aage badh sakte hain.Apni understanding check karein
Optional hai. Answers save ya send nahi hote; chapter kabhi lock nahi hota.
Har choice ki explanation padhein
- Har imagined resource release karein, jo acquired hi nahi hua use bhi.
Cleanup actual acquisition state aur API contract follow kare.
- Turant return karein aur pehle resource ka owner koi na rahe.
Ownership kho dene se pehla resource leak ho sakta hai.
- Ownership contract ke according pehla resource release ya transfer karein.
✓ Sahi reasoning. Partial work mein bhi already acquired resources ki cleanup obligations hoti hain.
C17
Agla idea padhne ke liye tayyar?
Mark this chapter as read whenever you feel ready.