Wednesday, September 29, 2004

state machines in C

In C++, state machines can use polymorphism, but C is not OOP so you might need another approach.

Tuesday, September 28, 2004

C and Dynamic Allocation


In C++, dynamic allocation is safer and easier to implement. In C things are not that easy or clear but they're more fun to program:

I was just playing a little with calloc functions and commented out the code enough so it explains it all well enough. I just used it with functions so that you can see where and how to use pratically dynamic allocation.

Check the code here...

It is easier to think about of it using abstraction at maximum, for example:

calloc( a , sizeof(int) ) will return a pointer to allocate enough memory for an array
of size a rather than trying to see those diagrams where the memory points to this and that.
Ok, after you getting this go ahead and try to grasp and grok those diagrams, but prior to it
you will know what it does...

Monday, September 27, 2004

C: Functions that return pointers and function that return values of pointers

It might be a little simple if you're used to C, but it is not actually. Let's say:

  1. int Horrors( int * );
  2. int *Horrors2();
  3. int main(void) {
  4. int y = 5;
  5. int *ptrF = &y;
  6. Horrors( ptrF );
  7. int h = 5;
  8. Horrors( &h );
  9. *Horrors2();
  10. return 0;
  11. }

Line 1 declares a function that returns and integer value. Line 2 declares a variable that returns a pointer. Normally you'd return a pointer to a structure, so it makes sense to make a function that returns a pointer.

  1. int Horrors( int *a ){
  2. /*Returns the value of a pointer*/
  3. return *a;
  4. }
  5. int *Horrors2(void){
  6. /*Returns a pointer*/
  7. int a = 6;
  8. int *ptr = &a;
  9. return ptr;
  10. }

Not that line 4 returns the value of the variable a whereas line 10 returns a pointer to a.

Wednesday, September 22, 2004

I give up


I gave up of this graphic library. I just don't feel too motivated to go further. I am researching for C programming for PIC , 8051 microcontrollers now...

My both arms still hurt and now my tooth is also. I am not sure it is named in English, but it is the Sisus tooth.. The last one..

Tuesday, September 21, 2004

How the graphic library code is


I thought I could avoid typedefs hehehe But I didn't get it..

Note that I had to implement a fake geninterrupt() as I am trying to get a similar one to run here.

Full code at here.



I was just for fun commenting C code using XML tags and wow, I didn't know it, but it looks like my browser parses it and does not display my tags as text or HMTL.

For example:


You should not see anything above if it worked. If not, something like:

[Horrors And Terrors] HORRORS[/Horrors And Terrors]

Should appear. Maybe the blogspot server parses the XML tags...

Let's us see..

A few seconds later...

YEAH the whole thing is parsed! You may find it me ignorant but I didn't know that really!

More code


I was looking after an Assembly routine to write a pixel inthe screen when I found this article. Despite a little old, it presents a logical sequence to accomplish the task. However, as it was written back in 1994, I suppose the author didn't use DevShed C++ to program it, so the function where he access the interrupts of the video card simply does not work ;-) As I say in a comment below, I am going to use my FREE and OLD C compilers IDEs : How to put a pixel in the screen by Mr. Grant Smith .

This whole code relies on a function, that does not work on WINDOWS, so let's find it or implement it ! :-)

I have added the code supplied above.

C /Assembly code for graphical pixel

I was checking the web yesterday when I found this excellent article:

It basically teachs you to program a routine to write a pixel to the screen. I will try to dig it deeper.

In the meantime, I just will search for writting a pixel on MSDN.

Monday, September 20, 2004

More on a graphical library

I've been working on a simple graphical library and I have more of it now. It has to get much better though. I am not using typedefs or Macros as I am trying to make it clear to the user. I am currently converting some parameters to use the structure point..

During the weekend

Besides playing a little with C# on WIN98, I actually spent time with my son and wife. I also listened to lots of great music, part of it had my son with me. Bozzio Levin And Stevens, Jethro Tull, Tempest, Van Hallen, Sophistree, Peter Gabriel, Aghora and a few more.

It looks like my 1 year and eightmonths son, digs rock and roll! WOW...


Besides, I watched an excellent film, which was "25th Hour" by Spike Lee. Man, I really enjoyed the films I saw by Spike, but this one ranks with "Jungle Fever". Also, the soundtrack by Mr. Terence Blanchard is superb. I recommend this 2002 movie and I am not sure why most of Spike's films are absent in the video/DVD renting stores. Also, saw a "Circ D'Soleil" film on HBO, which rocks like hell. I am not a big Circus fan, basically because I find putting animals to make funny things too stupid. But "Circ D'Soleil" does not have animals besides humans. I think I enjoy it more because of the live music or because those artists are really great at their jobs.

On Friday I saw the last "Carga Pesada" episode. It is a Brazilian TVSHOW. In the 70s/80s they were a popular series here and there's a 2003/2004 season going on TV. But it was cancelled because they want that "The City of God" crew new series to replace it. I find it too sad, because "Carga Pesada" in both eras, is one of my favorite series. Two truck drivers travel through the Brazilian country and find lots of good stories to interact. It has two of the best actors in Brazil, named Stenio Garcia and Antonio Fagundes and plots that go beyond the regular and average Brazilian scripts.

More of Mordor

It is fun that I call Microsoft "Mordor" because I actually like some of their software, basically:

Word, Excel, Visual C++ and C# and I might add WIN2000 too.


Anyways, Mordor says about .NET:

System Requirements

  • Supported Operating Systems: Windows 2000, Windows Server 2003, Windows XP

Terrified WIN98 home user like me? Yeah, if you don't plan to run .NET pages, .NET runs OK. I can't say what does run and what not. The article where I learned how to do it (which is plain simple) was written in 2000, and I am not sure whether the author updated it.

As I already mentioned, I installed the new version of Sharp Develop
and it runs perfectly on WINDOWS 98 and it's more stable than the other versions I have used.

My home set is a AMDk7 1.2GHz on a Soyu motherboard, 32Mbytes of Video memory, 128Mbytes of RAM. I actually only need an upgrade on RAM memory but for programmers like me, super mega ultra processors won't make difference. I am from a time, I had to run Delphi image processing algorithms on P1 or 486 and a machine that had 256 Mbytes of RAM was unique.

Blind .NET and Microsoft users will argue that it is an heresy to install .NET on WIN98. They will also point out that I am wasting time not focusing on web programming. I actually don't care. Asked to do web programming I'd rather use JSP/Servlets or I would try to learn PHP (with pleasure) rather than using .NET. Although I do enjoy C#.

"Horrors and terrors", should I quote a friend of mine...

Sunday, September 19, 2004


I will have more of this later, including the official thoughts from Mordor, er, microsoft. However, I could compile and run C# code on Win98! Probably ASP.NET won´t work as it relies on IIS. I have to check whether ADO.NET will ork also. But now you can program C# on WIN98 , but the funny aspect is that always could do it..

The article I previously mentioned here explains it all. It is not hard, open the installing .NET file with WINZIP or WINRAR. Next, just unzip the files and run the exe install.exe. Prior to it, be sure to have installed that msi installer .

I am running sharpdevelop right now and btw, the new version is considerably much better and stable...

Friday, September 17, 2004

Simple Events

If you haven't read the previous post, I hope you read it first.

The way C# Handles events is very clear. You first instantiate the component:

this.button1 = new System.Windows.Forms.Button();

Next, add it to the container. In this case I just add it to a groupBox component. I will have to add the groupBox to the Form of course. Actually, by using C# Builder this is all done automatically :-)

This code below sets the button properties and the last line is the one that matters:

this.button1.Location = new System.Drawing.Point(152, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Calculate";
this.button1.Click += new System.EventHandler(this.button1_Click);

By doing it, the compiler knows that you created an Event to the component. The Click event is the one in this case.

With Java, you would have a real complicated task for adding a handler, by either using an inner class or creating a separate class. With C# it gets easy:

private void button1_Click(object sender, System.EventArgs e)
this.label1.Text = this.textBox1.Text;

The System.EventHandler( ) class recieves as parameter the method implemented above. It simply passes the value in a textfield to a label whenever you click the button. If you know me, this the program I always to when learning GUI programming for a certain language. :-)

The code that is automatically created when you start an application with C#

When you launch C# Builder you get an automatic code just exactly any Borland RAD tools like J Builder. However, their code is simply optmized. I just launched it and want to comment the code, this will help me and perhaps you to get it:

First, these imports make a call for the proper classes:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

The solution is to put your project in a namespace, rather than naming it after the class name. MS and SUN do it because they think applications lie in the web so that it is possible that two applications have the same name.

Note the comments using /// and XML code, I didn't know this was possible, but it makes sense. I usually document my codes using XML, in hope that an ALIEN in the future parses my code :-)

namespace Gui_Sharp
/// Summary description for WinForm.

public class WinForm : System.Windows.Forms.Form
/// Required designer variable.

private System.ComponentModel.Container components = null;

WinForm is derived from the class Form, which is basically the typical way of doing it
in MFC or Swing, you have a top container and derives it.

It looks like that you may free memory from unused components with the the last line.

public WinForm()
// Required for Windows Form Designer support

// TODO: Add any constructor code after InitializeComponent call

It is cool to initialize most components on a method not in a constructor, as it will become a huge mess, when you start drag and dropping components over your form...

/// Clean up any resources being used.

protected override void Dispose (bool disposing)
if (disposing)
if (components != null)

This code will released memory after the programming is closed.

#region Windows Form Designer generated code
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.

private void InitializeComponent()
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "WinForm";

this points to the current class/object which is the form. With it you may set its properties.

/// The main entry point for the application.

static void Main()
Application.Run(new WinForm());

You need an static method to run the form as an application.

If you ever want to code using notepad cut this code and paste it. if Borland does it that way there's a possibility of being the best way of initializing a basic and simple form.

With C# you can create an executable, so despite performance issues, C# is much better than MFC ...

Lazy bastard I am ;-)

Thursday, September 16, 2004

C# on WIN98 and NT

This article claims that it is possible to install .NET on those two operational systems. I haven't tried it yet, but it looks possible. This strategy is related to the fact that MS does not want to give support to WIN98, forcing users to upgrade. The JVM runs on WIN98 and NT so it is not make sense not allowing that C# runs on Windows 98. Maybe all that web programming crap named ASP.NET does not work well, but who cares?


It stands for Graphic User Interfaces. When Windows was first released in 1985, C was the main programming language and C++ was a new tool. Although OOP was an old concept, for Windows and Microsoft C was the "lingua franca". So, the WIN32S API was conceived as a layer sitting at the top of DOS and Screen controllers boards.

Maybe there was a low level C code to write a pixel to the screen, by using an Assembly routine that lighted on the particular pixel with a particular color. Suppose this can be encapsulated in a simple routine :

void itWillLightAColorOnTheScreen( struct color c , int* x , int* y );

How could this function be implemented? The processor sends a signal to the screen board, we will name it a message. "Hey dear board, check if the pixel is on or off. If not, light on a pixel there with this color." Next, pulses are sent, the microprocessor at the screen board recieves the message (acknowledge it) and puts it in its stack of tasks. Next responds "Dear microprocessor, now I can put that pixel on the screen!". Next it will try and when it gets it responds "I got it bro!". So we have a pixel in the screen. I have no clues of how many lines of Assembly code this would require (I can check at my book at home if you need ;-)) , but assuming WIN32S coders arguments would make us thinking that "true coders" would only write GUIs in Assembly, as it sits at the top of the C WIN32S layer.

Well, let's assume we have that particular function. A code to write a simple line goes below:


struct color {
char* colorname;
int colornumber;

void itWillLightAColorOnTheScreen( struct color c , int* x , int* y );

int main() {
struct color c;
int i = 0;
int j = 0;
int InitX = 234;
int InitY = 234;
int FinalX = 500;
int FinalY = 500;
c.colorname = "Blue";
c.colornumber = 234;/*234 is a number picked up randomly ;-)*/
/*Now we are going to create a line*/
for ( i = InitX ; i < FinalY ; i++ ) {
for ( j = InitY ; j < FinalY ; j++ ) {
itWillLightAColorOnTheScreen( c , &i , &j );
return 0;

void itWillLightAColorOnTheScreen( struct color c , int* x , int* y ){
/*Do the thing*/
printf("Good man, I have written that color to the screen!");

Now, how do I create a window? Write four lines and connect them. Easy ? Maybe. Next, one should write code to map the serial port where we have a serial mouse. This mouse simply sends a signal to the processor to indicate where the user would enjoy to place the mouse in the screen. Or:

Input( Xuser, Yuser)------> function that maps these coords to the screen and informs the

If the user presses the button, we assume its state changes to "1", so that the processor might consider that the user wants to launch an event. Next the program should associate the event to the graphical representation and launch the desired event. Thus, it is huge and complex task when we add mulithreading issues, or, the processor simply has lots of other things to handle.

Suppose a programmer shoudl rely on Assembly code and low level design, one would take decades to create a simple program. So the C WIN32S API comes at hand. But then, it is also very complex and it generates large code.

Well, Microsoft encapsulated it in a library, an OOP library named MFC. Microsoft Foundation Classes, which puts WIN32S code (structs, functions, properties, macros) into the OOP standard, so that C++ can come and make it simpler to read. But then, MFC sits at the top of the WIN32S layer and frankly man, it still has some bizarre things like resources , macros and typedefs.

If you want to dig deeper how to create OOP code from C code, I recommend this site:

SUN just encapsulates the WIN32S API in an API named Swing, which is an OOP library. However, it loses in terms of performance. However, it is an excellent enviroment for GUI. I can say the same for the other OOP APIs I know, that are Python's Thinker and C# graphic APIs. However they sit at the top of the WIN32S and that low level Assembly code I mentioned.

As an Engineer, I find the solutions that are easy for me and have better results. So, I don't feel like knowing WIN32S or MFC. Charles Petzold, who's known as the WIN32S GUI guru simply says "I prefer C# APIs". Who Am I to disagree? For microcontroller tasks, non GUI and fast calculations C is still the top one. But come on dude, C for GUI development is too much :-) I like RAD tools, especially Borland ones like C# Builder or J Builder. But coding at hand it is funnier. But slower than doing it via RAD ways.

Wednesday, September 15, 2004

C# Builder Personal

I have installed the C# Builder software personal edition from Borland. It looks more stable than Sharp Develop .

However, you should not use C#Builder for commerical applications.

Monday, September 13, 2004


Why do I like C# and I am not that fond of .NET?

It is hard to say. I love the C# Syntax. It's even cooler than Java (although I prefer Java because of its libraries and APIs) because it has C++ features that we enjoy. You can create exe files. But I am not probably doing web programming with C#, neither would rely on the XML technology to create web services. I abhor VB.NET and ASP.NET programming, not because it is hard or easy, just I like desktop programming better than web programming, and as I am not paid to program in C# I do choose what I like best :-)

Despite of what anyone else says, I will use C# as a tool for math, plotting graphics and maybe windows sockets.

Thursday, September 09, 2004

As everybody knows ...

The Genesis probe crashed.... :-(

At least those probes in Mars are running Java, heh ;-)

Visual C++ 2005 Beta


Can you believe? I knew it from an Orkut C++ community: Microsoft is distributting Visual C++ 2005 Beta for free, yeah, free! I could not download it yet, I am not sure if that's due to security reasons (with MS the coolest downloads just go in that background way, so you can't download a file and install later) or due to lots of people trying the same. You can get the CD of course, 20 dollars. Microsoft Brasil does not distributte it, so they told me.

I once read an article that pointed that the Visual C++ guys at MS are not totally .NET dudes, so I am not surprised that Visual C++ is one of their best softwares ( with Word and Excel ).

I am actually totally fond of C#, not for XML, yadda yadda web services, ADO.NET, just simple and plain code and GUI. I am not totally happy with .NET but C# rocks...

Wednesday, September 08, 2004

"Golden Apples Of The Sun"

In 2001, the Genesis (cool name huh?) was sent to grab particles of the SUN. If you ever read "Golden Apples Of The Sun", by Bradbury, you will enjoy this more than anyone besides particle physics researchers.

As of today, September 8th, the probe is coming back to earth bringing together particles of the Sun...

Sunday, September 05, 2004

Python x Java

I am not really trying to analyse it deeper, just put random thoughts regarding what I have felt about these two languages.

Python does not require that you declare data types, although requires that you initialize it. As I am used to declare any variable, I had to change a little bit to enjoy the Python way. I did like the numarray package, as it provides lots of functions for manipulating arrays. For engineering purposes, having a strong array library is very important.

The numarray API already overloads operators like +, * and math operations, which is also great. If we see this way, Python is perfect for science. OOP programming in Python is easy, although I saw no references yet to the use of polymorphism in the same way we can do in Java or C++. For ex:

Foo * f = new Foo( );
Foo *f2;

Foo f = new Foo( );
Foo f2;

In either languages if you do f = f2, you make the pointers to reference the same objects. Also, if you have a function that accepts objects with an heritage hierarchy, the compiler knows at run time which object you're passing. Suppose for example, you have a class that represents a virtual world. And you have zillions of beings that are derived from a base class. Without polymorphism, If you were to implement a method that used any of these beings as parameters you would end up with an implementation for each. With polymorphism, this is not required. Suppose you have: Being->Warrior->Wizard->Preacher->Worker and any of these have a function named boolean Eat( ). Suppose in your code, there's an specific event where you need to pass an object and inside this method you make use of the Eat( ) function. Your implementation would be simple:

void IWilluseThatFunction( Being b){
if ( b.Eat( ) ) {
//.. do this
else {
//do that


I actually don't mind if Python does not allow it. I have implemented several simple non OOP functions in Python (simple enough, that everybody might have done it too) and it is too clear and direct. Suppose I would require a library with functions Jaca1, Jaca2...Jacan. In the OOP way, I would require an instantion of a class with the proper parameters and then I could make use of the functions. The non OOP way simple allows that I call every function passing the proper parameters. If I am just trying to get results from a data and I will not use them in another block of code, I'd not use OOP.

#OOP way
JacaObject = Jaca(HereIPassAXMLFileForEx)
#now I will have to access its methods
JacaObject.Jaca1( );

#Non OOP
from JacaCollectionOfFunctions import *


I did like the Tkinter GUI library that Python has built in. I found it faster than Swing. And both Swing and Tkinter make use of a similar OOP model.

Perhaps tempted, I would say that I still enjoy Java better, but Python comes close.

NP: DIO "Magica", excellent metal album!

Saturday, September 04, 2004

It is not an easy world


Everybody has  have seen the massacre in Russia. There are no acts that justify the killing of innocent. Not even killing more innocent children. We know that the Russian government has acted in same vile ways to the Tchechenian people but nobody will judge their actions legit  movements to save their nation now. Instead, they are now, just another group of terrorists, that do not want anything else besides causing sadness and horror to achieve their goals.

I would not be surprised if the Russian government responded it with more force.

In the meantime, in Brazil, drug dealers, robbers and kidnappers are doing their job. Politicians still take away money from the fees. Our president lies in his own castle of illusion and leaves the country to his colleagues  just allowing them to implement bizarre actions that evoke socialism mixed with neo-capitalism.

Nobody gives the proper importance to technology. They plant sugar cane and sell it abroad. This is our curse, or has been our curse since 1500, plant and sell sugar. We make the cakes and pies all around the world but we destroy our natural resources. We drive cars with alcohol-based engines but the nature is getting dry. The town I live is the ultimate example: the biggest sugar cane production from this world comes from Sertaozinho-SP and the weather and the nature there, practically look like like a desert. A sugar cane desert....

A moment of silence for this world and for the children that died in Russia...

Thursday, September 02, 2004

More of DIO

I have listened to "Inferno: Last In Live" and "Magica".

The first one: Excellent live album full of favorite songs of mine. My son and I actually headbanged to "Long Live Rock And Roll" before his bath. The kid is only 1 year and 7 months but has a future :-) !

"Magica" is another excellent album, with a good story and Craig Goldy performs pretty well.
It is great to see Simon Wright (former AC/DC drummer ) playing with DIO. The original DIO bassist also plays at "Magica".

btw, Steve Vai at Orkut, added me in his friend's list. Although I am not sure that's Vai, I am finding that it is possible to be...

My friend Chang and his wife Yi Wei are coming home tonight. They will cook Chinese "pasteis" and my wife will cook "canjiquinha". I think this is gonna rock. Yeah, my wife and I will have to forget the diet for at least today...

Music Advice: Steve Vai "Sex And Religion", my favorite Vai.

Wednesday, September 01, 2004

Listen to Dio

DIO rocks...

I have bought two cds a few hours ago, I am listening to them at the job, so it is not an accurate listening...

Anyway, the sound of "Magica" evokes the 80s metal that Dio was one of the most import artists.
Singer with no equal in metal, always was a great composer and song writter...

I have "Holy Diver" on LP for nearly 20 years and it still impresses me. This is actually one of my magic albums. I am not thinking much about complexity, just good riffs and excellent vocal melodies and a real decent story. You hardly get that from these new melodic and power metal bands, but with Dio, you get the finnest metal art...

I liked the Craig Goldy riffs. Just like Vivian Campell and most guys that played guitars for Ozzy in the 80s, they have a similar sound, but quite cool. You know those inversions, playing 3 notes chords keeping the root the same, just playing it open..

Man, I miss the 80s. The music at that time was better, even the pop music.

NP Dio Magica