Author Archive

C, Source Code

Growing String in c

This article contains the source code for a c utility that manages a growing string, and a brief explanation of the code.

To obtain this, we make something similar to a c++ class: a struct with associated functions. The functions add characters to the string in the struct, and allocate more memory space when the string is full.

This is usefull when we are working with string with an unknow length (e.g. we are reading a file).

The struct containing the string is the following:

// Struct containing the growing string
typedef struct
{
	// string
	char * text;
	// string length (with zero terminator)
	int length;
	// string capacity
	int capacity;
} growing_string;

The text variable is where the string is allocated, length is the current string lenght and capacity is the memory allocated for the string.

To better understand the difference between length and capacity, look at the next image:

Capacity-Length

Capacity is the total space allocated for the string, while length is the actual number of character of the string, including the \0 terminator (the special character that close the string). If we add enough characters and the length is greater than the capacity, we need to allocate more memory for the string.

Continue Reading »

Quiz

Coders4fun Quiz #4

Main.java

public class Main {

  public static void main(String[] args) {
    System.out.print(Quiz.GetMagicNumber());
  }

}

Quiz.java

public class Quiz {

  static int magicNumber = 3;

  public static int GetMagicNumber()
  {
    return magicNumber;
  }
}

What we need to add in Quiz class to print 5 instead of 3?

Quiz

Coders4fun Quiz #3: Will it compile?

Will this code compile??? Why?

foo.h

class Foo
{
	public:
		Foo();

		void fooo();
	private:
		int i;
		char c;
		short s;
	protected:
		char b;
		char e;

		char getFoo();
}

foo.cpp

#include "foo.h"

Foo::Foo()
{
	for (int i = 0; ; ) { }
}

void Foo::fooo()
{
while(true)
{
	c = 'f';
}
}

char getFoo()
{;
	return c + 2;
}

C, Source Code, USBAutoStart

First public version of USBAutoStart!

USB AutoStart

Usbautostart is a Windows utility that performs autoplay for usb mass storage device. Windows avoid the autoplay on the removable medias for security reasons, but with this program you can bypass this limit and you can choose to launch a program in the usb pen when you plug it.
The program will also close all applications running from the device when you try to “safety remove” it.

Screenshots:

Usb peripheral detection (the red icon on the left is Mozup, the program started from the usb pen):
USB AutoStart Inserting device

Usb removal:
USB AutoStart Removing device

The project is hosted on sourceforge, and it’s released under GPL license, so it’s completely free and open source! :)

Download Usb Autostart

Quiz

Coders4fun Quiz #2

This quiz is not original and isn’t about programming but I think it’s funny, so I publish it! :D
There is a pullman with 7 girls.
Every girl has 7 rucksack.
In each rucksack there are 7 big cats.
Every big cat has 7 small cats.
Every cat has 4 legs.

How much legs there are in the pullman?

C, Quiz

Coders4fun Quiz #1

What will be printed using this c++ code?

#include <iostream>

int main()
{
	int i = 0;
	for (i = i == 0; i < 9; i = 1 + i * 2);
		std::cout<<i<<std::endl;
	return 0;
}

Please don’t cheat using a compiler! :)

P.S.
Of course the example compiles!

C, Source Code, UDP Communication

UDPListener: Receiving UDP messages in C#

This post is part of the UDPCommunication project.

UDP - User Datagram Protocol

The class in this post allows, in an easy and fast manner, to receive UDP messages. You don’t need to be a socket expert to use the class, cause you only need to write few rows of code to use it. Following, a little example showing how to use the class:

private void InitConnection()
{
	m_udpListener = new UDPListener(m_port, new byte[4] { 0, 0, 0, 0 });
	m_udpListener.MessageReceived += new UDPMessageReceivedDelegate(udpListener_MessageReceived);
}

Continue Reading »

News, Site

W Firefox! :D

This post is dedicated to all non-IE users of coders4fun:
firefox batte ie

THANKS!!

C, Source Code, Windows

Reboot or Shutdown a pc

This is a little how-to that explains how to shutdown or reboot a Windows machine in c++ .
In the source code you will find the function ShutDown, definied as follow:

bool ShutDown(bool restart)

The function shutdowns or reboots the pc, and returns false if the shutdown can’t be done (for example a process denied the power off).

Shut Down

So, to shutdown the pc, you only have to call the function and check if the shutdown was done.

if (!ShutDown(false))
{
	// shutdown not done.
}

Part of source code following:
Continue Reading »

C, Source Code, UDP Communication

UDPSender: Sending UDP messages in C#

This post is part of the UDPCommunication project.

Header UDP

This is a class that allows to send UDP messages in C# with an unbelievable semplicity.
To create the class you only need to create it, specifying a port for the udp communication, eg 3125 in this case:

try
{
	INetworkSender udpSender = new UDPSender(3125)
}
catch(UDPSenderException e)
{
        	// error during the socket initialization
}

Sending a message to an host is simple: call the SendMessage method specifying the receiver (with an host name, like “www.coders4fun.com“, or with an ip, like “192.168.10.6“) and a byte array containing the message.
Continue Reading »

Next »