Showing posts with label Computer Science. Show all posts
Showing posts with label Computer Science. Show all posts

Monday, 14 November 2011

C# vs C++ syntax, round 1

While I was trying to learn F#, I logged on my project euler account to start with simple algorithms to practice. But when I looked the first problem, which I've solved a couple of years ago in C++, I couldn't help myself stopping putting my hands on the code editor and solving it in C# using Linq. 

The description of the problem:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.


The solution in C++ is something like

int problem_01( )
{
int sum = 0;
for (int i = 0; i < 1000; ++i)
{
if ((i % 3) == 0 || (i % 5) == 0)
sum += i;
}

return sum;
}

... and the equivalent in C#


public static int Prob01()
{
  return 
    Enumerable.Range(1, 999).Where(x => (x % 3) == 0 || (x % 5) == 0).Sum();
}

For me it's clear which one is more concise and better express what I'm doing.

Before I forget it, the equivalent in F# can be implemented like that:

let result = List.sum(List.filter(fun x -> x%3=0 || x%5 =0)[1..999])



Sunday, 3 April 2011

Pointers part II, learning how to do addition and subtraction again :-(



Last time I wrote about pointers I think the real use for then wasn't clear. I think now it can start to make more sense.

But before get to point I want, more theory makes necessary :( !

Pointer arithmetic

With normal numeric variables the result of simple operations like addition or subtraction are quite predictable if we know the contents of such variables. However this rule doesn't apply for pointers.

Pointers don't store numbers, but memory addresses. So if a pointer points to the mem. address 0x98456732 what happens if we add 1? The answer is, it depends! It depends on the pointer's type and how many bytes does that type needs to represent its information in the memory.

For example, on my 64 bits laptop using Visual Studio, a int variable has a size of 4 bytes, a double, 8 bytes while a char only 1 byte.

So when summing or subtracting pointers, this must be taken into account. Lets have a look in one example:


#include "stdafx.h"
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
1 int x = 10;
2 int *ptr = &x;
3
4 int address = (int)ptr;
5 printf("\nThe pointer points to %X == %X\n", ptr, address);
6
7 ptr++;
8
9 int difference = ((int)ptr) - address;
10
11 printf("%d bytes were added to %X\n", difference, address);
}


In this program at line 1 an int variable is declared and initialized with 10. In the line 2 I declared the pointer and initialized it with the address of x. Then at line 4 the address of x is assigned as a normal integer to the variable address. The printf at line 5 shows that the value stored in ptr and address are the same. Finally at line 7 the pointer is incremented by 1 unit! So now what's the value of the pointer? The answer is: the new value is equal to the original memory address plus 4! What lines 9 and 11 do is just proving what I'm saying. But why 4?

Let's call the memory address number being pointed by ptr as addr_number. The new address number obtained after the addition can be thought as:

addr_number + 1* sizeof(int) == addr_number + 1*4 == addr_number + 4
So for every arithmetic operation with pointers, we have to consider the size of the type which the pointer was declared. The same applies for subtraction. About multiplication and division, I've never seen or used it in practice but I guess the same logic applies.

Sunday, 20 February 2011

Pointers, it was once...

In these last 10 years working as a developer or teaching in a Introduction to Programming discipline, I've found many people struggling to understand pointers properly. Many found it a very abstract subject but the truth is it is not all that difficult to grasp.

I'll try to remember as many doubts I had listened to in order to try to write about then clearly.

I think first it worth have a look again in how variables work (in a more illustrative and informal way). First a variable has a type, which determines how the bytes stored in that variable will be interpreted. The type also determines the amount of memory necessary for that variable.

Let's say we want to declare an integer variable (in C):

int x;
This variable (assuming it is an int 32 bits) will use 4 bytes of memory; this means that it can store up to 232 possible values (not all of then at the same time obviously). And what about the memory, how is it being used by the variable x? Let's think of the main memory as a huge array (once again I want to be informal) divided in blocks of 8 bits - 1 byte. As an array, each byte can be accessed by an index, actually each byte in that 'array' has an unique index called memory address through which we can access the contents stored in that piece of memory.

Let's imagine that 
the memory address 0x100 has been reserved for the variable x  - that piece of memory would look like this:



As said before, the variable x, as a 32 bits integer, is using 4 bytes starting at the address 0x100, which we say is the address of that variable. So, knowing the start address and the type, we can 'visualize' the memory space used by that variable and verify that it uses four consecutive bytes starting at 0x100.

If we assign a value to x, say 27 (0x1B in hex), the memory space would look like this:

x = 27;


The LSB (Last significant Byte) is in the smallest address of the four bytes being used by the variable x. This is due to the fact I'm considering a Little-endian architecture in this example - used by popular PCs nowadays.

Now that we have a basic idea about variables, let's talk about pointers. In some extent, pointers are like normal variables. They have a type, a name and need memory space to store the data assigned to it.

But this starts to change a little bit when we look on what type of information does a pointer variable stores. Basically a pointer stores a memory address. So in part we can think on it as a index variable (remember that we can think of the memory as a big array). This memory address can be the address of dynamically allocated memory, of a variable and even the address of a function - which we can talk about later in another post.

In a 32 bits architecture an memory address is a number that uses 32 bits of space, so a pointer will use 4 bytes. Let's go to a simple example. Considering our x variable - whose address is 0x100 - lets assign its address to a pointer

int *p = &x;
Before going any further, lets briefly talk about the symbols being used here. When declaring a pointer variable in C or C++, what distinguishes such a variable from a normal one is the asterisk (*) symbol put on the left of the variable's name. The ampersand symbol (&) when put on the left of a variable is used to get the address of that variable. So in this single line we are declaring a integer pointer called p and assign to in the address of the variable x.




In our picture now, what we have is a int variable x, whose address is 0x100, and the int pointer p pointing to the same memory address. Now, as the pointer 'knows' the address where the variable x stores its data, we can manipulate the data indirectly using the pointer. For instance consider the line bellow:
*p = 30;

What happens is that the variable x no longer has the value 27, instead we change its contents to 30 using the pointer.

Before ending this very basic discussion about pointer, a little bit of C, C++ symbology.

int *p
Here the * symbol is being used to say the p is a pointer (int pointer)

p = 30;
A pointer always interpret the data assigned to it as a memory address. So in the case we are telling the pointer to point to the address 30 - which is likely to cause problems and seems wrong as we are not sure what is being store there.

p = &x;
Now we are assigning the address (&) of x into p - note that p without the *, as written above, everything we assign to a pointer will be treated as a memory address.

*p = 20;
Now look the asterisk! It tells the pointer to assign 20 into the memory being pointed by it, we are not telling the pointer to point to the address 20.

So the asterisk when used in a variable declaration, tells the compiler that it is a pointer. However when used in the middle of the program it tells to the pointer: assign this value in the memory being pointed by you!

I think it is enough by now. More coming soon...