Working on Learning C++


Yes and No: I gather it's been integrated into gdb since 5.3 and the version on the nand is 7.0, but there's no separate executable for it. I'm guessing here, but I'd say you can the functionality of gdbserver by running gdb, but there's no nice low-resource version of it.

Are you sure it's not gdbclient that's integrated in gdb ? on my debian gdb7.1 and gdbserver are two separate packages, and angstrom sure have a gdbserver7.2 package...


Oh, and I just remembered you have to use a cross-debugger version of gdb on the client side two (probably something like arm-linux-eabi-gdb).
 
Any tips for pointers, guys? I'm currently on that chapter, and I understand that later on they're important. Still not quite sure how, though, so some example ways pointers could be applied (with there being no better way to accomplish the intended task) would be appreciated.
 
Pointers are the bane of everyone who tries to learnC/C++. All I can suggest is that you do your best to understand it, and move on for now.
 
It's void, null, and to-function pointers that really get me. I'm also about to work on dynamic memory. I understand that involves pointers a lot, too.


Actually, I kinda get null pointers now; they're pointers that point to nothing. They're on break, and they're not supposed to be. Catching a pointer off-guard, while on break (ie, while null) results in him not being attentive to the situation, and ultimately screwing up (crashing the program). Boy, analogies are great!
 
Last edited by a moderator:
Pointers contain a memory address. You can access stuff in that address through the pointer. You can change the memory address the pointer is pointing to by assigning it a new value, for example from another pointer. A null pointer just contains the value NULL (typically used in C), or 0 (typically used in C++), or nullptr (used in the upcoming C++ standard C++0x), and signifies that the pointer actually does not point anywhere.


Dynamic memory allocation returns a pointer to the newly allocated memory. In C++ dynamic memory allocation is done with the operators new and delete. New takes a type and for classes optionally a constructror parameter list.


For example to allocate a few things:



Code:
int* ip = new int;

string* sp = new string;

string* sp2 = new string("Hello World!");



Now, the memory of those int and string variables is not freed until you say so using delete. Delete takes a pointer to dynamically allocated memory and frees it. If you lose the last pointer to your dynamically allocated memory, you can no longer free it and your program will leak memory!



Now to free the memory allocated above:



Code:
delete ip;

ip = 0;

delete sp;

sp = 0;

delete sp2;

sp2 = 0;


Notice that I nulled the pointers because they no longer point to an allocated portion of memory. You can also allocate and delete arrays of things, but that's less commonly needed.


Function pointers just point to the memory address where the function code resides. It can be used to call the function without knowing its proper name. Nice if you for example want to give a function as a function parameter or do something resembling functional programming in C++).


Void pointers are "pointers to anything". It usually means the type of data behind the pointer depends on runtime information. In that case the void pointer needs to be cast (means changing the type a variable is treated as) to another type, but you need to figure out from the code what it should be cast into. Don't use these lightly, as they break type checking and make your code really hard to follow if used improperly.


Pointers are just integers that signify memory addresses. Because the language is aware of this, pointers can have strong typing (type checking) and other nice stuff.
 
Last edited by a moderator:
Pointers are the bane of everyone who tries to learnC/C++. All I can suggest is that you do your best to understand it, and move on for now.

really? seem pretty simple to me, you just need a decent lecturer to run through them, and to sit down and use pointers for a bunch of standard lab excersizes


it's the OO design methodology and inheritence and all that that'll get you :D
 
Here's a really simple example of use. Say you have function void moveMonster(MonsterInfoStructure* monPtr). To pass the MonsterInfoStruct into that function you use a pointer so all that gets passed in is the address of the start of structure. If you didn't use a pointer you would be passing a COPY of the information (your structure could be several Kb in size), so to make any changes you would also have to pass a copy of the modified structure back out of the function and copy it over the top of the original information. VERY inefficient.


By passing a pointer to the start of the real structure you are only passing (probably) 4 bytes and the function can modify the information directly.


It's maybe worth reading some basic information on how a computer works from the assembly level i.e. a stack, registers etc. Nothing major, just an overall idea. It's makes it obvious how and why pointers are used.


Function pointers you can probably get away without using. They are very powerful, but you have to have a design solution that needs them (very handy for state machines). Standard pointers however are absolutely fundamental.


One of the main things to understand is "pass by value" and "pass by reference". If you pass something by value then you pass a COPY of the information, therefore any changes won't affect the original. Pass by reference means you pass a pointer/reference to the information, so any changes you make will affect it (see the MonsterInfoStruct example above). I guarantee you will get this wrong at some point :)
 
Last edited by a moderator:
^ except you can do that particular thing smarter with references without messing around with pointers and conversions to/from them.



Code:
moveMonster(MonsterInfoStructure& monster)
 
Pointers are the bane of everyone who tries to learnC/C++. All I can suggest is that you do your best to understand it, and move on for now.

really? seem pretty simple to me, you just need a decent lecturer to run through them, and to sit down and use pointers for a bunch of standard lab excersizes


it's the OO design methodology and inheritence and all that that'll get you :D
Perhaps that's just it - the small amount of C++ I do know I had to learn myself. OO, on the other hand, I found pretty easy, though that's probably because I ended up covering the topic in three seperate languages (and I actually had a lecturer the first time).
 
Perhaps that's just it - the small amount of C++ I do know I had to learn myself. OO, on the other hand, I found pretty easy, though that's probably because I ended up covering the topic in three seperate languages (and I actually had a lecturer the first time).

formal courses are usually the best way to grasp tricky concepts like these, pointers aren't too hard once it's explained what they are, and what operatins such as incrementing a pointer to a long does, as opposed to incrementing the same thing casted to void or a long int (ignoring whether your language will even allow that)
 
formal courses are usually the best way to grasp tricky concepts like these, pointers aren't too hard once it's explained what they are, and what operatins such as incrementing a pointer to a long does, as opposed to incrementing the same thing casted to void or a long int (ignoring whether your language will even allow that)
Pointers are simple; what's difficult to grasp at first (and also often difficult to work with) is indirection.


It's all very well to say "well, a pointer is just a variable that holds a memory address, (and to be accurate the compiler also keeps track of the type and size of the variable stored at that address), and there are some special operators, like &, *, and -> in C that allow you to reference values stored at those addresses," which of course elides over some important details but I think is pretty easy to understand.


Things start to get hairy, though, when you actually have to write or modify a program that manipulates data at more than one level of indirection, and you have to try to visualize how the data structures actually work.


This is why we have ideas like the 'four-star programmer.' :D
 
Anyone know a good place for C++ exercises? Cprogramming.com is very lacking in that regard.
 
Last edited by a moderator:
I find Project Euler to be especially good, if a little abstract. Although it does not really test out your entire programming repertoire, there's a lot to be learned there about optimisation of code (which is especially important for me as I use Python primarily, but is still important to know).
 
I think some of the best exercises in indirection mentioned by Tom are ones that deal with a single operation on a data structure. They are usually simple to understand, and you usually see very easily if you've messed up. For example take a linked list:



Code:
struct N {

  int i;

  N* next;

};

                         N                 N                 N

                   +-----------+     +-----------+     +-----------+

                   |           |     |           |     |           |

                   | i = 3     |     | i = 2     |     | i = 1     |

N* head = [o]----->|           |     |           |     |           |

                   | next = [o]----->| next = [o]----->| next = 0  |

                   |           |     |           |     |           |

                   +-----------+     +-----------+     +-----------+


 [o]-------->  pointer


1. Create the data structure above


2. Create a function to print the values of i for each node, if given the head pointer


3. Create a function to append a new node to the end of the list


4. Create a function to remove the last node (remember to delete it too!)


5. Create a function to append a node to the front of the list


6. Create a function to remove a node anywhere in the list


7. Create a function to clear a list


8. Create a function to reverse a list


9. Create a function to sort a list by values of i
 
Last edited by a moderator:
Back
Top