Forumite Members › General Topics › Tech › Makers & Builders › Programming/Code tips › C++ Programming
- This topic has 258 replies, 5 voices, and was last updated 2 years, 12 months ago by
Wheels-Of-Fire.
-
AuthorPosts
-
September 7, 2020 at 12:28 pm #61840
I meant to mention earlier that I was only able to miss noticing Struct’s because of classes. In most cases a class is a better option but Windows was mostly written in C and a lot of its functions expect a Struct as their input argument.
September 18, 2020 at 4:03 pm #62043Well I’m half way through my new programming book and so far it has been very good, but I can’t let it get away without a couple of niggles 😁
At the start of the book they say they are going to be teaching the latest C++ standard from the off, where a new feature is in every way better than an old one they will only cover the new one. Fair enough but then they spend a lot of time covering old features that you will only need if you want to use your new code with existing old code. I think that should have been covered in a separate chapter because it breaks up the flow of things.
The other niggle is that I think they over complicate some things too early. For instance, from the very start they say it is best to pass arguments that a function won’t change as Const, then they describe at length the restrictions that will cause. Well it is good practice but I think it is a bit over the top for a C++ Primer book.
Comments are welcome on the above 😃
September 18, 2020 at 6:23 pm #62045If it’s not spaghetti code I’m lost. Seriously I used to enjoy coding when I could freestyle it, I don’t have the patience to structure things properly. That’s not to say my coding was a total mess, I learned early that I needed to comment almost every line and try and keep it neat because when you come back to something you can never work out what’s going on :wacko:
If I could get my old Atari 800 back with the basic and assembler cartridges I think I would have a play again. Oh and Page 6 magazine.
September 18, 2020 at 9:46 pm #62047http://www.virtualdub.org/altirra.html
If you really wan’t to play around with Atari 8 bits then emulation is probably your best bet. Altirra, linked to above , is meant to be the best but I use Atari Win800.
Once you have your emulator set up then head on over to Atarimania for all the software and books/mags ever written. You’ll be playing Star Raiders again in no time 😃
The best thing about programming on an emulator is that you can have a hard drive as device H: and save directly into a folder on your PC.
I hope you give it a try because then you can have a go of the Sudoku solver program I just wrote in Atari BASIC, posted on the Atariage 8 bit forum 😁
September 19, 2020 at 10:59 am #62066Excellent, thanks for the link.
September 19, 2020 at 12:00 pm #62068My #1 Apple-loving son really learned to program with STOS Basic for the C64 as that gave him the ability to manipulate sprites and have animation in his games. Unfortunately imo there is nothing quite so inspiring in children’s programming languages today. Scratch appears to be passably okay, but only if you like fiddling with little pseudo-building blocks. Far too much overhead for my taste.
September 19, 2020 at 2:33 pm #62070I don’t know much about Scratch but Atari BASIC would be considered low level compared to some of the fluff that’s out there today.
Now if we all have our emulators set up then a really good demo of what the old 8 bit can do is Numen (Single disk).
This is an auto boot image so you need to run it from the boot image option under file in Altirra and then hit enter to start.
September 22, 2020 at 6:38 pm #62136My new book has been telling me more about Vectors and C++ style strings.
The elements in a Vector must be stored continuosly in memory so that they can be accessed by an index number. If you initialize a vector with an initial number of elements using square brackets, “[10]” say, or a list of initial elements then the vector will be allocated enough memory for them ONLY to start with.
If you use the push_back function to add another element to the end of the list then more memory will need to be allocated. To make sure that the memory remains contiguous the Vector function needs to assign a new bigger block and then copy the existing elements into that before deleting the originals. The Vector function ensures that the Vector name now points to the new block. This time round the Vector function assigns more memory than the new Vector will need and the amount is implementation specific. You can use the Capacity function in Vector to find out how much extra you have.
The other point to note is that the elements in a Vector are only pointers or “vectors” to the actual data the Vector holds so that data does not move when a Vector is expanded, that and the extra capacity makes expanding Vectors quick.
C++ std library strings work in a similar way but their elements actually do hold the charachte data.
September 24, 2020 at 2:51 pm #62175If you like playing with C++, you may like to try Swift as this will enable you to build cross-platform GUI Apps for Apple, Android, Linux and Windows.
September 24, 2020 at 6:13 pm #62187I may have a look at that later but at the moment I am trying my best to stick to just plain C++, Visual Studio does not make that easy though because Microsoft would really rather I used C# under the .NET framework to produce managed code. In fact they would really rather I wrote Universal app platform apps but it ain’t happening any time soon.
September 25, 2020 at 7:21 pm #62205More interesting info about Struct’s in my new book. In most ways C++ treats a Struct exactly the same as a Class. The only difference is that Struct’s don’t support inheritance. You can have a function in your Struct, no problem.You can even have a local Class in your Struct (you will have to pass any arguments from within the Struct).
September 25, 2020 at 8:30 pm #62207You can also have pointers to functions and double pointers (pointers to pointers) in your structure and these can when unwound point to other structures which in theory could even have double pointers pointing back at the calling structure. You can tangle your brains completely if you do this!
C/C++ enables you to do all sorts of crazy things which is why M$ pushes managed code.
September 25, 2020 at 9:52 pm #62209Well you could do that but I think that inheritance has mostly been introduced to make such things unnecessary, meant to keep your thinking on the straight and narrow.
I don’t really know though because I only just started the chapter on inheritance and so far its as clear as mud 😁
September 25, 2020 at 10:13 pm #62211I intended to skim the whole book just to get a flavour of whats in it and then start again at the beginning, doing the exercises, but I got sucked in so im READING the book without doing the exercises 😄
When I get to the end I will have to start again and do it properly.
September 26, 2020 at 11:46 am #62227I am going to need to go again with the exercises to make things stick. Remembering the syntax is the main thing and only practice will do it.
I know when I want to pass an object by reference or by value but the *’s or &’s are a pain. Then we get && and other such things.
Then we find C++ can pass an object AS a reference to another object instead of using a pointer, anything you do to the reference also happens to the original object .
Like I said, some practice required.
September 26, 2020 at 1:07 pm #62231And then we get “range” FOR loops. I know what they do but I forget where the iterator comes from. Practice, practce,practice 😆
September 26, 2020 at 8:36 pm #62233Based on my old C experience you really need to crack pointers and addresses as they underpin arrays, strings etc, but probably as important is the need to correctly instantiate and range check anything you use otherwise you finish up with pointers that point at thin air and null addresses.
Once you crack that then comes the real pain of making sure that you have already set aside memory for these objects (malloc etc.), and of course freeing it (garbage collection). This is the area where you can generate really unsafe programs e.g. memory overflow, use after free, corrupted double pointers, etc.
Just when you think you have cracked it all comes the problem of error trapping i/o. This becomes a very hard slog and only a very few programmers manage to write large completely ‘safe’ programs or routines. Bad programs can result in users blaming the OS for any security lapses (think of the ordure that Flash caused for years) and this is really why M$ pushed C# (though to be fair there are competitive ‘safe’ programming languages that are also a step or two away from the machine code that compiled C++ produces. e.g. Ruby, Python etc).
September 26, 2020 at 10:00 pm #62235C++ tries to keep me away from pointers with its references. I can just pass a reference to an object, it is almost the REAL object, if I change it I change the memory the original points to.
Exception handling is in 2 chapters time 😃
September 26, 2020 at 10:02 pm #62237REALLY good book
September 27, 2020 at 8:57 am #62241Still can’t match the hardware access I get in Atari BASIC. Can Peek or Poke from there 😃
-
AuthorPosts
- You must be logged in to reply to this topic.
