Memory leak

Discussion in 'C++' started by shilpi.harpavat@gmail.com, Aug 10, 2005.

  1. Guest

    Hi,
    I am trying to allocate more memory if the following condition is true.
    IS it free of memory leaks??


    if(numLevels >= numAlloc) {
    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);
    }
    Thanks in advance
    Shilpi
    , Aug 10, 2005
    #1
    1. Advertising

  2. wrote:
    > I am trying to allocate more memory if the following condition is true.
    > IS it free of memory leaks??
    >
    >
    > if(numLevels >= numAlloc) {
    > char **oldLevels = levels;
    > long *oldOrder = order;
    > numAlloc *= 10;


    Are you sure that 'numLevels' is not 10 times 'numAlloc'? If it isn't,
    then you're not allocating enough, probably.

    > levels = (char **) calloc(numAlloc, sizeof(char *));
    > order = (long *) calloc(numAlloc, sizeof(long));
    > for(i = 0; i < numLevels; i++) {
    > levels = oldLevels;
    > order = oldOrder;


    If (numLevels > numAlloc) here yields 'true', you're overrunning the
    buffers you just allocated. You never check that, AFAICS.

    > }
    > free(oldLevels);
    > free(oldOrder);
    > }


    I don't know about memory leaks. When do you free those 'levels' and
    'order'?

    V
    Victor Bazarov, Aug 10, 2005
    #2
    1. Advertising

  3. wrote:
    > Hi,
    > I am trying to allocate more memory if the following condition is true.
    > IS it free of memory leaks??
    >
    >
    > if(numLevels >= numAlloc) {
    > 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);
    > }
    > Thanks in advance
    > Shilpi


    If numLevels >= (numAlloc*10), this will fail as you will exceed levels
    and order.
    Josh Mcfarlane, Aug 10, 2005
    #3
  4. E. Mark Ping Guest

    In article <>,
    <> wrote:
    >Hi,
    >I am trying to allocate more memory if the following condition is true.
    >IS it free of memory leaks??


    Since this is in a C++ newsgroup I'll ask: why are you manually
    allocating like this? If you must have an array of pointers, why not
    use std::vector?
    --
    Mark Ping
    E. Mark Ping, Aug 10, 2005
    #4
  5. Old Wolf Guest

    wrote:

    > I am trying to allocate more memory if the following condition is true.
    > IS it free of memory leaks??
    >
    > if(numLevels >= numAlloc) {
    > 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);
    > }


    The logic is OK (assuming numLevels <= numAlloc).
    But you do not check for failure of calloc.
    Note that calloc with (char *) does not necessarily
    make the pointers null.

    Also, you should use realloc(): it will automatically copy the
    old items, and may be more efficient in that it only has to
    extend a memory block, rather than selecting a new one:

    levels = (char **)realloc(levels, numAlloc * sizeof(char *));
    if (levels == NULL)
    {
    levels = oldLevels;
    // process error and return
    }

    etc.

    Finally, assuming you posted to a C++ group instead of a
    C group on purpose, you should do away with all of these functions
    and use a standard container, which automatically grows its memory.
    Old Wolf, Aug 11, 2005
    #5
  6. Le mercredi 10 août 2005 à 20:01, a écrit dans
    comp.lang.c++ :

    > Hi,
    > I am trying to allocate more memory if the following condition is true.
    > IS it free of memory leaks??
    >
    > if(numLevels >= numAlloc) {
    > 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);
    > }
    > Thanks in advance
    > Shilpi


    std::vector<char *> levels(numAlloc);
    std::vector<long> order(numAlloc);

    ....

    if (numLevels >= numAlloc)
    {
    numAlloc *= 10;
    levels.resize(numAlloc);
    order.resize(numAlloc);
    }

    Isn't C++ beautiful?

    --
    ___________ 11/08/2005 09:22:26
    _/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
    \ \_L_) Il faut donc que les hommes commencent
    -'(__) par n'être pas fanatiques pour mériter
    _/___(_) la tolérance. -- Voltaire, 1763
    Serge Paccalin, Aug 11, 2005
    #6
  7. Ram Guest

    > I am trying to allocate more memory if the following condition is true.
    > IS it free of memory leaks??
    >
    >
    > if(numLevels >= numAlloc) {
    > char **oldLevels = levels;
    > long *oldOrder = order;
    > numAlloc *= 10;

    Seems like you are allocating more buffer as needed. But if your new
    requirement is greater than 10 times of old, it will not work as you
    expect.

    > levels = (char **) calloc(numAlloc, sizeof(char *));
    > order = (long *) calloc(numAlloc, sizeof(long));
    > for(i = 0; i < numLevels; i++) {
    > levels = oldLevels;
    > order = oldOrder;
    > }

    You are copying the old data to new buffer, but the number of items in
    your old data is not numLevels, you may cause buffer over-run. Since
    you already increased numAlloc ten times, it should be-

    for(i = 0; i < numAlloc/10; i++) {
    levels = oldLevels;
    order = oldOrder;
    }
    > free(oldLevels);
    > free(oldOrder);
    > }

    There is no memory leak if you take care to free levels and order.
    However as already posted, if you are using C++ best way would be to
    use a std::vector<char*> & std::vector<long> and use their resize()
    functions.

    -Ramashish
    Ram, Aug 11, 2005
    #7
  8. Guest

    levels = (char **) calloc(numAlloc, sizeof(char *));
    order = (long *) calloc(numAlloc, sizeof(long));

    you did not write anything about "levels" and "order". If they have
    been assigned any memory before

    "if(numLevels >= numAlloc) { "

    and you are re-assigning them in

    ;
    levels = (char **) calloc(numAlloc, sizeof(char *));
    order = (long *) calloc(numAlloc, sizeof(long));

    So make sure that they are not assigned any memory.

    Otherwise , they would cause memory leak.
    , Aug 11, 2005
    #8
    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. =?Utf-8?B?Y3liZXJzdHJpa2U=?=

    datagrid memory leak?

    =?Utf-8?B?Y3liZXJzdHJpa2U=?=, Jan 3, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    465
    =?Utf-8?B?Y3liZXJzdHJpa2U=?=
    Jan 3, 2005
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    698
    Eric Sosman
    Mar 22, 2005
  3. Richard Heathfield

    Leak or no leak ??

    Richard Heathfield, Jul 10, 2006, in forum: C Programming
    Replies:
    4
    Views:
    350
    Richard Heathfield
    Jul 10, 2006
  4. cham
    Replies:
    5
    Views:
    768
  5. Mark Probert
    Replies:
    4
    Views:
    325
    Mark Probert
    Feb 9, 2005
Loading...

Share This Page