Find the answer to your Linux question:
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 ...
  1. #1
    Just 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:

    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   }
    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!

    Using GDB at that line after the segfault, I can see that:

    Code:
    (gdb) print i
    $1 = 1
    (gdb) print j
    $2 = 0
    (gdb) print number
    $3 = 0x4012d9 "0102"
    number is pointing to the same address as when the method was entered, and even more 'bafflingly':

    Code:
    (gdb) print number[j]
    $5 = 48 '0'
    (gdb) print number[j + i]
    $6 = 49 '1'
    I can get GDB to execute the line with no problems.

    So, as far as I can tell, the line being executed at the segfault is

    Code:
    number[0] = number[0 + 1]
    and both array indices are legal and fine?

    Why is this happening? Any help will be much appreciated!

    Thanks
    Last edited by lemons; 08-09-2010 at 08:56 PM. Reason: Solved it

  2. #2
    Just Joined!
    Join Date
    Feb 2009
    Location
    Southport, England
    Posts
    31
    Sorry, I've solved it now. The number was initialised as

    Code:
    char* number = "0102";
    which wasn't right.

    Apologies!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...