malloc, calloc question

This is a discussion on malloc, calloc question within the C Programming forums, part of the General Programming Boards category; Hi all: When I was using the malloc and calloc function in C, in some situation it gives me the ...

  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,163
    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,066
    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,163
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,163
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > (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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 01: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, 11:26 AM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM

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