Thread: memcpy resulting in Segmentation fault

  1. #1
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27

    Arrow memcpy resulting in Segmentation fault

    Despite seeing the correct data in gdb when I
    Code:
    p *(DownloadFileChunkResponse.DownloadFileChunkResult)->__ptr@100
    the following C code gives me a SEG FAULT
    Code:
    calloc(NewMediaChunk, ChunkSize);
    memcpy((void *)NewMediaChunk, *(DownloadFileChunkResponse.DownloadFileChunkResult)->__ptr, ChunkSize);
    Please tell me how I got this wrong.
    BTW The data I am attempting to memcpy is the frontend of an MP4 file containing a number of NULLS.
    Code:
    \000\000\000\030ftypmp42\000\000\000\000mp42mp41\000\000\tdfree
    BTW ChunkSize is 64000
    Last edited by straygrey; 11-06-2013 at 06:11 AM.

  2. #2
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27
    That is what happens when one thinks they know what they are doing and don't read the manual.
    My calloc was something that I sucked out of my thumb. Sorry guys.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27
    As much as I would like to believe that I solved the issue, I did not.
    I am still getting:-
    Code:
    Program received signal SIGSEGV, Segmentation fault.
    __memcpy_ssse3 () at ../sysdeps/i386/i686/multiarch/memcpy-ssse3.S:142
    142    ../sysdeps/i386/i686/multiarch/memcpy-ssse3.S: No such file or directory.
    despite altering my code to:-
    Code:
                       NewMediaChunk = calloc(sizeof(size_t), ChunkSize);
                        if (NewMediaChunk == NULL)
                            {
                            result = sprintf(Message, "In %s at line %d calloc failed",__FILE__, __LINE__);
                            LogMessage(Message);
                            exit(99);
                            } 
                        memcpy(NewMediaChunk, *(DownloadFileChunkResponse.DownloadFileChunkResult)->__ptr, ChunkSize);

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You probably need
    Code:
       NewMediaChunk = calloc(1, ChunkSize);
    My guess is also that the dereferencing of the second argument of memcpy() is incorrect. That is a guess, as you haven't specified what type *(DownloadFileChunkResponse.DownloadFileChunkResul t)->__ptr is. However, the second argument of memcpy() is required to be a pointer to a buffer at least the length of the last (ChunkSize).

    Also, names with a leading underscore are not really a good idea. A lot of C compilers are actually C++ compilers, and names starting with two underscores are reserved (so using them gives undefined behaviour).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27
    Thank you grumpy. BTW the underscore prefix was something inherited from gsoap.

  6. #6
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by straygrey View Post
    BTW the underscore prefix was something inherited from gsoap.
    You're mistaken ;-)

  7. #7
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27
    SirPrattlepod, please explain.

  8. #8
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by straygrey View Post
    SirPrattlepod, please explain.
    How is the underscore prefix inherited from gsoap? Or do you mean your code specifically?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by SirPrattlepod View Post
    How is the underscore prefix inherited from gsoap? Or do you mean your code specifically?
    gSoap generates a bunch of boilerplate, and it's very fond of the underscore.

  10. #10
    Registered User
    Join Date
    Oct 2013
    Location
    Midrand Gauteng South Africa
    Posts
    27
    anduril462 is quite correct and the variable named with a preceding underscore was named and generated by gsoap.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memcpy into shm resulting in segfault?
    By 0xa0d933h52 in forum C Programming
    Replies: 5
    Last Post: 10-09-2013, 08:28 AM
  2. Segmentation Fault?
    By TIMBERings in forum C Programming
    Replies: 4
    Last Post: 10-08-2009, 07:30 PM
  3. segmentation fault in memcpy
    By George2 in forum C Programming
    Replies: 14
    Last Post: 08-24-2007, 02:58 PM
  4. segmentation fault using memcpy
    By dr$brown in forum C Programming
    Replies: 6
    Last Post: 12-21-2004, 10:55 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread