Showing posts with label pointers. Show all posts
Showing posts with label pointers. Show all posts

Sunday, March 4, 2012

#6 March 4

Another week and some more intricacies of C++ come to light - the many different uses of the const keyword. While I have covered const pointers before, it's still handy to brush up on how const can change the usage of a variable or a pointer depending on how you place it in the declaration.

So let's break things down:
When you declare something as const, quite intuitively, you're saying that it can't be changed. This obviously means that a const variable MUST be initialized during declaration.
So, for example:

const i = 2;

would initialize the constant in i to 2 and then you would not be able to modify it (i becomes read-only).

This becomes slightly more tricky once you start using pointers:

const int* p = &i;
int* const p = &i;

In the first case, we're declaring a pointer to a constant, i.e., the value being pointed to by p (in this case i) must be constant and cannot be changed, while p itself can. Thus I can reassign p, but something like ++*p would not be allowed.

In the second case, we're declaring a constant pointer. This means once initialized to some address, we will not be able to modify p. This is particularly useful if you think of pointers being returned to indicate the start of an allocated memory location - we want to ensure that we keep this address constant.

There are other variants to this, including mixing it up with references, but once these 2 have been clearly distinguished, the rest fall into place.

Saturday, February 25, 2012

#5 Feb 26

So begins the 2nd part of the course (I've logically divided it into 3 parts based on the Tests). First piece of advice - don't miss a single class! Due to personal reasons, I missed the class on Monday - when the 3rd project was being described - it's basically to implement the functionality of a heap. I felt lost when I came to class the other day - partly because there's no specs listed on the project page and also because I didn't realize how dependent I was to little things mentioned in class which help during quizzes and the project implementation. But I think I've caught up and now the project is going along full-steam ahead! Pair programming is going well - and I think I'm lucky to have found a partner who's as obsessive about correct indentation, spacing and brace-styles as I am!

So I don't have much material to talk about, since the class I did attend was mostly spent in re-introducing the different versions of the sum function for different iterators. Readings have finally started - and the quiz brought up the point of IS-A (inheritance) vs HAS-A (containment). For now, it's back to the project - we're knee-deep in pointer territory and you never know when a stray pointer could attack (the dereferenced ones are particularly vicious)!

Sunday, February 12, 2012

#3 Feb 12

This week was mostly a continuation of the discussion of exceptions. Some of the things that I found interesting:

  • C++ can throw any type whatsoever, it doesn't have to be an exception (soon you might see an int hurtling over your head!)
  • If you are doing exception handling in C++, you should always throw by value and catch by reference. Catching it by value leads to 2 copies (ugly) and catching it by pointer means you have to throw a pointer and that can lead to all sorts of memory management hell.
  • Stack allocation is faster than heap allocation, so avoid using the new operator if you can.
  • Wondering what the differences between references and pointers are? Well one is that you can always declare a pointer variable uninitialized and it's OK to reassign them to different variables. References NEED to be initialized to some variable, and like swans - they mate for life.
  • Finally in Java, (I know this but tend to ignore it while programming) use StringBuilder when repeatedly concatenating Strings, since doing something like str1 += str2 causes a new String to be built and assigned to str1 while its older value is garbage collected (they're immutable, remember)
We also started on the UVa Australian Voting problem. The logic seems pretty straightforward, once you're past the reading/ writing issues. More on that next week.