Video není dostupné.
Omlouváme se.

Handling Errors in C/Unix (perror, strerror, errno)

Sdílet
Vložit
  • čas přidán 7. 08. 2024
  • Patreon ➤ / jacobsorber
    Courses ➤ jacobsorber.thinkific.com
    Website ➤ www.jacobsorber.com
    ---
    ***
    Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
    About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
    More about me and what I do:
    people.cs.clemson.edu/~jsorber/
    persist.cs.clemson.edu/
    ***
    Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
    About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
    More about me and what I do:
    www.jacobsorber.com
    people.cs.clemson.edu/~jsorber/
    persist.cs.clemson.edu/
    To Support the Channel:
    + like, subscribe, spread the word
    + contribute via Patreon --- [ / jacobsorber ]
    + rep the channel with nerdy merch --- [teespring.com/stores/jacob-so...]
    Source code is also available to Patreon supporters. --- [jsorber-youtube-source.heroku...]
    Want me to review your code?
    Email the code to js.reviews.code@gmail.com. Code should be simple and in one of the following languages: C, C++, python, java, ruby. You must be the author of the code and have rights to post it. Please include the following statement in your email: "I attest that this is my code, and I hereby give Jacob Sorber the right to use, review, post, comment on, and modify this code on his videos."
    You can also find more info about code reviews here.
    • I want to review your ...

Komentáře • 65

  • @Pawl0solidus
    @Pawl0solidus Před 2 lety +34

    Please Jacob do a series of design patterns in C.

  • @RobarthVideo
    @RobarthVideo Před 3 lety +17

    From my experience, the error handling in C is one of the few cases, where the goto statement is useful. It can just go to the end of the function scope and fall through, thus freeing all the so far allocated memory in a stack based manner.

    • @RobarthVideo
      @RobarthVideo Před rokem

      @James you're not just going to exit the function. You are going to the end of it where you have the deallocation necessary. If something hasn't been allocated yet, you just goto a label below its deallocation

  • @YokeRoel
    @YokeRoel Před 2 lety +1

    That was super helpful and entertaining! :)
    It also replaced my bedtime story since I’m about to sleep. Cheers!

  • @Redmat527
    @Redmat527 Před 2 lety +3

    One interesting feature that I learned very recently is that you can use the %m formatter when using printf in replacement for %s plus strerror

  • @melissarainchild
    @melissarainchild Před 4 lety +1

    Subscribed! Notifications to ALL. Mid-corona lockdown, I have set to the task of learning C (go figure, I'm a developer that did'nt get to have C in school...) so this is usefull (and that is an understatement)...lemme have more of this :)

  • @sofiabueno2594
    @sofiabueno2594 Před 9 dny

    amazing video! thank you so much

  • @JT-nq9vh
    @JT-nq9vh Před 3 lety +1

    Your videos are great, please keep it up

  • @slap405
    @slap405 Před rokem

    great video , makes hard subjects easy to understand !

  • @Programeris
    @Programeris Před 3 měsíci

    exceptional as always ;) 🙌

  • @Ricko65
    @Ricko65 Před 2 lety

    Thank you so very much for your informative post!!!!!

  • @computerscientologist
    @computerscientologist Před 5 lety +13

    these are super helpful

  • @kratosreag
    @kratosreag Před 3 lety

    It helped a lot, thank you!

  • @Sahilbc-wj8qk
    @Sahilbc-wj8qk Před 5 lety

    3:35 to 3:47 that's now clear my doubts of why errno is stored and why it's value differ ..in every time before or after function call like opening files or other.
    That's really amazing and helpful I was in doubts and confusion about it

  • @mannb1023
    @mannb1023 Před rokem

    Really good video

  • @francoismartineau2519

    Great quality videos!!

  • @dwaynestgeorge2558
    @dwaynestgeorge2558 Před rokem

    Thanks

  • @simonsprogoe5496
    @simonsprogoe5496 Před 4 lety +2

    Love your videos

  • @thecelestialape8148
    @thecelestialape8148 Před 4 lety

    very helpful! thank you!

  • @user-zb3yh2gj7e
    @user-zb3yh2gj7e Před 3 lety

    awesome!!!

  • @ayashakumarimandal8681

    Amazing, love from India...

  • @XenoTravis
    @XenoTravis Před 5 lety +4

    Beginner students ignore the internal manual because they have videos like this or the all mighty stack overflow. They are more direct and to the exact point.
    I guess the next thing is to ask if that is a bad thing.
    Also, you were right about the common pronunciation of errno. But if you were to teach it to students, maybe say error number for clarity.

    • @JacobSorber
      @JacobSorber  Před 5 lety +9

      I don't think using videos and stack overflow is a bad thing. I would want to use those AND the man pages. The manual pages definitely don't feel super friendly to the uninitiated. But, once you get used to how they are structured, I can usually find most answers more quickly with the man pages than I can with a browser, and I don't need a network connection.

    • @michaelmaitland3126
      @michaelmaitland3126 Před 5 lety +1

      as a beginner programmer I felt like this video showed me some good/common practices (such as the use of this specific macro) that I can now implement into my code that I might not have gotten from just the man pages. If you have any resources other than videos that help show common practices in action dm me plz

    • @papasmurf9146
      @papasmurf9146 Před 2 lety +1

      @@JacobSorber I would phrase it as the beginner uses stack-overflow and CZcams, the seasoned professional uses man pages. Always good to introduce professional habits early. Eventually you might be at a job site and need a quick reference and hitting CZcams from the company hardware may be a negative.

  • @InMemoryOfNeo
    @InMemoryOfNeo Před 3 lety

    is writing macro better then writing function? Thanks.

  • @benjaminshinar9509
    @benjaminshinar9509 Před 5 lety +7

    great vid, very helpful.
    just a question:
    is there a reason to prefer fprint over printf?

    • @JacobSorber
      @JacobSorber  Před 5 lety +13

      It's generally a good idea to send error messages to stderr instead of stdout. It's just convention, but it makes your programs easier to use if we separate the output. Otherwise, printf works just fine.

    • @GurpreetKakar4873
      @GurpreetKakar4873 Před 5 lety +9

      in fprintf you can specify to which stream you want to output format text. printf simply writes to stdout.

    • @davidlynch4202
      @davidlynch4202 Před rokem +2

      stdout is buffered so there can be some inconsistencies in output
      stderr isn’t buffered so as soon as printf is called it will write to the screen

  • @iandmetick07
    @iandmetick07 Před 3 lety

    I have problem with memory layout in c and how c get interpreted into assembly and interted into memory

  • @outrotipo4193
    @outrotipo4193 Před 4 lety +6

    Great video, thanks for all this work!
    I got a question:
    You mention that macros give you the line number and file name, while a functions give you safety (and a cleaner syntax).
    This makes me think that you can write an error-handling function (say, check_func(int val, int lineno, char *filename)), but instead of calling it directly, use a macro (the macro expands to a call to check_func while adding the line number and file name).
    Not sure whether that is a good idea...

  • @hasanalattar9561
    @hasanalattar9561 Před 3 lety

    does this mean in multithreaded .. i cannot check errorno unless i encapsulate the sys call function inside a critical section ... because maybe other thread will call another syscall and change the error no before i handle it ?

    • @monochromeart7311
      @monochromeart7311 Před 2 lety +3

      From the man page for errno:
      _errno_ is defined by the ISO C standard to be a modifiable lvalue of type _int_ , and must not be explicitly declared; _errno_ may be a macro.
      _errno_ is thread-local; setting it in one thread does not affect its value in any other thread.

  • @low-key-gamer6117
    @low-key-gamer6117 Před 3 lety

    Clang?

  • @Poeterish
    @Poeterish Před 2 lety

    When using exit(-1), shouldn't free all the dynamically allocated stuff that you previously have outside the failing function call?

    • @papasmurf9146
      @papasmurf9146 Před 2 lety

      The exit call will terminate the program. Depending on the operating system, it will terminate the process, close open files, free allocated memory, etc. -- but it depends on the operating system. Some embedded operating systems may be less forgiving. Additionally, there may be some things (shared memory structures?) that the operating system won't know to clean up. Good practice would be to always clean up after yourself -- and this might have to be part of your design from the beginning so that you don't have a function call 15-deep that can terminate the program (you might also consider looking at on_exit if that's an issue from inherited code).

    • @maxaafbackname5562
      @maxaafbackname5562 Před 2 lety

      @@papasmurf9146 on embedded systems exit() makes no sense.
      That's why it doesn't matter what to do in exit()

  • @dorle3046
    @dorle3046 Před rokem +1

    What is the -1; after exit(-1); for?

    • @mamba201
      @mamba201 Před rokem +2

      It means that this part of conditional expresion runs some code and returns -1, it does nothing in this particular macro since it doesn' t assign value to variable.
      Try this to see how it works:
      int a = 3;
      a = (a == 3) ? ({printf("true
      ");1;}) : ({printf("false
      ");0;});
      printf("a: %d
      ", a);

  • @codingsafari
    @codingsafari Před 9 měsíci

    what I dont understand is why they use a negative one do indicte a failure. From working with linux and bash and so on, usually the canonical failure code is 1 and not -1.

  • @xpdseth
    @xpdseth Před 3 lety

    Is it preferable to use assert instead of the macro?

    • @Uerdue
      @Uerdue Před 2 lety

      I don't think so. Assertions are only for debugging, not for control flow / error handling. They're usually even turned off for release builds.

    • @maxaafbackname5562
      @maxaafbackname5562 Před 2 lety

      Assert calls abort() in stead of exit()?

  • @maxaafbackname5562
    @maxaafbackname5562 Před 2 lety

    You forgot to use the macro on fputc().

  • @sheetalray818
    @sheetalray818 Před 5 lety

    Can I use this code as an "C program to generate illegal exception in UNIX"

    • @JacobSorber
      @JacobSorber  Před 5 lety +2

      It sounds like you're trying to match this video up to some specific requirement (maybe for a school assignment?). In any case, the question is vague enough that I'm not sure how to answer. When I think about "exceptions", I'm typically thinking about illegal instructions (bad code or trying to execute data that isn't code), illegal memory accesses (bad pointer dereferences, usually), or arithmetic problems (divide by zero, overflow)-not so much error codes returned from any library call. But, these terms aren't super specific. If it's for an assignment, I don't know what your instructor expects or counts as an illegal exception.

    • @sheetalray818
      @sheetalray818 Před 5 lety

      I'm glad that you replied...
      Thank you for your concern...
      Well I'm searching for a college assignment...
      That's true

  • @carlossantamaria1820
    @carlossantamaria1820 Před 2 lety

    can you help me with something?

  • @mizel_1121
    @mizel_1121 Před 2 lety

    error is returned from system call posix that why c language is the best language for OS

  • @questionmarc8
    @questionmarc8 Před 5 měsíci

    errno is a C standard thing, it is NOT specific to unix. It is used in cross-platform functions such as strtol, which sets errno to ERANGE when the number string passed to it is out of range.

  • @ropersonline
    @ropersonline Před 4 lety

    3:41: "So one morning, ERRNO is set by a lot of different calls..."
    Yes I know it's _one warning,_ but I prefer the fairy tale version better and I want to hear the rest of it. Any takers?

    • @JacobSorber
      @JacobSorber  Před 4 lety +2

      Ah, that's funny. Editing glitch, of course. I'll try to remember what story I was about to tell about ERRNO going on an epic adventure and weave it into a future video. 😀

    • @ropersonline
      @ropersonline Před 4 lety

      @@JacobSorber If you enjoy this sort of thing, google misheard lyrics or mondegreens.

  • @unscriptedviusals
    @unscriptedviusals Před 2 lety

    slow down

  • @spiewajit4ncz
    @spiewajit4ncz Před 2 lety +1

    too fast, slow down

    • @papasmurf9146
      @papasmurf9146 Před 2 lety

      I almost always modify the play back speed of videos on CZcams (under the gear icon). The only real problem I've hit with that is shifting from watching on CZcams to interacting with people in real-life.

  • @papasmurf9146
    @papasmurf9146 Před 2 lety +1

    Not all operating systems use -1 (or 0 or pick your favorite constant) to indicate an error on exit. EXIT_SUCCESS and EXIT_FAILURE should be used (they're defined in stdlib.h)
    #include
    int main(int argc, char* argv[])
    {
    if (argc < 2)
    exit(EXIT_FAILURE);
    exit(EXIT_SUCCESS);
    }