Continue or Postpone.....

Started by
28 comments, last by bepawuca 15 years, 4 months ago
@jbadams: I realize that C++ doesn't reflect the workings of your computer directly, I'm just trying to explain in terms a beginner will understand. Perhaps I should have said C is a "systems programming language", but how do you really understand what that means if this is your first time programming?
Besides, it is far more likely that you will learn about heap and stack memory, paging, SIMD, caches etc after a few years of C++ than after a few years in Python. You don't need to know the physical properties of semi-conductive materials to know what a transistor does. But then again, perhaps it's good to at least let the student know that there is such a thing as semi-conductive materials beneath those transistors even if we don't explain it :)

@Kilom: I felt I should maybe just add something as clarification because my explanation was a little technical. You can think of your computer's memory as a huge array and a pointer is simply an index into that array. In other words a pointer is just another number. The only tricky bit is that the compiler knows what type of object a pointer is pointing to so it knows that an int is 4 bytes (typically) and a char is 1 byte.

So when you have a pointer to an int and you add 1 to it then the pointer automatically moves 4 bytes forward:
int* pointer;pointer = pointer + 1; // moves the pointer forward 4 bytes

In any case, good luck with your future endeavors!

[Edited by - errantkid on December 11, 2008 3:58:32 AM]
Advertisement
Quote:Original post by errantkid
@jbadams: I realize that C++ doesn't reflect the workings of your computer directly
Sure, I'm just continuing my quest to try stamping out phrases like "closer to the machine" when describing C or C++ - as you've just acknoleged, it simply isn't true. [smile]

- Jason Astle-Adams

Hey kilom i decided i will tell you what i did when i started. I am 14 now, started programming about a year and a half ago and i was having some trouble deciding on a language to. When there was a sale at borders my parents picked up some c, c++, and python books for me. I tried out python but eventually switched to c++. Oh yeah before that i tried out game maker but did not stick with that for long. The main thing that helped me learn c++ was about half a year ago when my parents ordered a couple gameinstitute.com courses on c++.

P.S If you do not mind spending some time you could try see.stanford.edu and you could start with cs106a to start with basic programming concepts then go to cs106b to learn more advanced topics using c++.
Check out my new blog: http://beefmachinegames.wordpress.com
Quote:Original post by errantkid
@Kilom: I felt I should maybe just add something as clarification because my explanation was a little technical. You can think of your computer's memory as a huge array and a pointer is simply an index into that array. In other words a pointer is just another number. The only tricky bit is that the compiler knows what type of object a pointer is pointing to so it knows that an int is 4 bytes (typically) and a char is 1 byte.

So when you have a pointer to an int and you add 1 to it then the pointer automatically moves 4 bytes forward:
*** Source Snippet Removed ***
In any case, good luck with your future endeavors!


How would that, using your example of pushing a 1000 element array onto the stack, increase its speed/efficiency by using pointers? Technically are you not still pushing the same stuff onto the stack? I kinda don't want to just accept that it just does, would like to know why. :P



EDIT: Wait, is it like after initially pushing the data onto the stack, instead of copying it again into the stack or moving (or whatever), you can simply "point" to where it's at so the program can gather it from there? Is that close or far off description of it? I just suddenly thought of this and wondered if this is right.

[Edited by - Kilom on December 11, 2008 9:23:41 PM]
Quote:Original post by Kilom
EDIT: Wait, is it like after initially pushing the data onto the stack, instead of copying it again into the stack or moving (or whatever), you can simply "point" to where it's at so the program can gather it from there? Is that close or far off description of it? I just suddenly thought of this and wondered if this is right.
Bingo, I think you're starting to get the idea there. Another useful thing with using pointers in this way is that you don't have to know in advance how many items of data there will be - you can just create them when needed and use your pointer to refer to them.

- Jason Astle-Adams

Quote:Original post by jbadams
Quote:Original post by Kilom
EDIT: Wait, is it like after initially pushing the data onto the stack, instead of copying it again into the stack or moving (or whatever), you can simply "point" to where it's at so the program can gather it from there? Is that close or far off description of it? I just suddenly thought of this and wondered if this is right.
Bingo, I think you're starting to get the idea there. Another useful thing with using pointers in this way is that you don't have to know in advance how many items of data there will be - you can just create them when needed and use your pointer to refer to them.


Ah. Glad I finally got it. Starting to think of ways I could use pointers.

While I'm still asking questions, I could also use some help on structures, enum-something, and there was something else. I understand a little of what they are doing/supposed to do, but it's tricky for my to try to implement them. I constantly have to refer back to this book to figure how to do them (mostly because I haven't fully practiced with them yet probably). Would you guys mind also trying to clarify this for me also. I would appreciate it very much.

Thanks guys for all the help.
Quote:Ah. Glad I finally got it. Starting to think of ways I could use pointers.

Don't worry too much about them, you'll start using them eventually whether you want to or not. In fact any time you use a string literal like "Hello world", you're already using a pointer :P

Quote:While I'm still asking questions, I could also use some help on structures, enum-something, and there was something else. I understand a little of what they are doing/supposed to do, but it's tricky for my to try to implement them. I constantly have to refer back to this book to figure how to do them (mostly because I haven't fully practiced with them yet probably). Would you guys mind also trying to clarify this for me also. I would appreciate it very much.


I'll try some layman's explanations:

Structures: A way of grouping together related data that you use together.

Classes: A way of grouping together related data along with the operations that act on the data. (Technically you can add operations to structures as well in C++)

Enum: Say you have a switch that can be on or off. You can simply make the switch a bool. If it's set to true, it's on. If it's set to false, it's off. Now suppose you have a car. It can be a mercedes or a toyota. You could use the same trick of making carType a bool. But if you add a third type of car, e.g. a Renault, you have a problem. So instead you just do this:
enum CarType{  Mercedes,  Toyota,  Renault};CarType carType = Renault;

This also makes a lot more sense than "true" or "false".

Just practice and it'll all fall into place, trust me. By the way, one of the most fun little programs to write as a beginner is something that emulates the console. You just print out 'C:\>' and then give funny replies to all of the classic DOS commands like 'dir' and 'cd' and so on. Great for fooling your friends! I hope you kids still know how to use DOS these days :P
Quote:Original post by Kilom
Quote:Original post by jbadams
Quote:Original post by Kilom
EDIT: Wait, is it like after initially pushing the data onto the stack, instead of copying it again into the stack or moving (or whatever), you can simply "point" to where it's at so the program can gather it from there? Is that close or far off description of it? I just suddenly thought of this and wondered if this is right.
Bingo, I think you're starting to get the idea there. Another useful thing with using pointers in this way is that you don't have to know in advance how many items of data there will be - you can just create them when needed and use your pointer to refer to them.


Ah. Glad I finally got it. Starting to think of ways I could use pointers.

While I'm still asking questions, I could also use some help on structures, enum-something, and there was something else. I understand a little of what they are doing/supposed to do, but it's tricky for my to try to implement them. I constantly have to refer back to this book to figure how to do them (mostly because I haven't fully practiced with them yet probably). Would you guys mind also trying to clarify this for me also. I would appreciate it very much.

Thanks guys for all the help.


Would the "something else" you mentioned be unions?

Quote:Original post by Jetjayjay


Would the "something else" you mentioned be unions?


Yes, that's what it was.

Quote:Enum: Say you have a switch that can be on or off. You can simply make the switch a bool. If it's set to true, it's on. If it's set to false, it's off. Now suppose you have a car. It can be a mercedes or a toyota. You could use the same trick of making carType a bool. But if you add a third type of car, e.g. a Renault, you have a problem. So instead you just do this:

enum CarType
{
Mercedes,
Toyota,
Renault
};
CarType carType = Renault;


This also makes a lot more sense than "true" or "false".


That...still doesn't make much sense. Meh, I'll just try reading that section about that stuff again maybe this weekend

Just think of it like this:
Imagine we have a function

char * Car(int carType);

The function returns the name of the car based on the number we pass to it as an argument. If we pass 1 it will return "Mercedes", if we pass 2 it will return "Toyota", and if we pass 3 it will return "Renault".
You would call the function like this:

Car(1);

Let's say you want to pass the argument to Car() in a more readable form, so you could call it like this:

Car(Mercedes);

To do this, you could make an enum called CarType, which would look like what errantkid said:

enum CarType
{
Mercedes,
Toyota,
Renault
};

Now you can have a variable of type CarType which can hold three possible values: Mercedes, Toyota and Renault. So the new function definition of Car() would be like this:

char * Car(CarType car);

You can then do this:

CarType myCar = Mercedes;
Car(myCar);

This topic is closed to new replies.

Advertisement