Monday, May 10, 2010

Signed and Unsigned

1. What is the output of the following program
main() {
unsigned int a = 10;
int b = -19;
puts((a+b)>0? "Positive":"Negative");
}

Answer : Positive
Explanation : When signed and unsigned arithmetic is done, signed is converted to unsigned and result will be unsigned, ANSI C standard.

Sunday, May 9, 2010

Linked List Example

Recently I was recalling my linked list concepts, and I found that I know the concepts, but when programming I am missing something. So I thought I should do it once again, so I made a simple program, which can add / delete elements from linked list. here it is for your reference.

shetty.h, this file contains the basic type definitions required.
#ifndef __SHETTY_H_
#define __SHETTY_H_

typedef struct le {
int ld;
le *next;
} le;

typedef struct lh {
le *next;
} lh;

#endif // __SHETTY_H_

Shetty.cpp, this file contains all the functionality required to add or delete from the linked list. The comments are self explanatory.

#include "shetty.h"
#include "malloc.h"
#include "stdio.h"

lh list;

/********************************************
** This function adds an element to Linked
** list, the head to list and element to be
** added is given as params.
** lst - pointer to the head of the list
** ld - an integer data, u can change this
** to whatever type you want to.
********************************************/
void add_list(lh *lst, int ld)
{
le *element;
le **tmp;
element = (le *) malloc (sizeof(*element));
element->ld = ld;
element->next = NULL;

tmp = &(lst->next);
while(*tmp!=NULL)
tmp = &((*tmp)->next);
*tmp = element;
}

/********************************************
** This function removes an element from the
** Linked list
********************************************/
void remove_list(lh *lst, int ld)
{
le **tmp, **prev;
le *deleted;

tmp = &(lst->next);
prev = tmp;
while(*tmp!=NULL)
{
if((*tmp)->ld == ld)
{
deleted = (*prev)->next;
(*prev)->next = (*tmp)->next;
free(deleted);
}
prev = tmp;
tmp = &((*tmp)->next);
}
}

/**************************************
** Display the list to see elements
**************************************/
void disp_list(lh *lst)
{
le **tmp;
tmp = &(lst->next);
while(*tmp!=NULL)
{
printf("%d\t", (*tmp)->ld);
tmp = &((*tmp)->next);
}
printf("\n");
}
int main(void)
{
list.next = NULL;
add_list(&list, 25);
add_list(&list, 30);
disp_list(&list);
add_list(&list, 45);
disp_list(&list);
add_list(&list, 55);
disp_list(&list);
}

Here, only one thing is important in dealing with linked list. i.e we need to get the address of the next pointer in a temp variable, and then change this variable with different address as we traverse the list. We should not modify any of the pointers in the list while traversing, unless we want to add or delete from the list.

Wednesday, September 16, 2009

Asynchronous Events Using Timers

Recently I was had an issue with Timer Call back function not working on Windows. I registered a simple call back function after 1 second, and it wasn't getting called, I was wondering how can it happen like that.

Then I did some study and found that, the windows will only send messages to your application that the timer is expired, and that message contains all the details such as the registered call back function etc, its the duty of the application to process these events and the call the windows API, DispatchMessage() which does the job of calling the call back function.

Here is the sample code, which works this way,
#include <windows.h>
#include <stdio.h>

int flag = 1;

void CALLBACK TimerProc(HWND, UINT, UINT, DWORD);

int main()
{
UINT_PTR id;
MSG msg;

id = SetTimer(NULL,
0,
3000,
(TIMERPROC) TimerProc);

while(flag)
{
GetMessage(&msg, NULL, 0, 0);
DispatchMessage(&msg);
}

KillTimer(NULL, id);

return 0;
}

void CALLBACK TimerProc(HWND hwnd,
UINT uMsg,
UINT idEvent,
DWORD dwTime)
{
flag = 0;
printf("I am in Timer Call Back Function \n");
}
So, if You are having the same problem as mine, please do follow this, and hope your problem is resolved, and do get back to me if you need any assistance.

The reason I think windows has been designed like this, is to make sure the call is made from the application context, hope that make sense, and I could not think of any other reason for doing it this way.

Friday, September 11, 2009

Inter-working with C/ C++ files

There are some times in the product development, especially in porting projects, and where we need to use the existing source codes, we may have to co-work with both C and C++ files, then we need to make them inter-work. Here I will show you how you can achieve that,

Accessing the C functions and data in C++ files.

supposed assume there is function func_c in a C file, my_file.c

void func_c(void)
{
    printf("I am a C function!");
}

to access this inside a C++ file, we need to declare it as shown below.

extern "C" void func_c(void);

The same methods can be followed for accessing all kind of data structures and variables.

Accessing C++ functions and Data Structures in C files.

Here things are other way round, just because C files does not take the key word extern "C", so all things need to be inside the C++ files, for C files its just another extern function or data.

So if I have a function func_cpp(void) as shown below, in my_file.cpp, it needs to be decalred as shown below

extern "C" void func_cpp(void)
{
    cout << "I am in C++ function";
}

To access this function inside the C file, just declare it as extern as you would normally do.

extern void func_cpp(void)

Hope it helps!

Tuesday, May 5, 2009

Windows DLL Programming

Creating a DLL is very simple, There are two files you need to create. The first is a DEF (define) file. The second is the CPP (C++ source) file. Add these lines in the CPP file of the DLL.

extern
"C" __declspec(dllexport) double AddNumbers(double a, double b)
{
return a + b;
}

and in the DEF file, add the following lines, in the EXPORT section. If more than one functions are there continue the numbering, (this is optional as we have already used __declspec(dllexport), but better to add here too.)

AddNumbers @1

Once the DLL is compiled it will create .lib, and .dll files in the debug folder, we need to add the .lib to the project in which we want to use that DLL, else we will get linking error. Once the linking is done, this .lib is not needed anymore, but we need to have the DLL in the same folder as that of our EXE, else the application wont be able to find the DLL, and will fail to load.

When we use a function from the DLL, the .lib file of that DLL need to be added to the project, and also the function need to be declared as extern as shown below.

extern __declspec(dllimport) double
AddNumbers(double a, double b);