Thread: malloc + segmentation fault

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154

    malloc + segmentation fault

    Could you help me ?

    Code:
          //Test part 1
          testInt = (strlen(OpenFiles[fileDesc].filename)+1)*sizeof(char);
          puts(BBuffer[newBufPos].filename);
          //Until here everything works fine
    
    
          BBuffer[newBufPos].filename = malloc((strlen(OpenFiles[fileDesc].filename)+1)*sizeof(char));
          //After malloc i get seg fault but arguments have already check by part 1
          puts("********");
    
          if(BBuffer[newBufPos].filename == NULL)
          {
            BF_errno = BFE_NOMEM;
    
            return BF_errno;
          }
    The segFault appears at malloc.
    At first i thought that might be an accessibility problem (violation access) with (strlen(OpenFiles[fileDesc].filename)+1)*sizeof(char) or BBuffer[newBufPos].filename thats why malloc creates segfault, so i made Test part1 .

    Test Part1 works fine !!!!!

    I print stars after malloc and they don't appear, this lead me to the conclusion that malloc is the suspect.


    Can malloc produce a segmentation fault regardless of my given arguments ?
    Should i free filename first ?

    Info : Windows XP \ DevC++
    Last edited by ch4; 04-07-2009 at 02:47 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    puts(BBuffer[newBufPos].filename);
          //Until here everything works fine
    
    
          BBuffer[newBufPos].filename
    Is your puts line being displayed? If it is, why are you trying to overwrite what's there with newly allocated memory? If it isn't, then stop trying to display a segment of memory with nothing allocated for it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Yes it's displayed.
    malloc returns new allocated memory regardless if ex-memory freed or not, right ?
    So why this seg ?

  4. #4
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    I use free before malloc and problem solved.

    But wasn't it strange ?

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    You have a lot of memory accesses going on in that single line -- are you sure it's the malloc call that's breaking? Try putting some of those array accesses into temporary variables before the malloc.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ch4 View Post
    Yes it's displayed.
    malloc returns new allocated memory regardless if ex-memory freed or not, right ?
    So why this seg ?
    Quote Originally Posted by ch4 View Post
    I use free before malloc and problem solved.

    But wasn't it strange ?
    Well considering you didn't show us what your variable or structure actually was, and considering you were just randomly allocating memory over top of what appears to be stuff that's already there ... no, it's not really surprising. You don't seem to have the best of programming habits, from what we're given to draw our conclusions.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault - aaaaaaaah!
    By yogibear in forum C Programming
    Replies: 6
    Last Post: 10-01-2007, 03:21 AM
  2. malloc segmentation fault
    By BharathKumar in forum C Programming
    Replies: 5
    Last Post: 06-27-2007, 02:53 AM
  3. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM