LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2022, 07:55 PM   #1
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432

Rep: Reputation: 110Reputation: 110
C array creates a mysterious value


Note: total newbie learning C here.

Code:
int myNumbers[] = {25, 50, 75, 100};
myNumbers[5] = 500;
printf("%d\n", myNumbers[0]);
printf("%d\n", myNumbers[1]);
printf("%d\n", myNumbers[2]);
printf("%d\n", myNumbers[3]);
printf("%d\n", myNumbers[4]);
printf("%d\n", myNumbers[5]);

25
50
75
100
6
500
Where did that 6 come from?

I never created myNumbers[4] and I wanted to see what would happen when I called it. That 6 came up.

Why 6?
 
Old 09-07-2022, 08:03 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,220

Rep: Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319
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.

TLDR: UB

Last edited by dugan; 09-07-2022 at 08:08 PM.
 
1 members found this post helpful.
Old 09-07-2022, 09:13 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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!

Last edited by astrogeek; 09-08-2022 at 12:24 AM.
 
Old 09-07-2022, 11:59 PM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by lucmove View Post
Note: total newbie learning C here.

Code:
int myNumbers[] = {25, 50, 75, 100};
myNumbers[5] = 500;
printf("%d\n", myNumbers[0]);
printf("%d\n", myNumbers[1]);
printf("%d\n", myNumbers[2]);
printf("%d\n", myNumbers[3]);
printf("%d\n", myNumbers[4]);
printf("%d\n", myNumbers[5]);

25
50
75
100
6
500
Where did that 6 come from?

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).
 
Old 09-08-2022, 11:08 AM   #5
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432

Original Poster
Rep: Reputation: 110Reputation: 110
Thank you for the replies.

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.
 
Old 09-08-2022, 11:34 AM   #6
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 998

Rep: Reputation: 470Reputation: 470Reputation: 470Reputation: 470Reputation: 470
C does not check array bounds because it was designed as a replacement for assembly language.

You can use many other languages if you don't want a replacement for assembler.
Ed
 
Old 09-08-2022, 11:55 AM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by lucmove View Post
Thank you for the replies.

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
 
Old 09-08-2022, 12:12 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,694

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
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.
 
Old 09-08-2022, 12:21 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.
 
Old 09-08-2022, 12:23 PM   #10
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432

Original Poster
Rep: Reputation: 110Reputation: 110
OK, I get it.

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.
 
Old 09-08-2022, 12:34 PM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.
 
1 members found this post helpful.
Old 09-08-2022, 12:36 PM   #12
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by lucmove View Post
OK, I get it.

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
 
Old 09-08-2022, 12:49 PM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,694

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
One of the first lessons learned was to always initialize all variables.
 
Old 09-08-2022, 01:07 PM   #14
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,432

Original Poster
Rep: Reputation: 110Reputation: 110
Quote:
Originally Posted by NevemTeve View Post
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.
Isn't C compiled? I think I'm using a compiler.
 
Old 09-08-2022, 01:17 PM   #15
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by lucmove View Post
Isn't C compiled? I think I'm using a compiler.
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.

Last edited by astrogeek; 09-08-2022 at 01:21 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Assigning a value to a variable also creates an empty file named as the value in bash StorageDon Linux - Newbie 3 11-01-2016 08:52 PM
strange value assignments variable = value, value?? ostrow30 Programming 2 07-24-2011 07:59 AM
difference between value *value and value * value PoleStar Linux - Newbie 1 11-26-2010 03:37 PM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM
LXer: Piracy creates jobs, FOSS creates opportunities LXer Syndicated Linux News 0 11-02-2006 11:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration