Results 1 to 2 of 2
Hi guys,
I really am out of my depth here, unless I'm missing something that's screaming at me!
I have a C method which trims any leading zeroes off a ...
- 08-09-2010 #1Just Joined!
- Join Date
- Feb 2009
- Location
- Southport, England
- Posts
- 31
[SOLVED] Mystery segfault
Hi guys,
I really am out of my depth here, unless I'm missing something that's screaming at me!
I have a C method which trims any leading zeroes off a number in char* form:
uint64 is typedef'd as unsigned long long in this case and is compiled and ran on x86_64. The segfault occurs at line 67. I pass the string "0102" into the method and it happens!Code:33 void myTrim(char* number) 34 { 35 assert(number); 36 37 if (number[0] == 48) 38 { 39 uint64 i = 0; 40 uint64 j; 41 42 uint64 numberLength = strlen(number); 43 44 while (number[i] == 48) 45 { 46 i++; 47 48 if (i == numberLength) 49 { 50 /* 51 SPECIAL CASE: all digits are zero, i.e., number is zero 52 (regardless of zero padding!) 53 */ 54 55 number[0] = 48; // '0' 56 57 for (j = 1; j < numberLength; j++) 58 number[j] = 0; // '\0' 59 60 return; 61 } 62 } 63 64 uint64 size = numberLength - i; 65 66 for (j = 0; j < size; j++) 67 number[j] = number[j + i]; 68 69 for (; j < numberLength; j++) 70 number[j] = 0; 71 } 72 }
Using GDB at that line after the segfault, I can see that:
number is pointing to the same address as when the method was entered, and even more 'bafflingly':Code:(gdb) print i $1 = 1 (gdb) print j $2 = 0 (gdb) print number $3 = 0x4012d9 "0102"
I can get GDB to execute the line with no problems.Code:(gdb) print number[j] $5 = 48 '0' (gdb) print number[j + i] $6 = 49 '1'
So, as far as I can tell, the line being executed at the segfault is
and both array indices are legal and fine?Code:number[0] = number[0 + 1]
Why is this happening? Any help will be much appreciated!
ThanksLast edited by lemons; 08-09-2010 at 08:56 PM. Reason: Solved it
- 08-09-2010 #2Just Joined!
- Join Date
- Feb 2009
- Location
- Southport, England
- Posts
- 31
Sorry, I've solved it now. The number was initialised as
which wasn't right.Code:char* number = "0102";
Apologies!


