C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-29-2004, 07:15 PM   #1
C++ Developer
 
XSquared's Avatar
 
Join Date: Jun 2002
Location: UWaterloo
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
XSquared is offline   Reply With Quote
Old 03-29-2004, 07:33 PM   #2
End Of Line
 
Hammer's Avatar
 
Join Date: Apr 2002
Posts: 6,240
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]
Hammer is offline   Reply With Quote
Old 03-29-2004, 07:51 PM   #3
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,661
>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.
Prelude is offline   Reply With Quote
Old 03-29-2004, 08:15 PM   #4
C++ Developer
 
XSquared's Avatar
 
Join Date: Jun 2002
Location: UWaterloo
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
XSquared is offline   Reply With Quote
Old 03-30-2004, 12:47 AM   #5
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> 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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 03-30-2004, 06:41 AM   #6
C++ Developer
 
XSquared's Avatar
 
Join Date: Jun 2002
Location: UWaterloo
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
XSquared is offline   Reply With Quote
Old 03-30-2004, 06:50 AM   #7
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 03-30-2004, 06:52 AM   #8
C++ Developer
 
XSquared's Avatar
 
Join Date: Jun 2002
Location: UWaterloo
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
XSquared is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
segfaulting! CMakesMeSad :( C Programming 23 07-10-2009 01:04 PM
why is strncpy segfaulting here? Calef13 C Programming 3 12-29-2008 03:27 PM
Segfaulting Distance Program radiohead C Programming 2 01-09-2006 08:48 PM
a segfaulting algorythm demonus C Programming 8 08-11-2003 08:06 AM


All times are GMT -6. The time now is 08:37 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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