Skip to main content

Posts

Initialize a char array/C-String

char *Type = new char[10]; Say you have a char array in heap. You want to fill this array with something; say "Access". If you do; Type = "Access" This would be a memory leak!!!! What happens? You'll get a warning/error; error : deprecated conversion from string constant to 'char*'   but this is not regarding the memory leak. What actually happens is that the statically allocated string literal will be assigned to the pointer Type, i.e. Type will now point to a different memory and you have no handle to the new'ed memory you actually allocated by new char[10] !!! Ouch! The proper way to do this is; strcpy( Type, "Access" ); But the actual error/warning is because the actual type created by saying Type="Access" is a const char*. The string literal "Access" is usually not inteded to be modifed by something like Type[0] = "a". Fix would be to do; const char *Type = "Access";

sprintf, snprintf, strcpy, strncpy and sizeof operator

"C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking." "snprintf is safer than sprintf" What do these statements really mean? int sprintf ( char * str , const char * format , ... )  int snprintf ( char * s, size_t n, const char * format, ... );  char * strcpy ( char * destination, const char * source ); char * strncpy ( char * destination, const char * source, size_t num );   The usage is something like; char* msg1 = new char[10]; strcpy(msg1, "test"); // 1 char buffer[128]; sprintf(buffer, "%s", msg); //2 strcpy : Copies bytes until it finds a 0-byte in the source code. The string literal "test" has 4 characters and a terminating null character at end, therefore needs 5 characters at least on msg1.  Is this dangerous? Yes, because if the source message is not null terminated it will read until a null character ...

Casting an int to void pointer in c++

I was a bit confused with the following piece of code which just creates a number of threads and assigns them an id. #include "pthread.h" #include "stdio.h" #include "stdlib.h" #define N 5 void *worker_thread(void *arg) {     printf("This is worker_thread #%ld\n", (long) arg);     pthread_exit(NULL); } int main() {     pthread_t my_thread[N];     long id;     for (id = 1; id <= N; id++) {         int ret = pthread_create(&my_thread[id], NULL, &worker_thread, (void*) id);         if (ret != 0) {             printf("Error: pthread_create() failed\n");             exit(EXIT_FAILURE);         }     }     pthread_exit(NULL); } The function...

Disk Usage for Directories in Linux

The Linux command du -h --block-size=MiB --max-depth=3 | sort -rn shows the disk usage for each directory upto a depth of 3. MiB is required to avoid showing all those long bytes numbers. -h is for human readable format. sort -rn sorts on the reverse numeric order, i.e. the most disk usage first

Signal Handling - Linux

A program running always face unexpected situations, running out of memory, lost network connectivity, losing permissions, read write errors etc. Therefore a robust program should be resilient to these conditions and should gracefully exit if the program cannot continue. Signals should be handled.... I found this to be extremely resourceful and I'll just give a summary of what the author Alex says..... Signal is a notification, a message sent by either operating system or some application to your program (or one of its threads). In addition to informative nature of signals, they also interrupt your program. I.e to handle a signal, one of the threads in your program, stops its execution and temporarily switches to signal handler. Signals, as their name implies, used to signal something. There are several types of signals, each indicating something of its own. For instance SIGINT that I already mentioned, tells your program that someone tries to interrupt it with CTRL-C....

Vaadin vs GWT

From Chapter 1 of book of Vaadin I quote the following. Vaadin Framework is a Java web application development framework that is designed to make creation and maintenance of high quality web-based user interfaces easy. Vaadin supports two different programming models: server-side and client-side . The server-driven programming model is the more powerful one . It lets you forget the web and program user interfaces much like you would program a desktop application with conventional Java toolkits such as AWT, Swing, or SWT. But easier. While traditional web programming is a fun way to spend your time learning new web technologies, you probably want to be productive and concentrate on the application logic. The server-side Vaadin framework takes care of managing the user interface in the browser and the AJAX communications between the browser and the server . With the Vaadin approach, you do not need to learn and deal directly with browser technologies, such as HTML or JavaScript. ...

What is GWT ? Google Web Toolkit

The GWT project states that, GWT is a development toolkit for building and optimizing complex browser-based applications. Its goal is to enable productive development of high-performance web applications without the developer having to be an expert in browser quirks, XMLHttpRequest, and JavaScript. GWT is used by many products at Google, including AdWords, AdSense, Flights, Hotel Finder, Offers, Wallet, Blogger. It’s open source, completely free, and used by thousands of developers around the world. I quote the development lifecycle of GWT App from the project site as well. Write : The GWT SDK provides a set of core Java APIs and Widgets. These allow you to write AJAX applications in Java and then compile the source to highly optimized JavaScript that runs across all browsers, including mobile browsers for Android and the iPhone. Constructing AJAX applications in this manner is more productive thanks to a higher level of abstraction on top of common concepts like DOM...