C/C++
How to switch between threads in GDB
Sep 16th
The GNU debugger (GDB) provides greater flexibility in debugging multi-threaded applications. Following are some of the facilities that can be used.
List information about all threads
(gdb) info threads
Set break point specific to a thread.
You can mention thread ID after the break point.
(gdb) break Myfunction thread 1
Switch GDB control to a given thread
Following command swicthes GDB control to thread ID 3. The thread ID is derived from the output of “info threads” command.
(gdb) thread 3
Listing Backtrace of all threads at once
(gdb) thread apply all bt
How to check if an IP address falls in a subnet based on subnet and subnet mask
Oct 29th
This can be achieved easily with binary AND (&) operation as shown the function below.
typedef unsigned long IPv4Addr;
/* Function returns 1 if the IP falls in the subnet. Otherwise returns 0. */
int IsIpInTheSubnet(IPv4Addr ip, IPv4Addr subnet, IPv4Addr mask)
{
if((ip & mask) == (subnet & mask)) return 1;
return 0;
}
Example:
IP Address Subnet Mask Return Value 192.168.1.10 192.168.1.0 255.255.255.0 1 192.168.2.10 192.168.1.0 255.255.255.0 0The above function can easily be converted in to a macro to make it simple and more efficient.
This is what preciselyTCP/IP networking stack does for finding a route in the system routing table before More >
How to make a BSD socket non-blocking – EWOULDBLOCK
Oct 5th
Sometimes it is needed to make a socket non-blocking so that I/O requests on the socket don’t block. For a non-blocking socket, when I/O requests like read() and write() return a negative value, the errno should be checked against -EWOULDBLOCK code to find if the I/O request would have blocked.
The following utility function sets non-blocking flag of a socket.
int socketSetNonBlocking(int sockfd)
{
unsigned int flags;
flags = fcntl(sockfd. F_GETFL, 0);
if(flags < 0) return -1;
if(fcntl(sockfd, F_SETFL, flags|O_NONBLOCK) < 0) return -1;
return 0;
}
Similarly the following function makes a non-blocking socket a blocking socket.
int socketSetBlocking(int sockfd)
{
unsigned int More > Why syntax highlighting is off for files opened using cscope
Sep 9th
Those who love syntax highlighting in Linux may be a bit annoyed when they see no syntax highlighting when a file is opened in cscope. Well, this ain’t cscope’s problem. It’s just that cscope doesn’t know (or at least it doesn’t attempt to learn) that you have vim command in the system. So cscope uses standard vi command instead of vim command while opening a file. This creates trouble for user as the general features of vim listed below are not seen when the file is opened with in cscope environment.
- Code syntax highlighting
- Remembering last visited line of a file
- Remembering history More >
What is the difference between a macro and an inline function in C
May 8th
The fundamental difference between a macro and an inline function is that they are handled in different phases of binary creation. A macro is expanded during pre-processing stage by the “cpp” command. The “cpp” command is the C preprocessor. On the other hand, the inline functions are handled by the actual compiler “gcc“.
As the preprocessor is less intelligent, it does blind code replacements wherever it finds usage of a macro. A function described in a macro form doesn’t look nice in terms of code readability. Also arguments taken by a macro do not carry their type and often cause compilation More >
Why my application can’t open more than 1024 or 4096 sockets in Linux
Dec 5th
Typically socket “open” call doesn’t fail unless you have missed out closing the previous connections using “close” system call. Typically Linux/Unix sets a maximum limit for the number of open FDs. That means you can’t keep FDs in open state more than certain number. These settings can’t be changed as a normal user of the system.
These limits can be seen using “ulimit -a” command.
[neo@techpulp ~]# ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 31729 max locked memory (kbytes, -l) 32 max memory More >
How to redirect standard output and standard error to same file
Nov 16th
Sometimes while compiling C source code, the screen scrolls down if lot of warnings and errors are generated. This is uncomfortable as you can’t see all errors and warnings and you can’t attend to all warnings and warning at once and keep on recompiling. Instead you can use redirection facility supported by Bash shell to capture both messages printed on standard output and error to a same file. This method is more useful when you have huge build process to compile sources of your project.
Basically redirection of standard output is done as usual but the standard error is appended to More >
Why macro can’t be used instead of typedef in C
Nov 14th
The C preprocessor blindly expands the macro with whatever they are defined with. Let us examine the following example.
#define CHARPTR char* CHARPTR p1,p2;
The C preprocessor expands the above code to the following.
char *p1,p2;
But we would like p2 to be a character pointer but not a character variable.
So macro are not generally used for variable type definitions as they are not suitable for all sorts of usages. Instead typedef is used as shown below.
typedef char* CHARPTR; CHARPTR p1,p2;
With the above code, the variables p1 and p2 will effectively become character pointers.


Recent Comments