Monday, October 31, 2005

Eating up all your memory

// Wasting my hate.
int main() {
  long double *x;
  while(1) 
    x=new long double;
}



Result:

Abort!
Exiting due to signal SIGABRT
Raised at eip=00005afe
eax=00092204 ebx=00000120 ecx=00000000 edx=00000000 esi=00000054 edi=000123a4
ebp=000922b0 esp=00092200 program=C:\DOCUME~1\ROXTAR~1.DEM\A.EXE
cs: sel=01a7  base=02980000  limit=2cf2ffff
ds: sel=01af  base=02980000  limit=2cf2ffff
es: sel=01af  base=02980000  limit=2cf2ffff
fs: sel=017f  base=0000e1b0  limit=0000ffff
gs: sel=01bf  base=00000000  limit=0010ffff
ss: sel=01af  base=02980000  limit=2cf2ffff
App stack: [000923a4..000123a4]  Exceptn stack: [00012304..000103c4]

Call frame traceback EIPs:
0x00005a24
0x00005afe
0x00004f2b
0x0000bae4
0x0000bb1e
0x0000b9cd
0x0000aeb3
0x000015fa
0x00004738


Windows ran out of virtual memory within 2 minutes of the run.

Anagram Checker

/* Program to check whether one string is a permutation of another.
   Google sample problem, Cochin October 2005. */
/* Author: Rohit Krishna Kumar */
/* Home page: http://www.geocities.com/rohitkkumar */

#include 
#include 
#define SIZE 10
int main() {
  char str1[SIZE],str2[SIZE];
  int len1,len2;
  int i,j,k;
  int flag=0;
  printf("Enter the first string: ");
  scanf("%s",str1);
  printf("Enter the second string: ");
  scanf("%s",str2);
  len1=strlen(str1);
  len2=strlen(str2);
  if(len1!=len2) {  /* Obviously */
    printf("Strings are not permutations of each other.\n");
    return 0;
  }
  /* This loop checks if each character in one string 
     is present in the other string. As soon as it finds 
     one character it deletes it from both the strings and
     continues. */
  for(i=0;i<len1;i++) {
    flag=0;
    for(j=0;j<len2;j++) {
      if(str1[i]==str2[j]) {
 for(k=i;k<len1;k++) /* Delete the character from str1 */
   str1[k]=str1[k+1];
 for(k=j;k<len2;k++) /* Delete the character from str2 */
   str2[k]=str2[k+1];
 len1--;   /* Decrease the length of both strings. */
 len2--;
 flag=1;
 i=0;
 j=0;
 break;
      }
    }
    if(!flag) {   /* One character in one string is not there
       in the other */
      printf("Strings are not permutations of each other.\n");
      return 1;
    }
  }
  printf("Strings are permutations of each other.\n");
}

Code to HTML

This python program replaces the special HTML characters (present in code) with their HTML codes. The listing below has been made from the program itself :)
print "Enter the file to read: "
name=raw_input()
in_file=file(name,"r")
buffer=in_file.read()
buffer=buffer.replace("&","&amp;");
buffer=buffer.replace("\"","&quot;");
buffer=buffer.replace("<","&lt;")
buffer=buffer.replace(">","&gt;")
out_file=file(name+".html","w+");
out_file.write(buffer);

My very own vector class

// A simple program to implement a *vector* like class which doubles its size
// whenever it is about to overflow.
// Inspired from Amortized time analysis. (although I still haven't understood
// what it is all about ;) )
// Author: Rohit Krishna Kumar
// Home page: http://www.geocities.com/rohitkkumar

#include 
using namespace std;
const int MAX=4;
template 
class Array {
  T *list;
  int max_size;
  int cur_size;
public:
  Array(void);
  void insert(T);
  T remove(void);
  void display(void);
  ~Array();
};

template 
Array::Array() {
  max_size=MAX;
  list=new T[max_size];
  cur_size=0;
}

// Inserts the element at the last position

template 
void Array :: insert(T val) {
  if(cur_size == max_size) {
    T *temp=new T[max_size*2]; // Allocate more memory
    max_size<<=1;  // Update the amount of memory allocated
    for(int i=0;i

T Array :: remove(void) { // Doesn't perform any deallocation
  if(cur_size!=0)
    return list[--cur_size];
  else 
    return -1;   // List empty
}

template  
void Array :: display(void) {
  for(int i=0;i<<<" ";
  cout<

Array :: ~Array() {
  delete[] list;
}

int main() {
  // Do a test run :)
  Array a;
  for(int i=0;i<1000000;i++)
    a.insert(i);
  for(int i=0;i<1000000;i++)
    cout<<<" ";
}