Thread: malloc, calloc question

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    12

    Smile malloc, calloc question

    Hi all:

    When I was using the malloc and calloc function in C, in some situation it gives me the error:
    Segment Fault (core dump), in some situation, it gives me the error: Bus error (core dumped), but in some case, it has no error at all. All these things occure for the same pieice of code. (exactly the same). What's wrong with this? Any advice will help! Thanks!

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    show some code.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Please show some code. It's accessing memory it can't sometimes which means something is undefined.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >What's wrong with this? Any advice will help! Thanks!
    I think our resident psychic is off today.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by swoopy
    >What's wrong with this? Any advice will help! Thanks!
    I think our resident psychic is off today.
    No, I'm here. *holds forehead in a concentration-intensive way*

    It's right there. Line 47. You need to change a = crash(); to a = dontcrash();.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by itsme86
    It's right there. Line 47. You need to change a = crash(); to a = dontcrash();.
    OMG I did wut u sed, but the compiler says it dont no wut dontcrash is. how shud i fix this?
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    12

    Smile

    The code is here:
    Code:
    	int rightSetSize = numberOfDistances - medianDisIndex;
    	rightSet->leaf = (Leaf*) malloc(rightSetSize*sizeof(Leaf));
    	if (rightSet->leaf == NULL)
    	FatalError("out of memory - vpTree.c - findRadius - 3");
    below is some type and variable declaration for the code
    Code:
    typedef struct
    {
    	int speciesID;
    	int leafID;
    } Leaf;
    
    typedef struct {
    	Leaf* leaf;
    	int numOfSpecies;
    } LeafSet;
    
    LeafSet* rightSet
    
    int numberOfDistances = leafSet->numOfSpecies*1 - 1;
    
    int medianDisIndex = numberOfDistances/2;
    Thanks!
    Last edited by Salem; 09-05-2006 at 03:41 PM. Reason: Added code tags - learn to use them yourself.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    LeafSet* rightSet
    Do you allocate memory for rightSet to point to somewhere?
    If you understand what you're doing, you're not learning anything.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    More random snippets of stuff which won't even compile, let alone run.

    We can't tell you where the problem is unless you post something which either compiles and then crashes, or at least something with a lot more context than you're showing at the moment.

    > int rightSetSize = numberOfDistances - medianDisIndex;
    Is this enough?
    My guess is when you come to do some loop on rightSet->leaf[ i ] is that your count is off by one and you trash the end of the allocated memory in some way.
    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.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    >Do you allocate memory for rightSet to point to somewhere?

    I have checked, rightSet have memory allocated for them. And exactly it is this part

    (Leaf*) malloc(rightSetSize*sizeof(Leaf))

    crashes.

    >My guess is when you come to do some loop on rightSet->leaf[ i ] is that your count is off by one and >you trash the end of the allocated memory in some way.
    I do not understand this.

    Thanks!

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Are you sure it's not the first part of that expression (rightSet->leaf) that's crashing? let's see the code where memory is allocated for rightSet, since if that's incorrect, dereferencing it to get to the leaf member is likely your problem.
    If you understand what you're doing, you're not learning anything.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > (Leaf*) malloc(rightSetSize*sizeof(Leaf))
    > crashes.
    A partial list of things which can cause malloc to fail.

    In some earlier part of the code
    - You didn't allocate enough space and you've trashed the memory pool
    - You used a pointer before allocating memory
    - You used a pointer after freeing memory
    - You freed the same memory twice
    - You freed something which wasn't the result of a malloc call.

    Now examine all your code for places where you might be doing those kinds of things and fix them.
    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.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    After I run my code in my own computer (previously, I run it in my student account), all problem disappear. I looks to me that my student account's memory is too small to make the malloc function work well. Thank you all!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about malloc() and free()
    By MissEileen in forum C Programming
    Replies: 8
    Last Post: 01-24-2009, 02:46 PM
  2. Question about malloc()
    By cdalten in forum C Programming
    Replies: 6
    Last Post: 05-12-2006, 10:57 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM