Safe code is a skill, not a feature.

Oriel Jutty
link
fedilink
711M

Strictly speaking, it should be

Unsafe block syntax in C++

{  ...}
@lily33@lemm.ee
link
fedilink
44
edit-2
1M

That is why I use just int main(){...} without arguments instead.

@Bogus5553@lemm.ee
link
fedilink
33
edit-2
1M

Any void main(){...} enjoyers?

stebo
link
fedilink
81M

besides not requiring a return value, what difference does it make?

Oriel Jutty
link
fedilink
91M

@stebo02 @Bogus5553 Neither of them require a return value, but void main isn’t legal C++.

stebo
link
fedilink
21M

yeah I thought so, does it work in C?

@Bogus5553@lemm.ee
link
fedilink
4
edit-2
1M

void main(){...} is not in the standard, but works on both MSVC and GCC (with warnings). I think it works on both C/C++, but you really shouldn’t use it in production. Just use int main(void){...} , without any return value, which is permitted in the standard, and will return success iirc.

It will also give an error if you try to add a return value anyways.

int main(void) { . . . } for me!

while (true) {...}

Very true, the less user input you have the safer your code will be.

@merthyr1831@lemmy.ml
link
fedilink
English
31M

counterpoint: all code is unsafe. retvrn to abacuses

Ziglin (they/them)
link
fedilink
English
31M

But I was hit over the head with one, that wasn’t safe either!

i will never forgive C for making the type syntax be

char* args[]

instead of the much more reasonable

&[char] args 

it also bothers me that char* args[] and char c are “the same type” in the sense that the compiler lets you write

char c, *args[5];

with no problems. i think the C languages would be way easier to learn if they had better type syntax. don’t even get me started on C++ adding support for

auto fn_name() -> ReturnType { … }
Oriel Jutty
link
fedilink
61M

@affiliate Hey, you didn’t even mention that char *args[] actually means char **args in a parameter list.

god, what a beautiful language. it brings a tear to my eye

I personally think that C++ can be beautiful. For example: std::filesystem::path overrides the / operator, for specifying parent paths. It’s the same as Kotlin’s OKIO and Pythons standard pathlib.

It could, but not necessarily.

char **args can just mean you have a pointer which points to an address, and at that address, you can get a second address. Follow the second address, there is a char saved there.

On the other hand, char *args[] means " follow this address to find a list of characters".

Oriel Jutty
link
fedilink
21M

@racketlauncher831 As far as the C compiler is concerned, there is literally no difference between those two notations. If you declare a function parameter as an array (of T), the C compiler automatically strips the size information (if any) and changes the type to pointer (to T).

(And if we’re talking humans, then char *args[] does not mean “follow this address to find a list of characters” because that’s the syntax for “array of pointers”, not “pointer to array”.)

Create a post

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
  • 0 users online
  • 22 users / day
  • 57 users / week
  • 283 users / month
  • 1.97K users / 6 months
  • 1 subscriber
  • 1.72K Posts
  • 37.4K Comments
  • Modlog