malloc + segmentation fault

This is a discussion on malloc + segmentation fault within the C Programming forums, part of the General Programming Boards category; 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 = ...

  1. #1
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142

    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 03: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
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    Yes it's displayed.
    malloc returns new allocated memory regardless if ex-memory freed or not, right ?
    So why this seg ?

  4. #4
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    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, 04:21 AM
  2. malloc segmentation fault
    By BharathKumar in forum C Programming
    Replies: 5
    Last Post: 06-27-2007, 03:53 AM
  3. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06: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, 01:53 PM

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