Allocate more memory

Discussion in 'C++' started by shilpi, Aug 10, 2005.

  1. shilpi

    shilpi Guest

    Hi,
    I am trying to allocate mroe memory if the following condition is true.
    But I get Invalid pointer write memory error when running this code
    through purifier.It is not freeing up oldlevels
    Any help appreciated.

    if(numLevels >= numAlloc) {
    //printf("Allocate more memory\n");
    char **oldLevels = levels;
    long *oldOrder = order;
    numAlloc *= 10;
    levels = (char **) calloc(numAlloc, sizeof(char *));
    order = (long *) calloc(numAlloc, sizeof(long));
    for(i = 0; i < numLevels; i++) {
    levels = oldLevels;
    order = oldOrder;
    }
    free(oldLevels);
    free(oldOrder);
    }
    shilpi, Aug 10, 2005
    #1
    1. Advertising

  2. shilpi wrote:
    > Hi,
    > I am trying to allocate mroe memory if the following condition is true.
    > But I get Invalid pointer write memory error when running this code
    > through purifier.It is not freeing up oldlevels
    > Any help appreciated.
    >
    > if(numLevels >= numAlloc) {
    > //printf("Allocate more memory\n");
    > char **oldLevels = levels;
    > long *oldOrder = order;
    > numAlloc *= 10;
    > levels = (char **) calloc(numAlloc, sizeof(char *));
    > order = (long *) calloc(numAlloc, sizeof(long));
    > for(i = 0; i < numLevels; i++) {
    > levels = oldLevels;
    > order = oldOrder;
    > }
    > free(oldLevels);
    > free(oldOrder);
    > }

    Please don't double post on the list. But the error you are having
    might be due to the fact that if numLevels >= (numAlloc*10), you will
    still not have enough allocated memory.
    Josh Mcfarlane, Aug 10, 2005
    #2
    1. Advertising

  3. shilpi

    Guest

    hi shilpi,

    there is a function in the c library that does exactly the same thing
    viz realloc
    u can code in the following way...


    if(numLevels >= numAlloc) {
    //printf("Allocate more memory\n");
    numAlloc *= 10;
    levels = (char **)realloc(levels, numAlloc *
    sizeof(char *));
    order = (long *) realloc(order , numAlloc *
    sizeof(long));
    }

    in case of any clarification,plz see the documentation of realloc.

    thanks
    rt
    , Aug 11, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas G. Marshall

    Re: more than 10 seconds to allocate an array!

    Thomas G. Marshall, Aug 28, 2003, in forum: Java
    Replies:
    2
    Views:
    377
    Ahmed Moustafa
    Aug 28, 2003
  2. Andy Flowers
    Replies:
    34
    Views:
    970
    Thomas G. Marshall
    Sep 6, 2003
  3. Jon Skeet
    Replies:
    6
    Views:
    376
    Ahmed Moustafa
    Aug 30, 2003
  4. Mark Thornton
    Replies:
    1
    Views:
    341
    Ahmed Moustafa
    Sep 1, 2003
  5. Alan Gifford
    Replies:
    4
    Views:
    379
    tom_usenet
    Oct 27, 2003
Loading...

Share This Page