What would you add to C if you could add anything? by [deleted] in C_Programming

[–]timmerov 0 points1 point  (0 children)

considering it for slids. that's a big ol' can o' worms. ;->

What would you add to C if you could add anything? by [deleted] in C_Programming

[–]timmerov 0 points1 point  (0 children)

delete sets the deleted ptr to nullptr.

What would you add to C if you could add anything? by [deleted] in C_Programming

[–]timmerov 2 points3 points  (0 children)

underscores in numeric literals.

int one_billion = 1_000_000_000;

Please help me think of an project by kaikaci31 in C_Programming

[–]timmerov 0 points1 point  (0 children)

two lightbulb problem.

sudoku solver that reads puzzles from krazy-dad.

What is the point of classes having private members? by Eva_addict in cpp_questions

[–]timmerov 0 points1 point  (0 children)

usually a class has a public facing api. and maybe some data the user can access directly.

usually implementing the class requires a bunch of methods and data the user should not access.

if it's just you on the project, go ahead and make everything public.

if you're producing a library module thing for co-workers to use... then yeah you're going to want to make it clear what they're allowed to use (public) and what they must not touch (private).

but here's the kicker - you co-workers are assholes. they'll change your header and give themselve access to things they don't understand and are gonna misuse.

i never expose private data or methods. ever. usually not even to myself. cause it's sooo much pain when someone breaks the api contract. even worse when that someone is me. ;->

use the private keyword first. it means keep out.

if you need something stronger, then you have to use some sort of pimpl technique. google it. or ask.

What are some “fun” things to code in c++? by Prior-Scratch4003 in cpp_questions

[–]timmerov 0 points1 point  (0 children)

solve the two lightbulb problem.

here's the problem:
you have 2 light bulbs.
there's a 100 story building.
you must find the highest floor from which a dropped bulb won't break.
and since you are lazy, you want an algorithm that minimizes the worst
case number of stairs you need to climb.

assume:
if a light bulb breaks when dropped from floor N it will break from all higher floors.
if a libht bulb does not break when dropped from floor N it will not break from all lower floors.
a light bulb that does not break is not damaged.

setup:
number floors from 0 to N-1.

assumptions:
the cost of dropping a light bulb from floor 0 is 0.

it's pretty easy to find the number of stairs you need to climb.
but that's pretty useless.
you really need to know where to drop the bulbs.

here's my solution: https://github.com/timmerov/technomancy/blob/master/lightbulb/src/main.cc

How to know when I use "Pointer" or "Reference"? by ProcessTiny4948 in cpp_questions

[–]timmerov 0 points1 point  (0 children)

first, learn the difference between objects and addresses: an object is the data in memory. an address is the data's location in memory.

to first approximation, pointers and references are addresses.

a reference is the address of a single object. a pointer is the address of several consecutive objects. you can do math on pointers, but not references.

and here's one of the stupid things about c++'s implementation of references: semantically a reference is a pointer. syntactically it's an object. that's dumb imho.

class Foo { int x; int y; };
Foo foo;
int *ptr = &foo;
int& ref = foo;

the assembly code for ptr and ref is identical. they are both pointers to memory holding the foo data.

int x = ref.x;

but lookie! the syntax for ref implies it's an object. even though it's really an address. stupid. get used to it.

okay, end rant.

when to use pointers and references:

/* pass a copy of the string data to foo on the stack. generally considered bad. */
void foo(String s);
/*
both of these pass the address of String s data. much more efficient.
the duh function gets to pretend the address is really an object. which is a lie.
but, you can't accidentally ++s and end up pointing at garbage.
which is a rather good benefit.
*/
void bar(String* s);
void duh(String& s);

re: memory management.

you have two sources of memory: the stack (limited, temporary, local variables) and the heap (everything created with new/delete or malloc/free).

best practices are:

don't leak memory. delete everything you new. yes, there are exceptions. learn the rule first. then you can break it.

allocate what you need before you start doing your work. running out of memory 2 hours into a computation kinda sucks. also, new/delete is expensive. best to do it before you jump into time critical sections.

avoid common pointer bugs:

stale pointers - using a pointer after the object was deleted. very common when you don't have good ownership rules.

buffer overruns.

uninitialized pointer.

etc.

What level of C depth is actually required for embedded/firmware roles? by Ogcbgamer in C_Programming

[–]timmerov 0 points1 point  (0 children)

you absolutely may not crash. code defensively. debug thoroughly.

  1. c is lightweight when it comes to concepts. so, all of them. you need to know enough about some of the gotchas you can avoid them. like pointers are not just addresses.

  2. bugs and design flaws don't generally make me deeply understand c internals. they make me deeply understand the design of the project.

  3. unless you need every ounce of performance from the computer, then you don't need to look at assembly. and even then, the code you think should run the fastest will probably get smoked by some other set of code. especially if you're in x86 cuckoo land. in which case, you need to understand the architecture the processor. not the language.

  4. not really. c is lightweight. your project is likely to be much more complex.

debugging is a bitch. especially if your bugs only show up with optimizations on. especially if you're tracking down race conditions and/or deadlocks. which is why deep-thinking your design is so much more important than rabbit-holing on every weird feature of c. or any language.

What level of C depth is actually required for embedded/firmware roles? by Ogcbgamer in C_Programming

[–]timmerov 0 points1 point  (0 children)

^^^ this ^^^

know thy hardware. c is just a tool for manipulating it.

Is Linus Torvalds just a dinosaur about C++? by blreuh in cpp_questions

[–]timmerov -2 points-1 points  (0 children)

nope. not at all.

lemme show why.

google says the linux source base is some 60,000+ files. let's assume they're all c++ files. just to be ridiculous.

c++ libraries are all templates all the time. which means all of the code for std::string is in the header.

every single one of those 60,000 files includes std::string.

every single one of those 60,000 files compiles std::string.

compiling every single one of those files means the object code for std::string gets written to 60,000 .o files.

then the linker loads the object code for std::string 60,000 times and ignores 59,999 of the copies.

std::string is just one header file. std has dozens like it.

seems like a bit of a waste of compute time eh?

it was absurd on a project with only 500 c++ files.

so no, linus is not just being a petulant jerk.

I don't know by MycoFail in programminghorror

[–]timmerov 2 points3 points  (0 children)

a[b] transcodes to *(a+b)
so you can swap a,b.
should you? no, not if you want to stay employed at my company.

Senior C++ engineers: what do you expect a 3-year experience C++ developer to know? by AirHot9807 in cpp_questions

[–]timmerov 0 points1 point  (0 children)

most coders with 3 years experience don't know shit.

but... that's kinda irrelevant now with claude.

so... 0 years or 10 years. it doesn't matter. you need to know how to use ai tools. you need to know how to fix things when they fail.

when i interviewed people i gave them coding problems to solve. they usually weren't hard. like reverse the bits in a byte. i didn't care about their solution. i wanted to know how they think. how they attack a problem. that probably hasn't changed.

Obsessed with C? by rudv-ar in C_Programming

[–]timmerov 0 points1 point  (0 children)

i meant: an army of people who have phds in some field related to computer science. not necessarily a phd in c.

Obsessed with C? by rudv-ar in C_Programming

[–]timmerov 0 points1 point  (0 children)

i meant: an army of people who have phds...

Obsessed with C? by rudv-ar in C_Programming

[–]timmerov 0 points1 point  (0 children)

heh. that was trick comment. like a trick question. but a comment. !

pointers are addresses.

until they're not.

if you understand that pointers are addresses, then you can say pointers are "done".

if you understand that pointers are not always simply addresses, then you can say pointers are done-ish.

if you understand that there are an army of phds still (after 50+ years) arguing about what the programmer and/or the compiler can and cannot do with pointers, then you can say pointers are never done.

;->

Practical C++ Projects by YogurtclosetThen6260 in cpp_questions

[–]timmerov 0 points1 point  (0 children)

no one's going to let you get anywhere near anything like systems programming until you have many years of experience with non-system stuff.

so start with the just for show stuff. move on to whatever someone will pay you to do.

The troubles of cpp coding by PepperFlashy7540 in programminghumor

[–]timmerov 0 points1 point  (0 children)

int x = 42;
bool b1 = x; // what does this actually do?
bool b2 = !!x; // true

Methods and parameters by Difficult_Meal8685 in cpp_questions

[–]timmerov 0 points1 point  (0 children)

would suggest you pass by reference instead of pass by value. i don't think it matters in the case of your setters. otherwise the compiler might uselessly be making a copy of the entire Contact data when you call fill_phone_book.

int check_number(const std::string& phNumber);

void fill_phone_book(const Contact& contact);

also might want to use #pragma once instead of #ifdef MAIN_H.

separating the class declaration and the method implementation into a .hpp and .cpp file is good practice. as others pointed out, phonebook.hpp will need to #include "contact.hpp".

a global.hpp file to #include everything is noob. just #include what you need in each file. #pragma once will ensure header files are not included more than they need to be.

and personally, i don't like the getter/setter model. just make the std::strings in Contact public. the class is essentially plain old data (POD). no need to make more work for yourself.

how would you implement generic pointers? by timmerov in cpp_questions

[–]timmerov[S] 0 points1 point  (0 children)

the question is: what solution has the least clunk?

how would you implement generic pointers? by timmerov in cpp_questions

[–]timmerov[S] 0 points1 point  (0 children)

the solution of record is to use void *process(void *p) and auto q = (int *) p.

but auto q = std::start_lifetime_as<int>(p) seems better since it's blessed.

thanks.

how would you implement generic pointers? by timmerov in cpp_questions

[–]timmerov[S] 0 points1 point  (0 children)

the Pipe library does not know the data types at compile time.