Thread: Segmentation Fault

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    Segmentation Fault

    Hello, I'm making a program that copy value from the source to the destination.

    Here is the code :

    in .c

    Code:
    void test_insertFP (void)
    {
    
        struct fp_des * p_fp;
    
        UC8 * p;
        UC8 dd, pp;
        p_fp= (struct fp_des *) malloc (sizeof (struct fp_des));
        p = (UC8*) malloc (sizeof (1));
    
        p_fp->p = &(dd);
        dd = 20;
        p = &(pp);
        insertFP (p_fp, p);
        (pp == dd);
    
    
    }

    I define UC8 as "unsigned char"
    Build succesful, but I have segmentation fault.
    Thanks for the help

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by dulipat View Post
    I have segmentation fault.
    Thanks for the help
    Have you made any attempt to isolate where? Why did you choose that function to post? What's this:

    Code:
       insertFP (p_fp, p);
    A couple of things I'll point out:
    Code:
        UC8 dd, pp;
        p_fp= (struct fp_des *) malloc (sizeof (struct fp_des));
        p = (UC8*) malloc (sizeof (1));
    
        p_fp->p = &(dd);
        dd = 20;
        p = &(pp);
        insertFP (p_fp, p);
        (pp == dd);  // totally irrelevant and meaningless
    The red part is a memory leak. You allocate a byte to p, then you throw the handle away by re-assigning it to something else.
    Last edited by MK27; 05-25-2010 at 10:29 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2
    problem solved..thx

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Why do you go ahead and malloc one byte, for something that looks like it's a member of fp_des struct? I'm thinking about the p_fp->p line, if p is a member of p_fp, you already got the memory from the first malloc call.

    Also, p is a pointer, so the size is not 1 but at least 4, possibly 8.
    Last edited by Subsonics; 05-25-2010 at 10:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Annoying Segmentation Fault
    By Zildjian in forum C++ Programming
    Replies: 7
    Last Post: 10-08-2004, 02:07 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM