Why is it segfaulting?

This is a discussion on Why is it segfaulting? within the C Programming forums, part of the General Programming Boards category; I've reduced the code to something simple that still segfaults (for me at least) on the memcpy line. It does ...

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Why is it segfaulting?

    I've reduced the code to something simple that still segfaults (for me at least) on the memcpy line. It does the same thing with strcpy. I'm using GCC 3.3 on Gentoo Linux.
    Code:
    char *funct( char *data, int bufsiz )
    {
      
      char *temp = malloc( sizeof( char ) * bufsiz + 1 );
      temp[ bufsiz ] = '\0';
    
      assert( temp != NULL );
    
      memcpy( data, temp, bufsiz );
    
      free( temp );
      
      return data;
      
    }	  
    
    int main( void )
    {
    
      char *str = "hello world";
      printf( "%s\n", funct( str, strlen( str ) ) );
    
      return 0;
      
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Because this
    char *str = "hello world";
    is a pointer to a static array. You cannot overwrite string literals (or at least you shoudln't).
    Use
    char str[] = "hello world";

    What exactly are you trying to do in that function?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >XSquared
    Aroo? I'm mildly confused about you writing this code XSquared. :P

    >char *temp = malloc( sizeof( char ) * bufsiz + 1 );
    sizeof ( char ) isn't needed, the size of a char is guaranteed to be 1. If you want to make sure that the argument to malloc is size_t then make bufsiz size_t. The sizeof only clutters the call.

    >temp[ bufsiz ] = '\0';
    >assert( temp != NULL );
    Wrong order. Check first, then access. An assert isn't meant for run-time checking, it's meant for asserting impossible cases. malloc returning NULL is far from an impossible case.

    >memcpy( data, temp, bufsiz );
    You're writing the contents of temp (which are undefined except for the nul character at the end) to data. Your segfault is likely data being in read-only memory because it is a string literal, but this statement does nothing useful, and plenty undefined.

    I say again, aroo?
    My best code is written with the delete key.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >Wrong order. Check first, then access.
    Those were just a couple of quick, butchered attempts to find the error.

    >which are undefined except for the nul character at the end
    There's a whole lot of processing which happens before the memcpy statement. I just tried to find the simplest case where it fails.

    >the size of a char is guaranteed to be 1
    Heh, ya learn something new every day.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > memcpy( data, temp, bufsiz );
    Too little, and to the wrong place

    memcpy( temp, data, bufsiz+1 );

    You're overwriting your read-only string
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >...and to the wrong place
    I'm copying it to the right place. I'm overwriting data with temp.

    I think you have the arguments to memcpy reversed. From the man pages:
    Code:
    MEMCPY(3)           Linux Programmer's Manual           MEMCPY(3)
    
    NAME
           memcpy - copy memory area
    
    SYNOPSIS
           #include <string.h>
    
           void *memcpy(void *dest, const void *src, size_t n);
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char *str = "hello world";
    printf( "%s\n", funct( str, strlen( str ) ) );
    No. You missed his point: You are trying to write over top of your read only string str. Your arguments are fine. What you're passing to it is not.

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

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Sorry. I forgot that in the code here I still had it as a char *. I changed it to a char [] a while ago and it's been working fine.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segfaulting!
    By CMakesMeSad :( in forum C Programming
    Replies: 23
    Last Post: 07-10-2009, 01:04 PM
  2. why is strncpy segfaulting here?
    By Calef13 in forum C Programming
    Replies: 3
    Last Post: 12-29-2008, 02:27 PM
  3. Segfaulting Distance Program
    By radiohead in forum C Programming
    Replies: 2
    Last Post: 01-09-2006, 07:48 PM
  4. a segfaulting algorythm
    By demonus in forum C Programming
    Replies: 8
    Last Post: 08-11-2003, 08:06 AM

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