![]() |
| | #1 |
| C++ Developer Join Date: Jun 2002 Location: UWaterloo
Posts: 2,718
| Why is it segfaulting? 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 | |
| | #2 |
| End Of Line 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 | |
| | #3 |
| Code Goddess 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 | |
| | #4 |
| C++ Developer 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 | |
| | #5 |
| and the hat of vanishing 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 | |
| | #6 |
| C++ Developer 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 | |
| | #7 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Code: char *str = "hello world"; printf( "%s\n", funct( str, strlen( str ) ) ); Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #8 |
| C++ Developer 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |