mallocs of memory in C

How would you write a loop that repeatedly mallocs a memory block and prints the amount of memory used until malloc returns zero or stops.

Comments

  • : How would you write a loop that repeatedly mallocs a memory block and prints the amount of memory used until malloc returns zero or stops.
    :

    Try this:

    [code]
    int allocated;
    while(malloc(1024))
    {
    allocated += 1;
    cout << allocated << " kilobytes allocated." << endl;
    }
    [/code]

    I don't know the semantics of printf, but I think this should work if you can compile it.

    -Phyxion
  • : : How would you write a loop that repeatedly mallocs a memory block and prints the amount of memory used until malloc returns zero or stops.
    : :
    :
    : Try this:
    :
    : [code]
    : int allocated[b][red]=0[/red][/b];
    : while(malloc(1024))
    : {
    : allocated += 1;
    : cout << allocated << " kilobytes allocated." << endl;
    : }
    : [/code]
    :
    : I don't know the semantics of printf, but I think this should work if you can compile it.
    :
    : -Phyxion
    :


  • : : [code]
    : : int allocated[b][red]=0[/red][/b];
    : : [/code]

    Doh, thanks... That's what I get for not compiling it to test.

    -Phyxion
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories