Release memory problem

This is a discussion on Release memory problem within the C++ Programming forums, part of the General Programming Boards category; Hi! I had problem in the next code. In it, I want to carry out a tokenizer for getting some ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    Release memory problem

    Hi! I had problem in the next code. In it, I want to carry out a tokenizer for getting some parameters values written in a text file.

    Code:
    /* Se lee el valor del parámetro Longitud del símbolo Apex */
    
           p_cadena=p_cadena+2;
           char *param_cpy=(char*)calloc(strlen(&(cadena[p_cadena])),sizeof(char));
           strcpy(param_cpy,&(cadena[p_cadena]));
           char *lg=strtok(param_cpy,",");
           p_cadena=p_cadena+strlen(lg);
           *result=sscanf(lg,"%f",l);       free(param_cpy);
    In the character chain 'param_cpy' I allocate memory in order to copy a fragment of the text chain being descomposed. After i get the token, a want to release this memory. Howewer, when i try doing it, the program crash in execution time. The instruction causing the problem is marked in red in the code above. I don´t understand the reason which this problem occurs for. Someone could help me?

    Thank you.

    Fernando Gutiérrez
    Last edited by fgutierrez; 10-15-2007 at 12:34 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As this is C++ I will not say "You shouldn't cast malloc/calloc"...

    Code:
    calloc(strlen(&(cadena[p_cadena])),sizeof(char));
    Leading question: How long is this section, and how long a string does it cope with?

    Is there something in a string, that makes it take up a tiny bit more space than you'd immediately think?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    Your calloc doesn't count the \0 at the end of the string, which will be copied (and which will trash memory when the strcpy happens).

    Say
    char *param_cpy=calloc(strlen(&(cadena[p_cadena]))+1,sizeof(char));

    Also, using calloc instead of malloc has no advantage in this instance, since you're going to overwrite all the data with the following strcpy().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Try allocating 1 or 2 extra bytes. Maybe strcpy() is filling param_cpy and not leaving a NULL at the end, or maybe there's no NULL at the end of cadena[p_cadena] and you're overflowing the buffer?
    Use strncpy() to prevent buffer overflow.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    > or maybe there's no NULL at the end of cadena[p_cadena] and you're overflowing the buffer?
    Then the strlen() itself would have failed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    2
    [QUOTE=Salem;678980]Your calloc doesn't count the \0 at the end of the string, which will be copied (and which will trash memory when the strcpy happens).

    Say
    char *param_cpy=calloc(strlen(&(cadena[p_cadena]))+1,sizeof(char));
    [QUOTE]

    I've to review my code, but I think that you are right and that is the reason for that my program crashes. Thank you

    Quote Originally Posted by Salem View Post
    Also, using calloc instead of malloc has no advantage in this instance, since you're going to overwrite all the data with the following strcpy().
    My question is if using malloc instead of malloc have some advantage.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    calloc does more work than malloc (it is after all just malloc + memset), so unless you need that feature, it isn't worth doing IMO.

    Also, calloc doesn't necessarily result in valid data, especially where floats and pointers are concerned.
    http://c-faq.com/malloc/calloc.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And once you use malloc instead of calloc, why not use new[] instead?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > calloc does more work than malloc (it is after all just malloc + memset), so unless you need that feature, it isn't
    > worth doing IMO.

    Not to mention that it confuses people reading your code trying to figure out where you use the initialization, only to finally realize you don't.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 03:15 PM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 01:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21