Malloc and calloc problem!!

This is a discussion on Malloc and calloc problem!! within the C Programming forums, part of the General Programming Boards category; Hi All, i am using a malloc to allocate some memory in my project and also defining the stack and ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    3

    Question Malloc and calloc problem!!

    Hi All,

    i am using a malloc to allocate some memory in my project and also defining the stack and heap size for the program to use while running.

    I am facing a weird problem which is like at certain point when i am trying to malloc and allocate some memory it fails and gives a segmentation violation error and terminates the program. I tried replacing the same with calloc and the program runs fine with no issues at all.

    as far as my knowledge base says its like the malloc and calloc both does the same thing of allocating the memory while calloc only initializes the memory to zero and aloocates the memory in blocks whereas malloc allocates in bytes.

    so i tried teh malloc and then memset the memory to initialize it to zero but that again crashes the program at malloc again. I cant understand the problem and how to rectify it.

    can anyone send some piece of information or some tutorial / article so that i can be able to undertand the logic and why it is happening??

    any help or pointers are highly appreciated.

    thanks a lot in advance.

    Best Regards,
    Himanshu Singh

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Try looking elsewhere in your code for the problem, rather than trying different allocators at this point, looking for one which "works".

    Somewhere else in your code, you've done this - a buffer overflow most likely
    char *p = malloc ( 10 );
    strcpy( p, "hello world" ); // this does NOT fit in the allocated space.

    In particular, watch out for traps like this
    int *p = malloc( 10 ); // this does NOT allocate space for 10 integers

    Exactly how/when/if this actually causes a segfault is entirely random.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    3
    Hi Salem,

    thanks for your prompt response this is the line where it gives the segmentaion violation

    q=(char *)malloc(strlen(theStr) + 1);

    where q is a pointer of char* type.

    when i try to debug it raises the exception at this line itself and when i calloc then it is running fine.

    Can you help??

    Regards,
    Himanshu

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    See, there is no problem with that line of code (except for the cast, see the FAQ).
    Now study all the other malloc calls in your code to make sure that you
    - allocate enough memory
    - don't run off the end of the memory you allocate.
    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.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    3
    Hi,

    This is the first call to malloc and we start off from here itself and I dont see a problem of memory if calloc can allocate the memory at same point then malloc should also do the same then why there is a problem only with malloc that is my question.

    Thanks a lot anyway.

    Regards,
    Himanshu

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    I suggest you post the whole code which shows the problem, and state your OS and compiler.
    If this really is your first malloc call, then I can't see any reason why one would work and the other wouldn't.
    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.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    are you sure theStr is null terminated?

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,337
    malloc() does not typically result in segmentation violations. It returns NULL if it fails.

    If a program crashes, the reason is either at the place where the crash occurs OR IN ANY CODE EXECUTED IT IS REACHED.

    If malloc() results in a crash, the error is rarely the malloc() call itself. It is usually some code executed BEFORE the malloc() call that has screwed something up (eg by falling off the end or an array, dereferencing a NULL pointer, etc). For example;
    Code:
    int main()
    {
     int x[2];
     int *y;
     
     x[5] = 42;    // offending line
    
     y = malloc(10);   //  the offending line might be above, but the crash can occur here
    }
    The crash might occur in the malloc() call in the above if the offending line manages to overwrite data used internally within the malloc() function.

    Changing the malloc() call to calloc() and having the crash go away unfortunately means nothing, although it has curiosity value. The only relationship between calloc() and malloc() is that the EFFECT of calloc() is like calling malloc() and then setting the memory returned to zero. There is no requirement that calloc() is implemented using malloc(), or vice versa.
    Last edited by grumpy; 08-09-2005 at 06:43 AM.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    q=(char *)malloc(strlen(theStr) + 1);
    Should not have a cast, as Salem said, and should be multiplied by sizeof(char) to be more portable:
    Code:
    #include <stdlib.h>
    
    q = malloc(sizeof(char)*(strlen(theStr)+1));
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by dwks
    and should be multiplied by sizeof(char) to be more portable
    sizeof(char) is one, so there is no point in multiplying by it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I know . . . but couldn't that change?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Not without leaving the language. And in that case all bets are off anyway.
    http://dev.unicals.com/papers/c89-draft.html#3.3.3.4
    Last edited by Dave_Sinkula; 08-09-2005 at 01:01 PM. Reason: Added link.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Guess not then. Okay, no sizeof(char).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Destoryer of Worlds
    Join Date
    Jul 2005
    Posts
    17
    why is recasting the pointer from malloc incorrect. malloc returns a void pointer if your puting into a char pointer recasting would be correct. But again this is a mute point as this has been discuessed many times on another forem where i raised the question. In reatlity it makes no diffrence

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    FAQ > Explanations of... > Casting malloc
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 02:29 AM
  2. Malloc & Calloc difference in terms of memory allocated
    By swapnaoe in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 12:57 AM
  3. malloc, calloc question
    By chen1279 in forum C Programming
    Replies: 12
    Last Post: 09-07-2006, 05:54 PM
  4. difference between calloc and malloc
    By saravanan_ts in forum C Programming
    Replies: 4
    Last Post: 07-28-2003, 06:13 AM

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