C/C++
Constructor and Destructor for C Program
Oct 8th
constructor and destructor attributes can be used to define a constructor function and a destructor function for a C program. In the following example pg_init function is defined as constructor and this function gets invoked before main() function. The pg_deinit function is defined as destructor and it gets invoked before process exits. Download: condes.c
#include <stdio.h>
static void pg_init () __attribute__ ((constructor));
static void pg_deinit () __attribute__ ((destructor));
static void
pg_init()
{
printf("program init...........\n");
}
static void
pg_deinit()
{
printf("program deinit...........\n");
}
int main()
{
printf("This is main\n");
}
The following is the output.
[neo@techpulp ~]# gcc condes.c -o condes [neo@techpulp ~]# [neo@techpulp ~]# ./condes program init........... This is More >
Get list of interfaces using SIOCGIFCONF ioctl
Oct 8th
The following example shows how the names of all network interfaces can be retrieved using SIOCGIFCONF ioctl. Other ioctls can be used subsequently to retrieve more information like IP address, netmask etc for each interface.
/* ifacelist.c */
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
int get_iface_list(struct ifconf *ifconf)
{
int sock, rval;
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{
perror("socket");
return (-1);
}
if((rval = ioctl(sock, SIOCGIFCONF , (char*) ifconf )) < 0 )
perror("ioctl(SIOGIFCONF)");
close(sock);
return rval;
}
int main()
{
static struct ifreq ifreqs[20];
struct ifconf ifconf; More > How to use assert function in C
Oct 8th
Typically the assert function is used to validate input arguments passed to a function before using them. In general, input argument checking is enabled in debug builds and disabled in production builds. The assert function validates the expression if it is true. If the expression is false, it invokes abort function to terminate the process.
void assert(scalar expression);
The following example shows how typically assert is used.
void which_fruit(char *name)
{
assert(name);
strcpy(name,"apple");
}
In the above example, if a NULL pointer is passed to which_fruit() function, assert evaluates the expression to be false and process gets terminated.
In case of production builds where assert needs to be disabled More >
Quick guide to profile C/C++ source code with gprof
Sep 22nd
This article is just a quick reference for profiing C/C++ source code using gprof. C/C++ source code can be profiled using gprof in three steps.
- Compile the sources and link the program with profiling options “-pg”
bash# gcc -pg -g -c source1.c source2.c bash# gcc -pg -o myapp source1.o source2.o
- Execute the program to generate profiling output in gmon.out
bash# ./myapp
- Analyze the profiling output with gprof. It prints the list of functions which have taken more time during the execution of process. This list is in descending order so probably you would want to start optimizing the code in the same order.The output also More >
How to resolve “Address already in use” error with bind system call
Jul 26th
To get rid of “Address already in use” error with bind system call, SO_REUSEADDR socket option should be set before invoking bind. This makes operating system to allow the socket to bind if there is no active socket bound to the requested address and port. Add the following code to the code snippet provided in “Find peer IP address and port number using getpeername” article.
/* create a socket */
sd = socket(AF_INET,SOCK_STREAM,0);
opt = 1;
if(setsockopt (sd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt)) < 0) {
perror("setsockopt");
exit(1);
}
...
Find local IP address and local port number using getsockname
Jul 26th
The getsockname function can be used to find the local port used by a given socket. It is valid to use this function only if the socket is already using a local port number. i.e A server socket which invoked bind system call with zero as port number. A client socket which has established connection to a server. In either case operating system selects an unused port number automatically. Similarly this function can also be used on UDP sockets.
The following code snippet explains how to use getsockname to retrieve the local port number assigned by operating system.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include More >
Find peer IP address and port number using getpeername
Jul 26th
The getpeername function can be used to retrieve the peer IP address and port number on a given socket. For this to work, the socket should have a valid TCP connection established. Typically this is used by server to find the IP address and port number of client.
In the following code snippet, a TCP server binds to port number 5555 and waits for clients. It retrieves client’s IP address and port number using getpeername if a client is connected.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/un.h>
int main()
{
struct sockaddr_in sin; More > Print first N characters of a string using printf
Jul 26th
#include <stdio.h>
int main(int argc, char *argv[])
{
char *p = "My Name is Neo";
printf("First 5 bytes of string is '%.5s'\n", p);
return 0;
}
Result
[neo@techpulp ex]# gcc example.c -o example [neo@techpulp ex]# ./example First 5 bytes of string is 'My Na' [neo@techpulp ex]#


Recent Comments