Thread: Why doesnt this malloc work please?

  1. #16
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    OK, so I deleted that line. But I'm pretty sure that's nothing to do with the initial problem. I tried this as a solution to that problem
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Here's the complete code of the function to make clear what I want it to do:
    Unfortunately, we really need the complete code to find where you're trashing memory. An isolated function which seems correct says nothing about anything else you're doing.

    When you're dealing with memory corruption, "cause" and "effect" are very different things. Where the problem shows up is often nothing to do with what actually caused it.

    What on earth are you trying to achieve with this?
    Code:
    if(!(formula = malloc(21)))
    {
         free(formula);//free up resources 
         if(!(formula = malloc(21)))
              exit(-1);
    }
    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.

  3. #18
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Thanks for all your suggestions/answers.

    Quote Originally Posted by Salem
    Unfortunately, we really need the complete code to find where you're trashing memory. An isolated function which seems correct says nothing about anything else you're doing.

    When you're dealing with memory corruption, "cause" and "effect" are very different things. Where the problem shows up is often nothing to do with what actually caused it.
    Unfortunately I can't post the complete code, for two reasons. 1) It's ~6500 lines 2) It's a pretty unique program that gives my company a big advantage over other companies (if it works, that is >.< )

    I have no idea what to do now...


    Quote Originally Posted by Salem
    What on earth are you trying to achieve with this?
    Code:
    if(!(formula = malloc(21)))
    {
         free(formula);//free up resources 
         if(!(formula = malloc(21)))
              exit(-1);
    }
    I tried this as it was a suggestion by King Mir here: http://cboard.cprogramming.com/showp...93&postcount=4
    I took it as a second try for memory allocation??
    Last edited by rkooij; 10-09-2006 at 07:40 AM.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  4. #19
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by rkooij
    Hmm don't know What else is meant with "free up resources" posted by King Mir somewhere above?
    Free up other resources. If malloc fails, that means you have allocated too much memmory. If you free some of that memmory, you can try malloc again. If it still does not work, your only option is to exit the program. If you have no other resources to free, then you can just quit.

    But really for small programs, malloc will not fail unless A) You're running a lot of memmory intensive programs at the same time(unlikely) B) You allocate an unreasonably huge continuous array. C) you have a memmory leak. A memmory leak is when you keep allocating a block of memmory multiple times without freeing it.

    I gave that suggestion because I could see nothing else wrong with your code. You can print a character or something to test if malloc is really failing or if the problem is elsewhere in your code. If it is failing, then you probably have a memmory leak somewhere, so make sure you free or realloc all memmory you allocate with malloc or calloc.
    Last edited by King Mir; 10-09-2006 at 08:22 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Unfortunately I can't post the complete code, for two reasons. 1) It's ~6500 lines
    We wouldn't want to wade through that much code either.

    > I have no idea what to do now...
    Well seeing as you're working for a company, get them to spend a few $$$ on something like Purify or boundschecker. These can take you direct to the root cause of the memory problem rather than you staring at the effect.
    Put it this way, how many days have you spent on this issue already (@say $50 per hour to the company), and how much longer are you likely to take on this issue alone.

    For linux users, try Electric Fence or Valgrind.
    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.

  6. #21
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by King Mir
    I don't see anything wrong there. The only thing that could be done is

    Code:
    if(!(formula = malloc(21)))
    {
         ;//free up resources 
         if(!(formula = malloc(21)))
              exit(-1);
    }
    King Mir, I think you've misunderstood something. It's not exit() that frees memory. It's free().
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #22
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    And I think you misunterstood what King Mir wanted to reach with this bit of code

    Free up other resources. If malloc fails, that means you have allocated too much memmory. If you free some of that memmory, you can try malloc again. If it still does not work, your only option is to exit the program. If you have no other resources to free, then you can just quit.

  8. #23
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Well, it would've been much easier to understand when he would've said that in the first place.
    Anyway, I'm sorry for not reading his next replies.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #24
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Sorry for being unclear.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #25
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    OK thanks guys. I've downloaded a trial of IBM PurifyPlus now and will see what it can do for me
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  2. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  3. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM
  4. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM