ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You intentionally wrote and read past the bounds of the array and created undefined behavior. You are seeing the results of undefined behavior. The answer is that it's the result of undefined behavior. There isn't a "better" answer.
As dugan said, you are attempting to read beyond the end of the array, the result of doing that is undefined.
An important part of learning C is learning to never attempt to access unallocated memory, or read from uninitialized memory. Reading/writing beyond the end of an array (unintentionally) is a common way that can happen, so learning to guard against doing that in your code is an important lesson to learn in itself.
Doing so intentionally does not change the fact it is still undefined behavior!
I never created myNumbers[4] and I wanted to see what would happen when I called it. That 6 came up.
Why 6?
Actually several things may happen:
1. a random value is taken (which is the content of the given memory). It was set earlier somewhere/elsewhere, actually we can say it is undefined.
2. segfault: the addressed memory is not available at all, the program will die. (also accessing myNumber[5] may die).
But I am very disappointed. I mean, how is that even allowed to happen? I've been using script languages for 20 years, languages that are often scoffed at while C is always regard in very high respect. But none of the scripting languages I have used would allow that nonsense. They would return NULL or the empty string, or maybe even throw an error. But the highfallutin C just makes up a random value out of thin air? That is mickey mouse.
But I am very disappointed. I mean, how is that even allowed to happen? I've been using script languages for 20 years, languages that are often scoffed at while C is always regard in very high respect. But none of the scripting languages I have used would allow that nonsense. They would return NULL or the empty string, or maybe even throw an error. But the highfallutin C just makes up a random value out of thin air? That is mickey mouse.
The power of C derives from the fact that it allows you mostly direct access to the underlying hardware resources. As EdGr points out, it just a small step away from an assembler, two small steps away from native machine code as it were.
C performs its checks at compile time, primarily type checking, but it otherwise gives you, the programmer, a free hand to access the underlying resources directly via their address in program memory space. That is a two edged sword - it gives you great power, but it requires you to understand and be responsible for the effects of your actions.
Last edited by astrogeek; 09-08-2022 at 12:08 PM.
Reason: tyops
In addition, c does not initialize variables to null like python or bash when they are declared. c uses stack space for variables and only sets aside the amount of memory necessary. Whatever contents of that memory location at the time will appear as the value of that variable.
C was never meant to replace P-languages (Perl, Python, PHP), but those languages are implemented in C, so when you use a Python-scrypt, you indirectly use C.
So it checks for types because god forbid you may mistype a variable.
But it won't check for nonexisting values. In fact, it will gladly produce a fake value on the spot so you'll never suspect there is an error in the code.
If it were a compiler (not an interpreter), you would see the difference between `compile time` and `run time`. The `type of variables don't match` is a compile-time problem, the `index out of range` is a `run-time` problem.
So it checks for types because god forbid you may mistype a variable.
But it won't check for nonexisting values. In fact, it will gladly produce a fake value on the spot so you'll never suspect there is an error in the code.
Got it.
No, it checks types because the meaning of the pattern of bits in memory located at a given address depends totally on the type for which the memory was allocated and by which its content is to be interpreted, to prevent many common programm[ing|er] errors.
Not to put too fine a point on it, but your expectations must conform to the language, not the other way around. If you want to learn an untyped language with runtime memory management and error checks, C is not the place to start. If you want to learn C, then learn how and why it works as it does, why that is a good thing, and use it accordingly.
Last edited by astrogeek; 09-08-2022 at 12:58 PM.
Reason: better verbiage
If it were a compiler (not an interpreter), you would see the difference between `compile time` and `run time`. The `type of variables don't match` is a compile-time problem, the `index out of range` is a `run-time` problem.
You need to be very clear about that then. Your previous thread was all about finding an interpreter which understood C syntax in order to avoid using a compiler. Others here may be excused for thinking that you are using an interpreter.
Be aware that running a subset of C syntax through an interpreter is never going to be the same as running compiled C. The interpreter, no matter how well it seems to understand the syntax of C, is still providing all the memory management and runtime environment, and likely does not expose you to any of the features of the preprocessor, compiler or linker, which are all integral parts of the C programming language itself.
With that thought now in mind, consider that learning the syntax of C is not the same as learning the C programming language - it is only a subset. The preprocessor, compiler and linker are parts of the language, not impediments to its use.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.