Null Terminated String Safety Issues | C Programming Tutorial

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

The basic behavior of strings in C, being arrays that are null terminated, got me pulling my hair(as a beginner) for weeks. Until I found that I wasn't adding the null terminator. So the string buffers were either incorrect in some cases, but in others doing a simple writing to them went off and overwrote other structs(memory regions) that weren't part of the array, simply because there was no null at the end of these. So the first 6min of the video brought back bad memory of days of frustration.

NikolaNevenov
Автор

Great video! That's one of the beauties of using a small and efficient language like C. When you choose to use C, you take responsibility for your own decisions, and there's no "nanny" to take care of you. Yes, it can be frustrating to hunt down errors that can take hours to eliminate, but there are also moments of great satisfaction when everything seems to work perfectly.

For learning purposes, I have created my own string library, and although it doesn't always perform operations without errors, I consider it as part of the learning process.

I often use calloc, so strings are automatically terminated.

In the end, I find that the code always does exactly what I programmed it to do, not necessarily what I intended it to do. It's all part of the learning journey.

grimvian
Автор

as an embedded designer, I can't emphasize how critical it is to master the application and structures of strings and character (char) manipulation. In serial communication applications, such as asyncronous data transfer (no clock pulses are used) in Bluetooth, WiFi and ATM machines; characters are transmitted one at a time and received one at a time. When the end of string is reached and you fail to concatinate the NULL character at the end then your system will be screwed up. Very important video for all serious programmers, like you and me. Thank you Kevin.

fifaham
Автор

@18:48 Even if the memory today is very cheap, I wouldnt take the risk and "not" limit the use of memory as per project requirements. This is very risky: ATM machine TranCeive (transmit-Receive) data from user interface to data base, if the user aske the ATM machine to withdraw $100 and the 'NULL" got lost because of thunderstorm electromagnetic interferrence or error in data exchange then the machine will accidently dispose $1000 (of say $1005) instead of $100 and register it as $100 only. Those mistakes get very serious in areas of medical, military, financial and other critical applications.

fifaham
Автор

@6:32 Can we cast those valuse as characters (char) so we get an error because a decimal was used, instead of character? Getting an error is to our advantage, or else the code will take it as NULL if decimal is used. Casting as character will caution us that there is something wrong going on and it needs our attention.

fifaham
Автор

I remember pascal storing the size of the string in index 0. But pascal string where limited to 255chars....

jp
Автор

But when declaring a char array like char array[] = "Hello"; does it add a \0 automatically?

adrianbarit
Автор

It is a bit the same, isn't it? What protects against passing the wrong number of charactes?

FritsvanDoorn
Автор

7:21 - This shouldn't compile in proper C. You declare a variable after calling printf().

Ribulosediphosphat
visit shbcf.ru