@grahamdearsley
Forum Replies Created
-
AuthorPosts
-
I have submitted a new thread called C++ programming. If and when it appears I have a few things relating to classes and constructors to put in it. I fear I may be on my own here though ?
I have put a note on my memory aid sheet ?
As im now looking at the memory management module I have noticed something else. If I use “int *Pointer = new int” to reserve some memory then Pointer is actually an object. The memory for Pointer will be allocated from the heap instead of the stack and it will remain in use for the life of the program or until I remove it with “delete Pointer”. Don’t quote me on this though because the module on objects is between where I was and memory management and I haven’t read it yet ! They do say that creating objects and failing to delete them after use is a cause of memory leaks though.
I found the link below when I was looking up the C++ standard.
https://isocpp.org/get-started
I will look into some of these books later.
Oh yeh, we are also “overloading” the & operator. Int &Ref = Avariable gives us a reference to Avariable called Ref as you would expect but int *pointer = &Avariable gives us a pointer to the memory address of Avariable. And of course * and & are already used as multiply and a logical operator so it all depends on where they appear in your code. Did they have to do that ?
They also explain the gobbledy gook in the video too.
Even though Pointer will contain the memory address of Avariable when when I use int* pointer=&Avariable, Pointer must still be the same data type as Avariable because it must be capable of holding its value as well. Pick the bones out of that if you will ?
Ah. Reading ahead again they have their explination in print. We are “overloading” the use of the * operator, we are using it both to declare the use of a pointer (int *Pointer=&Avariable) and to dereference a variable (cout << *Pointer in my example). If I just want to reserve some memory for a pointer of type int then apparently I must use the NEW key word.
Int *Pointer = new int
This is all in the memory management chapter, about 3 ahead of where I am !
Avariable is declared and initialised with a value of 3 at the beginning of the program so it has an address in memory. Int* Pointer=&Avariable really does put the memory address of Avariable into Pointer even though it is 4 bytes long. It does not matter if I put the space before or after the * either.
Another thing that was shown in a video clip, but not actually mentioned, relates to splitting your function definitions into a separate cpp ( only just realised that cpp stands for C plus plus ?) file. They say you should create a new header file with the function declaration in it, fine. They say that your new cpp file with the function definition in it and your main cpp must include your header file, fine. They do not say that EVERY cpp file must include the “Pre Compiled Header”. Bloody thing would not build ?
double Pointer=&Avariable won’t work either, another type mismatch error.
You would think so but it works in Visual Studio and it is the way the Microsoft course told me to do it. If I remove the * dereference from Pointer then I get a type mismatch error. The address in Pointer is indeed a four byte value. They give some reason about why this works but its in a video clip and I have played it back several times without being any the wiser.
I wrote the code myself in Visual Studio and pasted it here so you can paste it back and try it if you like ?
One error is that the output is 3, memory address, 3,3,4.
I could also have just used Ref++ instead of Ref=Ref++, a hangover from my BASIC ways ?
A crisp and sunny 10° in Wembley at the moment.
I am just about to print out the following and add it to my C++ Course folder as a memory aid. If anyone can spot any errors please let me know.
#include “pch.h”
#include <iostream>
using namespace std;
int main()
{
// Another way to assign a variable
int Avariable{ 3 };
cout << Avariable << endl;
// A pointer to a memory address
int *Pointer = &Avariable;
cout << Pointer << endl;
// DeReff a Pointer
cout << *Pointer << endl;
// Reference or alias a variable
int &Ref = Avariable;
cout << Ref << endl;
Ref = Ref++;
cout << Avariable << endl;
return (0);
}
The output is 3, memory address, 3, 4.
So many jokes I could make about the EU having 1 mind here..
But I won’t bother ?
There are actually two “its” . I did make the first one clear. The EU institution HATES us. They hate our ways they hate our history they hate any success we may have and most of all they hate the fact that they need our money ! They keep telling us we need THEM while all the while they are bleeding US dry. Not all of us are happy for this to continue and that brings me to the second “it”. We had a “Peoples” vote for a change and the result MUST stand. The “elected dictatorship” as Ed puts it must do as they were told for a change.
They actually did a fair job with arrays and pointers. First they did data types for variables and THEN pointers and THEN pointers into arrays. Even there though they tended to assume that I am already handy with Visual Studio (I am not?).
I originally taught myself C out of a book on my Atari ST and I dont remember having to read ahead just to understand the topic being talked about, refer back plenty but not forward. I think it may have something to do Hyperlink style of writing used on the web these days. This style just does not translate well into a book or video clip.
Who do you trust as a financial advisor ? One school of thought is to get an independent FA because they can advise you on products from any company. On the other hand they are just as likely to sell you something that earns them the most commission. The Pru used to offer some good free advice on such things and you dont have to take it.
I ment should have been told in the introduction course.
Is higgledy piggledy the new method of teaching or is it just a Microsoft thing ? I first noticed it in the Microsoft press Windows Internals books. These books would keep doing things like this.
We will now cover “something”. In order to understand “something” you must first understand “something else” that we will cover in a later chapter.
They then go on as if you already know what “something else” is. You end up having to read through the whole book four or five times, picking up what you can each time.
Now I am finding the same thing on the Microsoft C++ course on edX. At the end of the introduction course, past the final self test, they tacked on a video about C++ classes. The video said don’t worry if you don’t get everything going on here because it will all be explained in the intermediate course. The intermediate course starts off with some things about headers in Visual Studio I should have been told in the intermediate course and then moves on with “As we learnt about classes in part one” !
Quite likely, and I have now also read that it would be difficult to do consistently on larger projects, especially those involving collaboration.
-
AuthorPosts
