coding in c until my program crashes

preview_player
Показать описание
C PROGRAMMING IS HARD! CODING IN C SAFELY IS HARDER! Software development has never been like this before. Why can't C use C++ references D:

Subscribe :D

🏫 COURSES 🏫

🔥 SOCIALS 🔥

Рекомендации по теме
Комментарии
Автор

To be fair to the compiler, it did warn you when you passed the integer to an argument which should a pointer.

ShrirajHegde
Автор

Ah yes, I know this error. I usually just add & and * symbols at random places until it starts working somehow... If not, just hardcode the input arguments, works 100% every time. No need to thank me, happy to help!

ffoska
Автор

If someone not expert in C does not know what went wrong:
The error was in the scanf, since he needed to pass a pointer in the second argument but he passed a value, the value of x, instead of a pointer to x, which is &x. His compiler actually threw a warning... That's why you should take warning seriously.

The technical explanation of why SEGFAULT happened (access to a protected direction of memory) is because the scanf treated the second arg as a pointer and tried to write its read value into an unknown position of memory (unknown because x was not initialized). Variables in C when are not initialized can have trash values, values that in this case have been treated as a literal position of memory; and the most probable is that a trash value points to a protected position of memory, and when scanf tries to return what it read, it writes into a protected position of memory, which makes the OS throw a SEGFAULT, signaling that a process has tried to access memory positions outside of its memory segment

snnnBatangreet
Автор

didn't add the & to x in the scanf, it receives an address there not an actual int, you either declare it as a pointer and pass it as is or add an ampersand.

PASTRAMIKick
Автор

I just want to appreciate all kind c programmers in comments who explained what was wrong to us non c programmers 😵‍💫

tesla
Автор

As one man once said,
"I fear nothing. But when I hear 'C' and 'strings' in one sentence, it scares me."

konstantinsotov
Автор

Scanf needs to read to the _address_ of the variable. The prototype takes in a pointer. What happened is whatever leftover garbage value x was (part of why you should initialize your variables) was used as an address & scanf stored the value there. Since that’s not memory you either allocated or declared for a variable, it’s out-of-bounds, hence segfault.

NinjaRunningWild
Автор

Here we have the prophesied C code that will read an integer from the user and print it back to them without causing a segfault. For the non C programmers the mistake that caused the segfault in the video was not reading the value into the address of the integer variable x in this case.




#include <stdio.h>

int main()
{
int x;
scanf("%d", &x);
printf("%d\n", x);
return 0;
}

jeromesimms
Автор

I turn on -Wall -Wextra -Werror, so that I never ignore any warnings. But even with that, it still didn't prevent me from accidentally putting & while doing memset and corrupt the memory lol.

At least it's better than TempleOS HolyC experience. It performs no type check, so that you can mistakenly put -> and have fun debugging times.

yjk_music
Автор

PerfectCorp writes perfect software. When a dev makes a mistake he gets punched. One guy almost died but now he's flawless.

perfectionbox
Автор

i love this coding until series. want moreeee :)

prabeshsapkota
Автор

Just make x = 3. Then print out x and have the program wait for the end-user to press any key to exit.

SHONNER
Автор

The problem with pointers is worse when you are working with OpenMP and MPI. What a nightmare.

Deltosio
Автор

"We will erase your hard drive"

* uses a virtual machine *

_GhostMiner
Автор

This is hilarious.

For real: I've only ever had fun coding C and Rust. I liked Javascript and Python, but damn was learning C & Rust fun.

defnlife
Автор

As someone who practiced programming with C# I have no clue what just happened

thatdudenamedkevin
Автор

Just printf(argv[1]) // TODO: tell users not to pass anything fishy

There's no mention in the assignment that the number cannot be passed via argument list ; )
Plus it works with numbers, complex numbers, boolean values, strings, even expressions!

sprytnychomik
Автор

This is entertaining "&" educational!

cipherxen
Автор

I really feel irritated when someone unnecessarily passes argc and argv into main. Idk it kinda became an obsession, just don't pass the goddamn thing.

Oh, wait there is more...
int main(void)

kerim
Автор

Correct missing & scanf was trying to access address 3, as you passed the contents of x.

steveokinevo