Subscribe for email updates

Get all updates to your inbox to stay tune.

Curve tracing programs - Draw sin(x) curve using Graphics programming in C

Learn how to draw curve tracing graphics programs in c language

Graphics programming in C

Learn how to write basic and advanced graphics programming in c language

Curve tracing programs - Draw Cos(x) curve using Graphics programming in C

Learn how to draw curve tracing graphics programs in c language

Make file tutorial

A step by step guide to learn how to write make file in Linux with examples

Thursday, December 8, 2011

C programming best practices

 this article describes some of best practices in C programming. Will be useful.
check it out.

Click here

Friday, December 2, 2011

MALLOC Failure Test Program in C

Wanna test malloc failure without changing the binary of the application. [Works good for C]

Try the below program. 

#############
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <unistd.h>

static int mCnt;
typedef void* (*MallocFn)(size_t size);

MallocFn mMalloc;
extern "C" {
  void* malloc(size_t size)
  {
    mCnt++;
    if (!mMalloc){
        mMalloc = (MallocFn)dlsym(RTLD_NEXT, "malloc");
    }
    if(mCnt >=20){
      printf("MyMalloc is called\n");
      return NULL;
    }
    else{
      return mMalloc(size);
    }
  }
}
 
#############
compilation : gcc -shared -ldl -fPIC mallocTest.c -o libMyMalloc.so
before running your application, export the LD_PRELOAD evn.
export LD_PRELOAD=<absolute path of the libMyMalloc.so file>
run the binary or application. 

    if(mCnt >=20){

Above line controls the malloc behaviour, when application uses 20th malloc onwards, memory will not be allocated.


Regards 
-Praveen

Thursday, December 1, 2011

Regular Expression in C [gnu library]

How to use regular expression in C

Sample Program. 

################# 
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#define RE_DIGIT     "^[0-9]*$"
#define RE_ALPHA     "^[a-zA-Z]*$"
#define RE_ALNUM     "[[:alnum:]]"
#define RE_IPV4      "^((([2][5][0-5]|([2][0-4]|[1][0-9]|[0-9])?[0-9])\\.){3})([2][5][0-5]|([2][0-4]|[1][0-9]|[0-9])?[0-9])$"
int main(int argc, char **argv) {
    int result;
    regex_t rx;
    regmatch_t *matches;
    char buf[1024];

    result = regcomp(&rx, RE_IPV4, REG_EXTENDED);
    if (result != 0) {
        fprintf(stderr, "Invalid regular expression: %s\n", buf);
        regfree(&rx);
        return -1;
    }

    matches = (regmatch_t *) malloc((rx.re_nsub + 1) * sizeof(regmatch_t));
    if (!matches) {
        fputs("Out of memory!\n", stderr);
        return -1;
    }

    for (;;) {

        fputs("Enter IP Address string (empty line to exit): ", stderr);
        memset(&buf, '\0', 1024);
        if (!gets(buf) || !buf[0])
            break;

        result = regexec(&rx, buf, rx.re_nsub + 1, matches, 0);

        if (!result) {
            int i;
            if (matches[i].rm_so != -1) {
                fprintf(stderr,
                        "Match %2d from %2d to %2d: \"%.*s\"\n",
                        i,
                        matches[i].rm_so,
                        matches[i].rm_eo,
                        matches[i].rm_eo - matches[i].rm_so,
                        buf + matches[i].rm_so);
            }
        } else {
            regerror(result, &rx, buf, sizeof(buf));
            fprintf(stderr, "No match: %s\n", buf);
        }

    }

    regfree(&rx);
    fputs("Goodbye\n", stderr);
    exit(0);
}

################ 
-- 
-Praveen