Saturday, March 12, 2011

[muwykzpg] Running out of memory in a hurry

Creating a useless linked list stack that only contains links, no data.

#include <iostream>
using std::cout;
using std::endl;
#include <new>

//use "ulimit -v" to limit the memory usage
int main(){
  long count=0;
  void** last=0;
  try{
    for(;;++count){
      //for(void**runner=last; runner; runner=static_cast<void**>(*runner)) cout << runner << " "; cout << endl;
      void**newlink=new (void*);
      *newlink=last; //anything may be implicitly cast to void*
      last=newlink;
    }
  } catch (std::bad_alloc&){}
  cout << count << endl;
}

This will probably freeze up your machine.

The one-liner version:
typedef void*v; int main() {v*a; for(;;) {v*b=new v; *b=a; a=b;}}

No comments :