The Lifetime of Automatic, Stack Variables in the C Programming Language

  Рет қаралды 2,696

Kris Jordan

Kris Jordan

Күн бұрын

Пікірлер: 8
@gloverelaxis
@gloverelaxis 2 жыл бұрын
"insane and pathological" is right on the money, haha. every single new thing I learn about C makes my jaw drop with horror
@silveradd4977
@silveradd4977 2 жыл бұрын
Hi Kris, very nice and in-depth tutorials on system programming. I have a question though: Using cc 6.3.0 on debian stretch, it seems the program selects two different addresses for both i and j, eventhough their lifetime has expired. Do you have any idea why this happens?
@silveradd4977
@silveradd4977 2 жыл бұрын
I am talking about the third example @ minute 26:58 where i and j are allocated inside two separated scopes within main
@johnnyappleprng614
@johnnyappleprng614 3 жыл бұрын
In the code around minute 21 ... try changing the number of elements in z ... and the output will be different than expected. When I copied the code Kris wrote verbatim... it produced: *a: 211 *a: 92 When I added an extra element to the end of z array, then the output was as Kris said it should be with the last element in z being written on the second line of output. Why? I am running debian 10 and using gcc with the basic default settings if that helps.
@eis3nheim
@eis3nheim 2 жыл бұрын
I know you probably knew the answer by know, nevertheless the answer to your question is this is an *undefined behaviour* you can't expect everytime to replicate the result, for instance your OS could have written some value to that location when it became available, even for Kris's code behaviour it is not guaranteed to produce the same result on different machines.
@johnmorrison2645
@johnmorrison2645 2 жыл бұрын
My Mac M1 did this: MAC:Tue Sep 06:16:54:~> ./scopes i = 0 &i = 0x16fad7604 j = 1 &j = 0x16fad7600 *p = 0
@shanghengwu8254
@shanghengwu8254 Жыл бұрын
I also got the same result on my Mac, do you know the reason?
@ncmathsadist
@ncmathsadist Жыл бұрын
@@shanghengwu8254 Likely M1 chip architecture.
Static Memory and Static Lifetimes in the C Programming Language
46:00
REAL 3D brush can draw grass Life Hack #shorts #lifehacks
00:42
MrMaximus
Рет қаралды 4,8 МЛН
My Daughter's Dumplings Are Filled With Coins #funny #cute #comedy
00:18
Funny daughter's daily life
Рет қаралды 27 МЛН
Life hack 😂 Watermelon magic box! #shorts by Leisi Crazy
00:17
Leisi Crazy
Рет қаралды 79 МЛН
Call Stack, Scope & Lifetime of Variables
16:51
ReelLearning
Рет қаралды 49 М.
CppCon 2014: Mike Acton "Data-Oriented Design and C++"
1:27:46
Understanding Ownership in Rust
25:30
Let's Get Rusty
Рет қаралды 260 М.
range in Python - Representing an immutable sequence of integers!
25:35
Summation Algorithm with Lists and a Loops - COMP110
27:33
Kris Jordan
Рет қаралды 2,8 М.
The Call Stack and Stack Overflows (example in C)
12:56
Jacob Sorber
Рет қаралды 45 М.
REAL 3D brush can draw grass Life Hack #shorts #lifehacks
00:42
MrMaximus
Рет қаралды 4,8 МЛН