-1

[enter image description here][1]First of all I declared array of 5 elements.

int a[5];

And then I assign 58 to a[8] which is for experiment.

After I tried to print the value of a[8].I was shocked when I viewed the output as 58.

I feel its ok may be compiler allocated memory dynamically and so I wanna test with sizeof(a).The output was 20(5*4) then I be like hmm??

Check the demo program and of course my image too.

#include<stdio.h>
 int main()
{
    int a[5];
    a[8]=58;
    printf("\t a8=%d snd size of A= %d",a[8],sizeof(a));
}

The output was a8=58 snd size of A= 20

Note the fact that I practice the same with char and of course on different devices.

Ultimately when I tried to assign 58 to a[15] or increasing index then the output was 0 all the cases.enter image description here

  • 3
    "And then I assign 58 to a[8] which is for experiment." And with that experiment you enter the dark realm of "undefined behaviour". All bets are off. Whatever your experiments show as result - you can ignore it. Read up on "nasal demons". – Yunnosch Jul 05 '22 at 06:07
  • FAQ: [What is undefined behavior and how does it work?](https://software.codidact.com/posts/277486) Some thousands of beginners before you also made this "sensational" discovery when writing to an array out-of-bounds, because they were assuming that C would somehow be there to hold their hand even when they do things not supported by the language and possibly also not supported by the OS or hardware. – Lundin Jul 05 '22 at 06:20
  • Does this answer your question? [How dangerous is it to access an array out of bounds?](https://stackoverflow.com/questions/15646973/how-dangerous-is-it-to-access-an-array-out-of-bounds) – Gerhardh Jul 05 '22 at 07:07

3 Answers3

1

"And then I assign 58 to a[8] which is for experiment." And with that experiment you enter the dark realm of "undefined behaviour". All bets are off. Whatever your experiments show as result - you can ignore it. Read up on "nasal demons".

If you get a result which seems to indicate that accessing beyond the array somehow worked - then you can ignore that result. It is undefined behaviour and you did not find out anything, especially not that your compiler does some magic beyond what is promised with the array definition.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Yeah, I am aware of that undefined behavior but if it is actually true than it should produce undefined or unexpected result. Well, sometimes it may return with desired one but practically not. At least in my case after testing 4 devices or more and even in online compilers, Have you tried on yours? – Anup Adhikari Jul 05 '22 at 06:21
  • "... than it should produce undefined or unexpected result." a) No. Undefined behaviour can result in any behaviour, including results which can be confused with "expected" or "predictable" ones. b) You DID get an unexpected result, in being surprised by the fact that writing and reading beyond array did deceive you into thinking that it worked... c) You even found evidence that something did not work, by the "20" which clearly tells you that there is no space for what you tried – Yunnosch Jul 05 '22 at 06:25
  • Yunnosch, When you tested it over few devices than how can we call it undefined behavior as the result is 100% same and of course the same value which is previously assigned. My only regard is that if all the systems are printing exactly the same value up to few near blocks how can we say it is unexpected behavior ? And my other concern is that when something is 100% that what to argue on the matter? It is either **yes** or **no**, It can not be **yes** or **no** at the same time. And the last thing is none of the compiler warns me to prevent doing that. – Anup Adhikari Jul 05 '22 at 06:39
  • You can now consider yourself experienced with the more confusing parts of undefined behaviour. If you try it as often as you like, on as many environments as you like (which admittedly is a little hard to explain) you can still not publish a paper on how you found a great way of making more memory than hitherto was considered available. – Yunnosch Jul 05 '22 at 06:45
  • I am neither experience nor whatever you told but I am confused that if something is 100% certain, is it fair to tell still the matter is 50/50? (This may sound arrogant because I am speaking testing over 4-5 systems) It could be TRUE only in my case perhaps. Thanks for your suggestion. Well, can you try over your system? – Anup Adhikari Jul 05 '22 at 06:53
  • What is 100% certain? Who said 50/50? It is 100% certain that your code exhibits undefined behaviour, by definition, not by observation. There is no 50/50 there, not even 1/99. You encountered undefined behaviour with the sneaky, non-obvious kind of symptoms (aka "none"). It is still undefined behaviour, which is not the same as "unexpected" behaviour. The problem is that what you are doing does not have guaranteed behaviour AND does not guarantee that the observed behaviour will persist. And there is no benefit in insisting that it works. – Yunnosch Jul 05 '22 at 06:54
  • I am not questioning you on the note of 50/50 but if the code works perfectly fine on several devices than it is still undefined behavior? Again I am telling this may sound arrogant coz I have tried only on some devices, Will you mind testing on yours? – Anup Adhikari Jul 05 '22 at 06:58
  • Let's assume that I tried on the 500 very different environment which I happen to have access to and control over. What would you do if I told you that eveywhere I confirm your observation? It is still undefined behaviour and code using it is broken for being unmaintainable and unreadable for others. – Yunnosch Jul 05 '22 at 06:58
  • It is undefined behaviour. The best you can do with your code is to add a comment: `/* This is known to cause undefined behaviour. I (full name, phone number and company) tried and it works. */`. Then at least it is well documented and nobody reading it gets confused. – Yunnosch Jul 05 '22 at 07:02
  • Yunnosch, the example you exemplified of 500 differernt environments sounds logical but my only concern is that what probably may had happened with memory or compiler doing that and still providing the output which I had assigned. – Anup Adhikari Jul 05 '22 at 07:08
  • 1
    Ok the conclusion is **Undefined behavior** – Anup Adhikari Jul 05 '22 at 07:10
  • It is a tricky topic and hard to grasp. I hope I did not come over as cynical or in any way unkind. I tried different angles to help you come to this final conclusion. The one with the comment in particular is kind of a trap. You did notice that, I assume and hope. Let me know if any of my comments seemed inappropriate (they were not meant to be but your impression is important). I will happily delete those. – Yunnosch Jul 05 '22 at 07:36
  • 1
    @AnupAdhikari: Whether behavior is defined or undefined depends simply on one thing: Is there a statement from the producer of the software (or other product) that defines the behavior. If a compiler has a manual, and the manual states it conforms to the C standard (or at least largely conforms, with various exceptions) or that says something else about what the compiler does, and the behavior in some situation X is defined by the C standard or other things the manual says, then the behavior in situation X is defined. Otherwise, it is not… – Eric Postpischil Jul 05 '22 at 10:32
  • 1
    … It does not matter at all if you try situation X and you always get some particular result. That does not make the behavior defined. The behavior is defined if there is actually a definition for it: A document specifying what the behavior will be. When there is not a definition for the behavior, you might still be able to figure out what the program will do, because there is often nothing actively trying to make the program go haywire just because there is no definition, just as a badly designed building will not fall down just because it is badly designed;… – Eric Postpischil Jul 05 '22 at 10:35
  • … the building might stand for many years before some combination of aging material and poor design and storm-level wind forces cause it to fail. “Undefined” just means there is no specification of the behavior. It does not mean the behavior will be random or there will be “garbage” output or there will be a crash. It just means there is no promise about the behavior. – Eric Postpischil Jul 05 '22 at 10:36
  • Yunnosch, great but you need not delete them. It is just a part of conversation and quite funny, and may be I was arrogant too. – Anup Adhikari Jul 05 '22 at 16:52
  • @EricPostpischil Did you mean it is depended on the developers and compilers and their behavior? – Anup Adhikari Jul 05 '22 at 16:56
  • @AnupAdhikari: No. Stop. Back up. “Defined” means one thing: The behavior has been defined. It means the publisher/producer of the product (compiler, operating system, other software, computer, automobile, whatever) has specified (perhaps incompletely) what will happen in a particular situation. “Undefined” means one thing: The behavior has not been defined. It does not mean the behavior depends on the compiler or other things. That is all it means: It means the behavior has **not** been specified… – Eric Postpischil Jul 06 '22 at 02:06
  • … In fact, the behavior might be a function of what compiler is used or what compiler settings or used or what random number was chosen for the address base when loading the program or on other factors. But “undefined” does not mean any of that. “Undefined” does not tell you what the behavior depends on. “Undefined” merely tells you no statement has been made about the behavior. That is it. Nothing else. – Eric Postpischil Jul 06 '22 at 02:08
0

It is something like you book for 5 rooms in a hotel and try to access room number 8. It is lucky that this room is not use by any one so you could enter the room. However, if all the rooms are full, some complain (undesired behavior) will come. :)

ThongDT
  • 162
  • 7
  • Nice comparison. But there might be no complaint even if the hotel is out of rooms on a busy weekend. Because the room 8 might be avoided for reasons of quarantine after a horrible contactious desease festering there, or for the asbestos. No complaint. Not being thrown out. No detectable immediate problem. Still unwise. – Yunnosch Jul 05 '22 at 06:51
  • @ThongDT well hotel rooms comparison is absolutely fine. But how the room no. 8 could be empty is not busy over different hotels. Hence I am confused and so I posted despite being aware of the undefined behavior. I am just trying to know the reason behind different compilers providing same result with no any warn in spite of being **undefined behavior** and all. – Anup Adhikari Jul 05 '22 at 07:04
  • similar to the memory, that position might be empty or used by other application. – ThongDT Jul 05 '22 at 07:12
0

A simple example of slightly modified code around your example:

#include<stdio.h>
int main()
{
    int x = 23;
    int y = 24;
    int z = 25;
    int a[5];
    int p = 15;
    int q = 16;
    int r = 17;
    a[8]=58;
    printf("\t a8=%d snd size of A= %zd\n",a[8],sizeof(a));
    printf("\t x=%d, y=%d, z=%d, p=%d, q=%d, r=%d\n", x, y, z, p, q, r);
}

Before you try to compile and run this. What is your expected result?

Cheatah
  • 1,825
  • 2
  • 13
  • 21