Monday, February 27, 2012

Coding lessons: Memory allocation in C

How and where you store things is pretty important, C will take care or a lot for you, it's a pretty high level language, but it does give you pretty enormous power too, direct access to memory is just one of those powers.

In the last coding lesson (some time ago now) I covered pointers and how not only could we give variables names and use the name to access those variables but we could also use a pointer to read the variable from the memory location for the address of where the variable is stored.

In this lesson I'm going to cover a bit more about memory usage.

Allocating memory
To allocate memory we use the memory allocate function called malloc

Malloc will allocate a certain amount of memory during the execution of a program, the function requests memory from the memory heap (not the stack), when the request is granted that memory is reserved for the program.

    int main()
    char words[]={"My Malloc String\n"};
    char *p;
    p = (char *)malloc(sizeof(words));
    printf("p = %s\n",p);
    printf("words = %s\n",words);

I have introduced a ew function (sizeof())
the sizeof function returns an integer that is the size of the argument givern to is

char x[] = "hello";
int length = sizeof(x);

length = 6, five letters and the null terminator.

Anyway, the code, first we've created a string, -which we covered in earlier lessons.
then we declared a pointer.
then used malloc to reserve a space of memory that was the same size as the string, we then copied that string data into the memory that we'd reserved, (that pointer p pointed to).

(there is a bit missing from this program, so scroll to the bottom before you go crazy trying to figure out why your adapted code sample is doing funny things once you've run it loads of times.)

The next function that I'll look at is calloc,
Calloc is slightly different to malloc in that two arguments are needed.
with malloc you can reserve say 50 bytes by writing
pointer = malloc(50);

With calloc you reserve an array of memory, say for storing a list of numbers,
therefore you must give two dimensions (as you;re asking for a 2 dimensional array of space.
that's the amount of rows you plan to save, and the amount of bytes required in those rows.

int main ()
int row,n;
int * ptr_data;
printf ("Enter amount: ");
scanf ("%d",&row);
ptr_data = (int*) calloc ( row,sizeof(int) );
for ( n=0; n{
printf ("Enter number #%d: ",n);
scanf ("%d",&ptr_data[n]);
printf ("Output: ");
for ( n=0; nprintf ("%d ",ptr_data[n]);
free (ptr_data);
return 0;

You see this program, we see that we're going to enter a list of variables, but the programmer does not necessarily know how many items are in the list, so the user is asked first.
this program takes integers,

Enter amount: 4
Enter number #0: 1
Enter number #1: 2
Enter number #2: 3
Enter number #3: 4
Output: 1 2 3 4

With this sample run we can see that we have a memory allocation of 4 rows, and each row has 4 bytes. (because they are 32 bit numbers)
hence this list was stored in 128bytes of memory.

The other difference between malloc and calloc is that calloc initializes the memory, (writes zeros to all locations) malloc does not.
This means that with malloc you could feasibly read random old data from the memory heap.

The above example miss two very important things.
the first is error checking.
Neither malloc nor calloc guarantee that the memory will be allocated, if the system is out of memory then the system is out of memory and cannot allocate memory, in this case a NULL pointer is returned.
when using the function you should check for a null being returned, because things will not work as expected if you don't get the memory that you asked for!

if (ptr_data==NULL)
printf ("Error allocating requested memory");
exit (1);

The other thing that you should do, (and if you're using a loop with memory allocation functions) is free the memory that you have reserved, because if you don't you will run out of memory.

in computer terms a program that requests memory from the heap, then never returns it, only ever requesting more is usually thought to have a memory leak. eventually the program will consume all the available resource.

to return memory to the heap once we're finished with it we use the free function.

When we're finished with the memory that our pointer is looking at we just write


and that un-reserves and returns the memory ready to be used by other programs.

Monday, February 20, 2012

Electronics Basic Lessons: Home Automation

The last two lessons were pretty short and sweet, kind of a hey look you can get light dependant resistors, and temperature dependant resistors.

Now I'm going to use those to create a little environmental control project.

This isn't like some kind of fully fledged multi zone control thing. it's just a simple set of monitoring circuits that switch appliance outlets where you would plug in domestic equipment.

Lets define some component parameters of this circuit.
The light dependant resistor when fully dark has a resistance of 1Mohm, and when fully light will measure some 200 Ohms,
The Thermistor will have values that follow a linear pattern with temperature, 10 degrees = 1M, 20 degrees = 500K Ohms, 30 degrees = 250K Ohms
the transistors have a turn on voltage of 0.5v
The supply voltage is 9v

So lets start at the left side of the circuit and investigate the sorts of values of resistor that need to be used in the light sensor.

When the room is light, the LDR is 200 Ohms, and we need the base voltage to be below 0.5

0.4v = (9v/(?+200))*200

so we re-arrange that
0.4/200 = 9/ ? + 200
0.002 = 9 / ?+200
0.002 * (200 + ?) = 9.
200 + ? = 9 / 0.002
200 + ?  = 4500
? = 4300

We know that the resistor must be at least 4300 Ohms for the device to switch off the mains lighting when the LDR is in full brightness. the closest value would be 4.7K
? = (9 / 4700 + 200) * 200
? = 9/4900 = 0.36v
(so with a 4.7K we know for sure that the light will not turn on in full bright sunshine)

As well as the 4700 resistor (which we consider on it's own as we imagine that the variable resistor is turned right down and set to zero in full light turn on conditions)
We now need to make sure that we can set the turn on level over a wide enough range.
At full dark the LDR is 1M, and we need to make sure that the base voltage is 0.5v or above.

So we do the same equations again, (but we do have to include the resistor that we worked out earlier).
0.6v = (9v/4700 + 1000000 + ?) * 1000000
0.0000006 = 9v/1004700 + ?
(1004700 + ?) * 0.0000006 = 9
1004700 + ? = 9/0.0000006
1004700 + ?= 15000000
? = 13995300

So we need a variable resistor in the realms of 13Mohm to make sure that we can set the light not to come on in full dark, and adjust the light sensor to possibly keep the lamp switched on all the way up to near full light.

The Temperature sensing part of the circuit works roughly the same. but since the resistor / variable resistor and thermistor network is the opposite way around the equations needed are

transistor V desired = (v/(R Thermistor + ?)) * ?
So at 10 degrees we want the thing to turn on, the thermistor = 1 MOhm

This time we'll use a slighly different equation, instead of Vjunction = (Vsupply/Ra+Rb) * Rb we'll use the equations

Rb = Ra/((Vsupply / Vjunction) -1)

in this case :

Vrb + Rb = 1000000 / ((9 / 0.6) -1)
Vrb + Rb = 1000000 / (15 -1)
Vrb + Rb = 1000000 / 14 = 71428

So we want our fixed resistor for ensuring that the heater turns on in the cold (and with the thermistor turned all the way up, (which in this case will actually set the temperature lower!) to around 71K

For the it's definitely far too hot turn off the heater temperature (30 degrees) the thermistor changes to 250K
Lets put that into the equation, (again assuming that the variable resistor is now turned all the way up.

VRb + rb = 250000 / ((9 / 0.4 ) -1)
VRb + rb = 250000 / 22.5
Vrb + Rb = 111111

From this we know that the resistor Rb must be 111111 (11.1K)
And the variable resistor must be 71k - 11k = 60K

Monday, February 13, 2012

Electronics lessons: The Light dependant Resistor

Following on from the last lesson which was about temperature dependant resistors called thermistors, I thought I'd move on to discuss light dependant resistors.

One of the very basic competent in electronics is the resistor, (that was covered a long time ago.)
Fixed value resistors are good and very useful in a lot of applications, however, you find that other times you need something that will alter in response to external stimulus.

Just as the thermistor can change it's resistivity in accordance with temperature, the Light dependant resistor changes it's resistivity in accordance with the amount of light that falls onto a sensor.

The standard LDR is a round package, about 1cm across and 7mm tall, it has a window at the top of it where the light falls on it.

Light dependant resistors have a symbol that is the same as a symbol for a resistor, but with a circle drawn around it, with two arrows pointing towards that circle, (the circle may be omitted in some schematics.

As light falls on the light dependant resistor it's resistivity goes down, when the window is covered, or when the sun goes down/lights go off etc, the resistivity goes up.

Monday, February 06, 2012

Electronics lessons: The Thermistor

I thought I'd go a bit more back to basic in the next few lessons, and introduce some components.

One of the very basic components in electronics is the resistor, (that was covered a long time ago.)
Fixed value resistors are good and very useful in a lot of applications, however, you find that other times you need something that will alter in response to external stimulus.

Say something like a resistor that could sense the temperature?

Well this exists, it's called a thermistor.

It's a small component with two legs, in a bead shaped package (usually) it also usually has some banding around it that specifies what sort of thermistor it is.

Thermistors generally come in a variety of types the different types are able to handle different temperature ranges, but more importantly they set the nominal values of the thermistor.

If you're dealing with incredibly low signal electronics, a thermistor that operates in a range of Megohms may not be appropriate.

On the other hand, if you're using very large voltages, a small thermistor may have to disipate a lot of power and also would not be appropriate.

Thermistors generally work that the warmer they get, the less resistive they become, and the colder they get the more resistive they become.

Hence if your thermistor sits in a beaker of ice, you may get a very high reading when measuring the component, when the same component sits in a beaker of boiling water a much lower reading whoulf be expected.