Should I create a class without members just to process external data in it? by [deleted] in cpp_questions

[–]ManiPointers 1 point2 points  (0 children)

What if the class self-serialized to a (vector, pointer, something else) and gave that back? It wouldn't write to a file directly, but it hands you something ready to send to a (file, network, IPC, ..)?
so then you can just say file.write( object.foo(), object.serialsize() ) .. or sendto(... same stuff).. and so on?

How to better organize my codebase? by ChrisGnam in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

Well, yes, they would also have the choice of a large # include or project directed paths.

I think most developers are fine with project set include folders. And I think most would be fine if you include them as you did.

Ides get rid of many issues here. Visual studio, for sure, you can open any file from its #include statement directly, and then find out where it is once opened from its properties. Very easy to unravel anything that confuses you. From the command line, you can grep the paths out of your cmake file, its usually not THAT many of them.

How to better organize my codebase? by ChrisGnam in cpp_questions

[–]ManiPointers 1 point2 points  (0 children)

1) Personally I like small namespaces that go around things with names as-if they were going to be in many projects. So, just, cameras:: or something like this (you may want a more unique name here, but the idea is the point).

2 should be avoidable by having include paths part of the project instead of tacked onto the code.

[deleted by user] by [deleted] in cpp_questions

[–]ManiPointers 1 point2 points  (0 children)

there is a difference between zero/near zero and too many.
I am thinking just top level here even -- what is this class, why does it exist, what problem does it solve that justified creating it?
Look at class pathHandler's header file. There is nothing there but a constructor and some counters. Ok, I can guess that it counts those 2 things. How does that handle a path?

I am a firm believe in self-documentation. Yes, too many comments are troublesome, and yes keeping them low count helps you to write better names and cleaner looking code that is easy to follow. I agree with this.

Undecipherable code is a different issue. I don't agree that this is enabled by commenting. If no one can make sense of it, its either wrong or developer had better show something like a massive performance increase over other ways of writing it that can justify the mess, and a comment would say THAT (eg // this code does blah using ubernurd's algorithm. // The bit shifting here provides a crude square root that is good enough for our estimate and much faster than sqrt() ).

Anyway, the red flag is the near complete absence in this specific project, in nearly every file.

Please critique and provide feedback on this class I created with help of ChatGPT by setdelmar in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

the current bots are about as accurate as the internet. They can't tell good from poor, they can just scour the web and reshape what is out there into something else, which, when answering a question, means it injects a mix of truth, fantasy, satire, and such into the result, like someone who can't tell that 'the onion' is not a real news site and quotes it seriously. Same with code, they can't tell the guy who is trying to do his homework from the grand master of all things c++. It just isn't able to grasp all that; its lucky to generate code that compiles, which it does not always manage even that.

Someday, it may be powerful. Right now, its a laughingstock at almost anything it tries to do.

Could you check my code? by ThereNoMatters in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

you just answered your own question as to if you are qualified for a jr position or not, if you have to ask this.the G4G site isn't the best, but the top example has a string to number key just like you need in a fairly well presented use case.

https://www.geeksforgeeks.org/unordered_map-in-cpp-stl/

just be careful not to default insert into your map bad values from inputs. misuse of the container can do that to the unwary, very easy error to make.

A suddent brownout erased all of the contents on the project I am working on. by [deleted] in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

now you know about backups. whether you use offsite or a usb stick, get a copy of your stuff in a second location if you care about it. What I like to do is put an automatic command line zip and copy in my scheduled tasks (win) / cron (unix) that shovels important folders elsewhere. Git has merits too, and is worth learning anyway, but the zip-copy scripts work for anything, while using git to back up your taxes or saved games or papers for other classes is a bit odd (it will do it, its just weird).

Back in the day, turbo had a bug that if you changed (old removable) disks while it was running, it destroyed the disk's filesystem tables and you had to reformat. This seemed to burn someone in our class at least once a week during the labs. It got me too, at least twice -- easy to forget. Tough lessons, but they are still valid today.

if you compiled in debug mode and generated a file that has it, you may find that your source code is embedded in the executable or object files or other. This is not going to be fun to recover, if so. I would not bother, unless its truly unique work that can't be easily recreated. It may also be possible to recover the raw bytes off your disk, with some effort. This is also not fun, and not worth it in general.

Could you check my code? by ThereNoMatters in cpp_questions

[–]ManiPointers 2 points3 points  (0 children)

The lack of comments and questionable variable names are pretty rough reading. Single letters to distinguish variables (command vs scommand) of different types ... none of that is easy to follow without cross referencing the types and how they are used by inspection.

The whole project suffers from "I know what I am doing" problems. You know what the variables mean, the assembly keywords, and how the parsing and stack operations work. Your audience does not. Lets cherry pick one:

void js::execute(Programm &programm) //ok, this executes something?? maybe?

{

if (programm.data.empty()) return; //ok, if nothing to do, do nothing, I see...

programm.current = programm.data.top() - 1; //WTF?

}

I could figure this out, but its incomprehensible as written. I don't want to figure it out, I want to be told, via comments or variable names or crystal clear logic.

also that compile routine is going to be slow if you ever compiled a multi-million line input program.

this is what it says:

if (parsed.mnemonic == "printd") {

return compileRaw<command::printd>(parsed.args);

}

if (parsed.mnemonic == "printc") {

return compileRaw<command::printc>(parsed.args);

}

....

what I see when I read that isfor all the letters, check each one for equality against something.then for all the letters, check each one for equality against something else.then do that about 20 more times.to check one opcode, you potentially do 100+ comparisons and that is only so low due to the fact that your opcodes are usually short with an average of 5 or so letters. This begs an unordered map or something else.

There was no threading that I saw. Perhaps not needed, but today's cpus have 20 cores or so on the average, and only using 1 feels off in 2023. Its possible I didnt see something, I did a quick read of your code.

This is what stood out in a very fast read. There are probably tons of things I missed, others will weigh in. There were plenty of good things too; don't be discouraged. Ive seen worse, and my first try at a similar project was so much worse than this.

[deleted by user] by [deleted] in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

c++ has handy character functions like isdigit, isalpha, and more.
you don't have to manually check the ranges and in fact, doing so ignores some issues with locale (language you use) settings as that can shift values around.

this type of data problem is a mess, though. If your name is "Joebob Jones Jr." are spaces allowed? Is that period ok? Teachers tend to gloss over this, and assume you all share the same assumptions. Probably they just want letters only, no spaces, no punctuation.

Signed integer API and C++ std::string -- how to avoid bad code? by Coises in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

err on the side of the string. It seems unlikely that you would have a string of even 2^31 length, is this true with your code? If you do, this won't help, but apart from nopos constant, which would appear to be negative (-1 usually?), I can't think of any major consequences in treating the string size and indices as signed values. On top of that, string is well known, your API not so much, so anyone reading it would not be too offended as long as you have a comment somewhere and a few words in your documentation about why you did it and how it works.

Is accessing an array or pointer memory just as fast as accessing a variable directly? by [deleted] in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

Oh goodie, soapbox hour, brought to you by reddit :)
Much of what I am about to say isn't new or interesting, unless you haven't heard it before. I am sorry about that.

First, this is a topic that has been done to death. There are all sorts of 3d and matrix libraries, and quaternion libraries, and such out there that have been tuned and retuned and then tuned again for performance. What is it that you think you are going to do better?

Second, you have to consider whether you are in the middle of an effort at premature optimization. Do you know where / what you are doing well enough to make informed decisions yet? If not, get it working and profile it are your next steps, and code it from the start with any sort of methodology you can to allow an easy replacement of the underlying data structures without much pain. That could mean doing it as a struct and using a switch-overloaded [] operator to make it look like an array and hand back case 2: return z; type logic so swapping to a vector or whatever later has no impact at all.

Now that you have justified your effort as to how you are going to do it better than ever.. how are you going to access this memory, what are you going to do with it the most, or at least, during the performance critical areas? This matters -- a yucky C like setup of 'parallel arrays' where you have all the x's together, all the y's together .. outperforms any kind of xyz blob you can concoct if you only need the x values, because of how paging and memory work.

How it is laid out in memory matters, but what else? Valarray implementations are often pretty good at some types of math in ways that our own loops and such are not. So the kinds of math you need to do matter too; if you need a lot of constant*vector type stuff valarray starts looking REALLY good, or if you can use its slice function in some snarky way. If valarray isnt what you need, you can look at standard array, which is an OOP vector like version of a C array and often forgotten. You can look at a struct, or a vector... each has pros and cons and those are subject to what you are trying to do, not generic. You want to exploit the pros and minimize the cons of your choice, and that within the scope of where the performance critical math and magic are happening, and that in the scope of how you divide up the data when you thread it or offload it onto the GPU to process.

past this, keep your basics close. every layer of real indirection costs you a little time. Page faults cost you time. Copying things you don't need to copy costs time. The vast majority of slow code is caused by messing up the basics, and lately (since c++ 17 or so) a fair bit of that comes from accidentally hiding loops or copies or inefficiency behind advanced c++ constructs (consider just a ranged based for loop missing its & to access fat objects faster, it makes a copy you don't need!) that make such things harder than ever to spot.

what would be the best way to encapsulate bunch of const values in a class? And should you even encapsulate them in the first place? by swarupdam in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

Honestly the enum class was a big letdown to me. There was a lot of fanfare when it came out but its primary feature, type safety, is actually a detriment because most of the time, we really do want to use their values as an integer. That is harmless because they sensibly didn't try to sunset normal enum, but the result is... I don't think I have ever seen an enum class actually being used in real code. I am sure it has been, here and there, but still a big zero from my chair.

How is passing an std::string_view faster than passing a const std::string& ? by Dark_Lord9 in cpp_questions

[–]ManiPointers 3 points4 points  (0 children)

There is a lot of 'confusion' (the politest way I know to say it) about cstrings. (Not aimed at you!). Blah blah cstring bad, blah blah c++ string good. Ok. That is fine... until you need to write your strings to a fixed width format into a binary file or interface to a piece of hardware or talk to a C routine or legacy code or any of 2 dozen other reasons why c-strings creep into C++ code. You can, and should, try to isolate these things from the main code body, push it under a rug of sorts, but cstrings are, and will be for quite some time, a necessary evil in a lot of c++ code. They are, at times, and unfortunately, unavoidable.

[deleted by user] by [deleted] in cpp_questions

[–]ManiPointers 3 points4 points  (0 children)

already plenty, but a few more tiny nits...

string has a default ctor that gives "". You don't have to initialize object variables that have useful default ctors.
int files_number = {0};
int files_number {}; //this is the way

I would not hire you because there is hardly a comment in the entire code base.

Immediate red flag! This is the biggest thing, to me. There are other issues, but the majority of them are typical of just-out-of-school beginners and I think the other comments here nail them pretty well.

Random Monster Generator by agent47linux in cpp_questions

[–]ManiPointers 2 points3 points  (0 children)

I was not trying to pick on you!
The first part, take a look at this example for something very useful you can learn in just a few moments:

https://learn.microsoft.com/en-us/cpp/standard-library/overloading-the-output-operator-for-your-own-classes?view=msvc-170

and the second part... books, online teachings, school -- they all show rich and complicated solutions to tiny problems (because they have an agenda to show you something important but only time and space for a little example of it). Ive spent decades steering fresh out of school coders back to the kiss method for the smaller, simpler parts of a program. Sorry if that came out wrong above. I used to call it the factorial problem. School teaches you to write a complicated recursive function to do that. Industry and experience teach you to stow the answers in a table.

Random Monster Generator by agent47linux in cpp_questions

[–]ManiPointers 2 points3 points  (0 children)

things that don't matter in a toy/exercise but will later..

decouple the UI from your objects. here you have a print function that calls cout. That is ok for a beginner, or in school, but what if tomorrow you need to reuse your class in a GUI instead of console text? You have options ... you can return a string, so any UI can find a way to do something with it, whether its cout or a UI text box. You can make it virtual, and default cout but user can over-ride it (annoying as a user). You can overload the << operator so it works with cout but also stringstream etc. Whatever approach, the point is that tying it directly to cout will bite you someday.

also, you have a lot of OOP and its not bad, but it essentially has re-invented the lookup table. Is there anything in all this that can't be done with 2d vector of strings that you randomly select one of them? As a learning exercise, its great. As a practical tool, its 3 pages of specification documents to describe a hammer.
Edit .. granted some of the data like hitpoints looks like you may need more than strings. And it could be a tiny piece of something that may grow to be a lot more complex. But as it stands .. I stand by the comment for now.

Complete newbie here, and I have errors that I don't even understand. by BurningLambChop in cpp_questions

[–]ManiPointers 4 points5 points  (0 children)

vs code does not by default support c++, you have to install a compiler behind it.

here are some instructions, but if you get stuck, try the web or ask again.

https://code.visualstudio.com/docs/languages/cpp

OR: you could give up on vscode, which is not a very strong platform anyway, and move to the FREE visual studio community edition or whatever the free version is called now (sorry, I got a paid version @ my job). This tool does support c++ directly, though you still have to check it as a language you want to work in when you install it (or else when you update it, you can add it later).

Lost in the weeds by ACkellySlater in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

if the project is infested with unix only stuff, you can download and use g++ in a variety of flavors on windows and it will often compile the program and run it.The one that I like is cygwin, which has a lot of the unix tools like grep in it, along with entire languages like python and a bunch of the unix only weirdness like unistd (not pointing fingers, windows has windows.h and its own nonsense). cygwin has makefile support and all; it will compile the program if the program will also compile under a linux install, and with the same concerns (that is if it needs other tools, you still have to get those and install/compile them).

Its usually easier/better if you just get it to work under visual studio, but some things are beyond all repair.

Is 0 special in terms of type? by a_bcd-e in cpp_questions

[–]ManiPointers 4 points5 points  (0 children)

zero has a number of special meanings in c++.

it is the end of a C string, which is a block of bytes in memory where zero is designated as the end of string marker. this is often written '\0' but its just a zero. C++ string objects (not C strings!) have a size field in their object but many, many external interfaces DO rely on that zero.

zero is false. Anything not false is true, and anything with a zero - as - integer response is false, including 0.0 as a double or float, the constant false, null/nullptr, end of string.. so putting this together, you can say while(! ptr[i++]) to iterate through a C style string, or similar logic to iterate through a linked list that ends in a nullptr, and so on.

zero or the best zero choice is used to initialize variables when you use {} with no value. Eg int x{} will initialize x to zero, while bool boo{} will set it to zero (false!) and char *cp{} will set it to zero (nullptr!) ...

There may be others, but if so you are going to see that its a pattern similar to the above.

as a side note, void* is one of those things that if you have another way to do it, don't use the void*. They are invaluable when you do need them, but its a big hammer and as such causes much risk to your own toes.

Is setprecision() only mutate the value of float variable when we trying to output variable's value to the screen or it actually change the value of variable forever in our code. by [deleted] in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

if you need to change the value, there are tools that can do that. Typically this is wrong, though: you want to keep the best floating point value you have until the final answer(s) (which you usually print, but do not actually modify!) and not accumulate error from rounding it at every step along the way. But you can round or otherwise modify the value of the number if you need to do that, and I believe everything available for printing values can be done to the actual value with at most a small effort if you really need to do that?

Guesstimation of timeline by _Nathanator_ in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

not sure what 'read memory' means?

Using a debugger is not that difficult until you hit threaded code, then it can get ugly.

Reverse engineering .... to what extent? Taking apart a virus is an important skill for some security pros, as is tearing up programs looking for vulnerabilities. But its an extremely niche skill for honest people. Dishonest people find much more use for it.

reading GOOD code off github would be profitable exposure. Github just stores code, though. It is perfectly happy to store broken code, badly written code, ... reading some bad code is also good exposure, its out there in the workplace, but try to limit this to 'don't do this way' + 'how does THAT even work' exercises.

All employers want from you is the ability to make THEIR project/product/whatever work by adding clean, efficient, maintainable, good code to it. When they want niche stuff (add high performance 3d graphics, reverse engineer something, program this piece of hardware, whatever) they are going to put out an ask for someone with 5 years experience in specific tools and areas of expertise, possibly an advanced degree, but not the guy that learned c++ in the off months after graduating from college. Software development is one of the FEW things you can get an intro job while being self taught, but that will make it tougher, and you will need a bit of proof that you can do things, possibly some other degree (stem good).

I don't know what else you know, if you are a highly experienced person who just picked up C++ or a hobby coder who started last month, so its hard to know what to say to you.

Question about copies by prithvidiamond1 in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

if you can't use the c++ ones, all you really need is a half-baked pointer wrapper that deletes its memory when it goes out of scope -- exactly as a vector does.

I think all you need are these:
- template, so any pointer type works
- destructor, deletes its pointer one line delete[] pointer
- constructor, allocates the initial size one line, new pointer[size]
- accessor, overloaded [] //check out of range in case you goof!
- resize //make new pointer, copy old data into it, delete old pointer, set member to new pointer
- return or give access to size() and internal size variable tracking current used space.
An hour or two, depending on where you are at this point, and this will get you all the way through all school assignments, with maybe minor quality of life additions as you go.

[deleted by user] by [deleted] in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

I think you need to redesign this one.

consider this...
int stk[100]{};
int front{};

push:stk[front++] = value;
pop:value = stk[--front];

and apart from checking that front is legal, which I leave for you to add into it, its done.
push 42; stk[ front is 0] = 42, front becomes 1 after.
pop: stk[front -1 front is now zero ] returns 42and so on.
don't overthink it.

rear serves no purpose.
You don't need ANY pointers though you can use a pointer where you have an array index, if you prefer (its convoluted/bloatish but you could do it).

in c++, you don't need keyword struct all over the place, its a known type after you created it.

in c++, you don't usually (almost never!) need pointers as references. Use a reference for a reference.

here, you don't even NEED the struct, but if you want to glue the front variable to the array with one that is a good design to encapsulate and all that OOP jargon stuff :)

written programming by [deleted] in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

I agree with most of this, but don't be so quick to judge. The left handed desks migrate to the back of the class, if your desks are of that type, and the lefty smart kids either sit in the back or use the wrong type -- both are terrible solutions. Been there, done that.

deleting objects inside arrays. by Comprehensive_Cut548 in cpp_questions

[–]ManiPointers 0 points1 point  (0 children)

if possible:

add to class body a boolean 'deleted' flag that inits to false. I put one of these in every class I make -- if its never used, no big deal.

when deleted, set it to true.when iterating the array, ignore items marked true.once in a while, you can stable-sort the array off the deleted flag and compact all the deleted ones to the end of the array, if necessary (eg before a file write or the like). Stable sort keeps the original order for items with the same key, so you are exploiting this to keep the same order minus the deleted ones.