Thread: malloc + segmentation fault

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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