fast I/O in C/C++

There many ways to do input/output in C/C++. Some are slow, some are fast and some can be very fast. Here I’ll be discussing some of these methods.

Many a times during competetive programming you come across the warning “Careful – large input/output“. Now what exactly does this means?
This basically means that you have to use an optimized code for your reading/writing to the standard input/output to stay inside the tighter time constraints of the problem. So let’s get going and explore the different methods to optimize your I/O in C/C++.

All the code can be found here: GitHub.

First one is the basic method that you guys must already be using. This is based on cin/cout methods of the iostream library. It is the most basic method and does not require you to specify the type of input you are expecting, but, is extremely slow.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {
	/* integer or any integer like */
	int integer;

	/* character string */
	char charstring[100];
		cin>>charstring;	// stops input after a "space"

		cin.getline(charstring, 100);	// stops after 100 character or eof whichever comes first

	string strstring;
		cin>>strstring;		// stops input after a "space"

		getline(cin, strstring);	// stops after eof or '\n', whichever comes first

	/* safest way to get an integer (but very slow) - taken from */
	int number = 0;
	string input;
	while(true) {
		getline(cin, input);

		stringstream myStream(input);

	/* Safest way to get a single character */
	char singlechar = {0};
	while(true) {
		getline(cin, input);

		if(input.length() == 1){
			singlechar = input[0];

The second method is based on the simple printf/scanf functions of stdio.h library for C. These are much faster than the above method and can be easily used in competetive programming for a decent time limit. These are multi-thread safe as they lock the file before writing.
Detailed description of the stdio.h library you can refer to here.

#include <cstdio>
#include <string>

int main() {
	/* integer I/O */
	int a;
	scanf("%d", &a);
	printf("%d\n", a);

        Other format specifiers.
        %d, %i = signed integer
        %u = unsigned integer
        %l = prefix for long
        %f = signed floating point
        %e = signed scientific
        %c = single character

	/* sting I/O */
	char charstring[100];
	scanf("%s", charstring);    // only till the first white space is stored
	printf("%s\n", charstring);

	scanf("%[^\n]s", charstring);   // sets th delimeter to be "new line"
	printf("%s\n", charstring); // thus whole line is read until a \n is observed
								// does not eliminate \n from the input stream

	printf("%s\n", charstring);

This method is very fast than the last method and used unlocked versions of the above functions used. I have written the code to scan integer and a string. For others you can easily write your function referring the below code. These are NOT multi-thread safe. Should be used in caution. But, in competetive programming plateforms, one program is already separated from others, so can easilt be used for better I/O in competetive programming which have even tighter time constraints.

#include <cstdio>

inline void fastRead_int(int &x) {
    register int c = getchar_unlocked();
    x = 0;
    int neg = 0;

    for(; ((c<48 || c>57) && c != '-'); c = getchar_unlocked());

    if(c=='-') {
    	neg = 1;
    	c = getchar_unlocked();

    for(; c>47 && c<58 ; c = getchar_unlocked()) {
    	x = (x<<1) + (x<<3) + c - 48;

    	x = -x;

inline void fastRead_string(char *str)
    register char c = 0;
    register int i = 0;

    while (c < 33)
        c = getchar_unlocked();

    while (c != '\n') {
        str[i] = c;
        c = getchar_unlocked();
        i = i + 1;

    str[i] = '\0';

int main()

  int n;
  char s[100];

  	printf("%d\n", n);

  	printf("%s\n", s);
  return 0;

There is another method I came across a solution in one of the problems on codechef. I haven’t tested it’s performance, but, his solution had the lowest timing with the same logic for the actual problem. So, I am assuming this gives a better performance for I/O. You can download the code here.
The solution referrenced is this.

I’ll come up with more updates on the same topic.

C++: incomplete type and cannot be defined

A very confusing error just now:
error: aggregate ‘std::ofstream out’ has incomplete type and cannot be defined

from simple code:
std::ofstream out;

And the similar one:
error: variable ‘std::ofstream out’ has initialiser but incomplete type

from this code:
std::ofstream out(fname.c_str(),std::ios_base::app);

Using this didn’t help:
#include <iostream>

Using this also didn’t help:
#include <ofstream>

That was confusing me most, but now I see I’ve been getting “error: ofstream: No such file or directory” and I was missing it in the noise of other warnings and errors.

The solution was simple:
#include <fstream>
Yes, a very simple solution, but google wasn’t helping. If it had been saying “ofstream is not a member of std” I’d have known I was missing a header file; a strange error message has you looking in other places. (I guess another std header file is doing a forward declaration for ofstream, which is why we get “incomplete type”.)

Memory profiling in C++

Code profiling as said in the earlier post is the dynamic analysis of resources used by a program or a small section of it.

Here we will discuss about monitoring the memory during a run of a C++ program. Monitoring the memory greatly helps in optimizing your code. Memory leaks (when memory is not released back to the operating system and the operating system stops the program in the middle because of the over-usage of memory), swapping of data (swapping of data between main memory and disk greatly reduces the performance as disk IO is slow compared to main memory IO), free memory available at any point of time, when and where memory is allocated and freed, inaccessible areas of stack data, usage of cache (proper usage of cache memory can increase your performance), heap memory usages.

NOTE: using memory management tools reduces the performance of the program (it could get 100 times slower :O ). So it should only be done for testing, and development and not during production phase.

first let us see the various tools freely available to work for us:

  • debugger – just use your C++ debugger to keep track of memory leaks, memory allocations step-by-step. But, this process is very slow. Instead of compiling using g++, compile the program using “gdb”.
        gdb file.cpp

  • sysstat – just install this package using the command (sudo apt-get install sysstat).
    Using the command “free” will tell you memory statistics at that point of time. (you can use the -m option to show the memory in megabytes).
  • valgrind – This is the best tool available freely. (to install type: sudo apt-get install valgrind). It has the various subtools:
    • Memcheck – When a program is run under memcheck’s supervision, all reads and writes of memory are checked, and calls to malloc/new/free/delete are intercepted. Memcheck reports errors as soon as they occur, giving the source line number at which it occurred, and also a stack trace of the functions called to reach that line.
    •  Cachegrind -its a cache profiler. Tells you which part code has lead to a cache miss. The number of cache misses, number of instructions executed on each line of code.
    • Massif – performs detailed heap profiling. It produces a graph showing heap usage over time, including information about which parts of the program are responsible for the most memory allocations.

    Valgrind can be easily used from the terminal. When calling your program executable just write “valgrind” before the call using the appropriate options, eg:

        valgrind --leak-check=yes <myprog> arg1 arg2

    To know the options and other methods you can refer to its documentation from here.

  • leakfinder – This is a simple GUI application for windows to find leaks in your program with an basic inbuilt code editor.
  • gperftools – this is a tool developed by google for use by developers so that they can create more robust applications. Especially of use to those developing multi-threaded applications in C++ with templates. Includes TCMalloc, heap-checker, heap-profiler and cpu-profiler.
  • dmalloc – This is another tool available on the web.

The concept used in dmalloc is quite a simple one and we can ourselves make a simple library that can keep track of memory allocations and memory release. This concept is based on function and operator overloading. Here we will overload the new/delete operator. Thus, whenever a memory is allocated or freed, we can print the appropriate information. So let’s start,

#include <execinfo.h>   // this is a header file contains the backtrace function

void *caller()
      const int target = 3;     // trace three functions back
      void* returnaddresses[target];
      if (backtrace(returnaddresses, target) < target) {
               return NULL;
      return returnaddresses[target-1];

void* operator new(size_t size) throw(std::bad_alloc) {
       void* ret = malloc(size);
       if (!ret) throw std::bad_alloc();
       cerr<<"allocate: "<<ret<<" "<<size<<" bytes from "<<caller()<<"\n";

void* operator new[] (size_t size) throw(std::bad_alloc) {
       void* ret = malloc(size);
       if (!ret) throw std::bad_alloc();
       cerr<<"allocate: "<<ret<<" "<<size<<" bytes from "<<caller()<<"\n";

void operator delete(void* data) {
       cerr<<"free: "<<data<<"\n";

void operator delete[] (void* data) {
       cerr<<"free: "<<data<<"\n"; 

Just write this code in a file and then you can include this as a header file in your program whenever you want to monitor your memory allocations and who allocates it. There is no need to write any extra code in your program. Similarly, you can overload the malloc/free functions if new/delete does not work for you.

good luck.

    Code Profiling for time in C++

    Code profiling is a very important aspect of programming. First you must be wondering what is code profiling. You can always google it but in simple words “code profiling” is just measuring of the resources used by your program or small sections of your program.

    Here I will be talking about code profiling for “time”. Dynamically measuring your code for how much time it takes for different input sets is of keen importance when you have to optimise your code. There are many ways you can achieve this,

    • there are many unix tools available to do the job for you.
      • time – just type time while calling your executable file.
        eg: time ./a.out
      • sysstat (to install just type “sudo apt-get install sysstat”) – It has many tools available to check the resources used up in the running processes.
        While your program is running, you can check up the resources used up your program by the commands “iostat -i”, “iostat -c”, “iostat -dx”. if networking is involved you can use “netstat -i”, “netstat -s”. To check the memory usage, i.e., free memory and used memory and memory swapped etc., you can type “free -m”.
      • callgrind – you can download this tool to profile your code.
    • The simple way is to put a small code inside your code to measure the time. Here I’ll tell you how to do that using a small library I have written.

     Its use as simple as writing:

    int func() {
        timeit s(“func()”);
        // your code here.

    It is based on a simple concept that when a object goes out of its scope, its destructor is called. So to time a code snippet, you just have to make an object of the type “timeit” having the same scope as the code snippet. In the above example it will print to the standard error:
               func()       3.21554ms

     The object simply times the call between its construction and destruction. If its difficult to maintain the scope of the object by using curly brackets, then you can also use the “new” and “delete” to manually set the scope. The code is rather simple and here it goes:

    #include <iostream>
    #include <ctime>
    class timeit {
        char const *name;
        timespec t_start;
            timeit(char const *temp): name(temp) {
                clock_gettime(CLOCK_REALTIME, &t_start);
            ~timeit() {
                timespec t_end;
                clock_gettime(CLOCK_REALTIME, &t_end);
                double dur = 1e3 * (t_end.tv_sec - t_start.tv_sec) + 1e-6 * (t_end.tv_nsec - t_start.tv_nsec);
                std::cerr << name << '\t' << std::fixed << dur << " ms\n";

    Just include this this code in your program and timeit 😉