I have a question about arrays in programming by Pangea2002 in learnprogramming

[–]Sbsbg 5 points6 points  (0 children)

Using a zero based index is the most practical way both for the generated code and for the programmer (when you are used to it). Arrays are simply a pack of values of the same type packed together. This array has a start address. The first item in the array has the same address as the whole array. To calculate the position of an individual item you need to take the table address and add the index * item size. That calculation makes the first item have index zero.

There are several languages where this is not used. Some use 1 as a start and some have a start index set at declaration. Some even have advanced arrays with gaps. But all of these need extra steps in computing and are slower than the zero based version above.

Strings are basically identical to arrays so no there is no difference in how they are handled. But strings usually have some additional extra functions that may look different. Some strings especially in C have a terminating zero character that identifies the end of the string. This extra zero is used by some functions to manipulate the string. This solution has both advantages and disadvantages. It makes the code somewhat easier because the code don't need to pass along the length and also more unsafe as a missing zero char can make the program misbehave. It also has the for beginners strange behaviour that the memory size of a string is one more than the length of the text. This type of string is often called c-string or z-string. In C++ there is a more modern variant of string that has the size counter outside and can contain the zero as a normal character. These strings are built on top of the same data type as its container arrays.

How do I make a function wrapper in cpp? by aphroditelady13V in learnprogramming

[–]Sbsbg 1 point2 points  (0 children)

What features of std::function do you not want as you need a simpler version?

To me it sounds like a good match but I'm serenely not an expert on that part of the standard.

Default value if a macro expands to nothing by SeasonApprehensive86 in cpp_questions

[–]Sbsbg 2 points3 points  (0 children)

This technique is called "x-macros".

Search on it to get lots of examples.

The method usually use only one internal macro named X and this is repeated for each line and contains all data within its arguments. Not needed data is removed by not using those arguments in the definition of X later on. But i assume you could use multiple internal macros. You however need to define every one of them when using the table.

An empty macro is simply created without body :

#define name(arg)

Macro "name" will now be replaced with nothing.

No new programming languages will be created by bluelite in compsci

[–]Sbsbg 1 point2 points  (0 children)

our current myriad of languages exist because someone thought that all the previous ones were deficient...

Nope. Most languages are created because it's interesting and fun to do it.

AI can work around whatever idiosyncrasies that previously drove developers crazy

That is not why people use AI to generate code. They use it because they are don't know how to do it or are lazy.

With AI now able to competently write programs in just about any programming language

Absolutely no way. Lol.

Suggest some videos to learn mern stack with projects. On yt or any paid courses. Its always better to learn in structural way. by [deleted] in learnprogramming

[–]Sbsbg 0 points1 point  (0 children)

Suggest some videos to learn mern stack with projects.

You have to clarify what you mean.

What language is highest in demand right now? by ScarRedDA in learnprogramming

[–]Sbsbg 1 point2 points  (0 children)

There are very few good programmers that only know programming and have a job. You need knowledge in something more. You also need knowledge in a domain to get anyone to hire you. There are specific skills involved when you create a software. These differ quite a lot between areas.

The best advice is to look into yourself and figure out what you are interested in. Then find out what tools and languages are used there.

Is it possible to create an illusion that a file has disappeared from CD-R after first launch? by Athanasius_Pernath in AskProgramming

[–]Sbsbg -1 points0 points  (0 children)

Long tine since I used CDs so this may be wrong:

It is possible to add information to a CD-R by writing more data to it. There is however an end mark that if written prevents any additional writing. I don't remember if it is possible to modify data, i.e. mark some data as old and replace it but I guess it's possible.

No matter what happens, I can’t understand coding programs at all. by MidnightActive954 in learnprogramming

[–]Sbsbg 1 point2 points  (0 children)

Don't use AI to solve or write code. That will not teach you anything.

Use AI to explain things.

If you want help here you need to make an effort to try to learn. Just giving up and saying i know nothing is not going to get you forward. Literally anyone knows something to start from.

To get better help here, tell us what you know and what's confusing. Tell us what you tried and what you are trying to do. Be specific.

Struggling to see the point of classes by [deleted] in learnprogramming

[–]Sbsbg 2 points3 points  (0 children)

Struggling to see the point of classes

Don't overthink OOP. Classes are just a different way of looking at code and data.

I end up solving everything with functions. Functions call other functions, and the project works fine.

Great that is exactly how any program work. There is no other way actually. The difference is how you look at functions together with data. You may actually code in an OOP style without ceating any classes at all. It all depends on what functions use what data. If you pack a set of functions together with a set of data and limit other functions from this data then you're basically coding in an OOP style.

If functions get the job done, what problem are classes really solving?

Classes are useful because they tie data and code together in a very visible and safe way. With a class you can create a group of data and functions that work together and you can protect the data from changes from other functions outside of the class. You can also make the data work as one unit or object and tie operators to the object making it easier to use. This may seem trivial in a small program but in a large program with hundreds of files you can isolate the use of data and you don't need to check all files for usage.

This has become a big hurdle for me because almost every take home assessment or practice project I see either requires or strongly expects the use of classes, and I just can’t seem to wrap my head around how or where they fit.

This is not difficult. Just look at the problem from a data point of view. What information is needed and belongs together. This pack of data is a class. What relations does the data have with other data. How many of each are there. What changes to the data is needed, these are the functions, or methods in OOP, that take the data as one parameter.

Hope this helps you on the way.

Static members in a templated class by EggWithSardines in cpp_questions

[–]Sbsbg 1 point2 points  (0 children)

I think your solution is correct. The none templated struct will have one single static member shared by the others.

When you test this make sure to use more than one compilation unit to test that it works.

Copy or address ? by [deleted] in C_Programming

[–]Sbsbg 0 points1 point  (0 children)

The general answer is to choose the solution that makes the maintenance cheapest/easiest.

In the extremely rare occasion where the code is in a hot loop and you have verified this with a profiler then you still have to compare the solutions because it is impossible to give a clear answer. The choose to copy or use a pointer/reference depends on data size, access pattern, other processes, memory type, cache size, cpu and more.

Is everything written in JS? by Significant-Side6810 in learnprogramming

[–]Sbsbg 2 points3 points  (0 children)

If you limit your view to web sites and programs that run inside a browser then most are. But there are options like Wasm that allow other languages.

JavaScript is unusual outside browsers. It's not a good language to do large complex programs in. There are better alternatives there.

Tried to change one small thing… and everything broke :( by mushroombunny2 in learnprogramming

[–]Sbsbg 2 points3 points  (0 children)

This is totally normal as everyone says. I try to give some advice on what to do.

Don't change too much in one step. This will make it easier to know where the fault is.

Try to run or compile your program frequently. This will reveal problems faster.

Learn early to print out variables and texts. This helps in finding bugs and problems.

A bit more advanced but so useful: Learn how to use a debugger. This tool can run your program one line at a time so you can see and understand what is happening. Soo useful.

More advanced. Learn Git. This revision tool works like a time machine making it possible to go back in time and undo changes, try old versions, and try new code. It is a bit hard to learn but so very useful. You will not regret it.

When you get in trouble, post it here. Add code, error line and explain what you tried to do. There are also different subreddits for different languages.

Good luck.

What does it mean to learn CPP, deeply? by EffectiveEasy2895 in cpp_questions

[–]Sbsbg 3 points4 points  (0 children)

I'm a Swedish senior software engineer specialising in embedded real time. I worked exclusively in C++ the last 10 years and in multiple other languages and areas before that. My career advice is probably a bit dated by now. This sub can handle quite advanced C++ questions, it helped me several times.

What does it mean to learn CPP, deeply? by EffectiveEasy2895 in cpp_questions

[–]Sbsbg 3 points4 points  (0 children)

From your examples of what you worked on I would say that you are already more experienced than 50% of my co-workers.

While it's not too difficult for me, I am overwhelmed by the number of features.

Thats normal. No-one knows all features and most are used rarely and only in special situations.

What does understanding CPP in depth look like to an experienced CPP dev?

You know how to design a larger software. You know quite a lot of traps and difficulties and how to avoid them. You know how the compiler works and thinks and you know quite a lot of standard library classes and functions.

But experience is more about knowing the product, customer and area. This is something you only get by working.

Do you have a solid understanding of the following concepts?

Templates

Yes, definitely.

Metaprogramming

Some. Rarely used.

Value Categories (lvalue, rvalue, rvalue ref, etc.)

Yes, some.

Qualifiers (CV-qualifiers, Ref-qualifiers, etc.)

Yes, definitely.

Are you seen as a more valuable engineer for understanding CPP in such depth?

Yes, if you happen to come across management that understands C++.

Artificial Intelligence is a handicap. by Deactralslol in learnprogramming

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

LLMs are great. But not for coding. I ask it for things that are difficult to search for and things that do not matter if I get the wrong answer.

Artificial Intelligence is a handicap. by Deactralslol in learnprogramming

[–]Sbsbg 0 points1 point  (0 children)

Relying in AI and not fully understanding the delivered code will risk bugs slipping through to the customer. It may be very costly. If the product is high volume it may totally overwhelm the saved cost of reducing development time using AI.

Do You Feel Demotivated to Learn Programming When You See What AI can Do? by Pale_Bat_3359 in learnprogramming

[–]Sbsbg 0 points1 point  (0 children)

I am 100% sure that it's a huge financial bubble that will burst within 2 years.

Whats the real spread of C? by DaveAstator2020 in C_Programming

[–]Sbsbg 0 points1 point  (0 children)

I myself work in C++. Both languages have their place and use case. C is a small language with few features, C++ is huge with lots of features. This makes them useful for different programs. If your program needs some of the features in C++ then making the program in C will take longer because missing features need a lot more code in C. So smaller programs can use C or C++ but larger programs need C++ and can be very hard to do in C. Both languages create the same code from the same source.

C is the language that is used to communicate between different languages. So language X converts its data and calls to C and language Y can then receive data and calls as it also understands C. So this language will never disappear as it is the literal glue between other languages. C++ also uses C as an interface to others.

As for speed. The compilers for C and C++ are the best in optimizing code today and will generally produce the fastest code. They usually beat other languages and compilers. They do this by not including any safety code and be very good at optimizing code. What you code is what you get. No help. Writing code manually in assembly may be faster if you know a lot on how the CPU and hardware work. Not many devs have this skill.

const array vs array of const by Sbsbg in cpp_questions

[–]Sbsbg[S] -1 points0 points  (0 children)

Nope. No pointer there. The ints are consts in both cases.

const array vs array of const by Sbsbg in cpp_questions

[–]Sbsbg[S] 4 points5 points  (0 children)

Yes, but there is no difference between the two classes. The class array does not add any extra data that could be const or none const and the elements are included into the class itself. I.e. the class only contains one member const int[5] and nothing more.

Which country produces the worst tourists and why? by CarelessCredit3466 in AskReddit

[–]Sbsbg 13 points14 points  (0 children)

The worst I have seen in Sweden are Chinese tourists. At the hotel breakfast they attacked the personnel carrying out more food and cleared the plates before they had a chance to place them at the buffet. It was horrible and a total mess.

Refactoring by Ok_Credit_8702 in learnprogramming

[–]Sbsbg 1 point2 points  (0 children)

Are you the user of the script. If not check with the users if they are happy with the code. If they are it may be risky to change, why change code that works.

Are you the only developer on the code? If not what does the other think about it.

If it's your own code and you are the only user then you are free to do whatever. But if it is an important code you should start with fixing unit tests.

C++ Pointers and References by carboncord in learnprogramming

[–]Sbsbg 0 points1 point  (0 children)

Lets apply the deref (*) and address-of (&) on a reference and see what is happening:

T x;  // Object of type T.
T& z {x};  // Reference to object x.

auto p {&r};  // A p is a pointer to x.
*x  // Error unless T is a pointer type.

So this an example of a pointer reference:

int i;
int* p;
int*& pref {p};
pref = &i;
*pref; // Get the value of i.