I have a situation where I want to efficiently remove a character from a NULL-terminated char *. I can assume the incoming string is large (i.e. it wouldn't be efficient to copy); but I can also assume that I don't need to de-allocate the unused memory.
I thought I could use std::remove_if
for this task (replacing the character at the returned iterator with a NULL-terminator), and set up the following test program to make sure I got the syntax correct:
#include <algorithm>
#include <iostream>
bool is_bad (const char &c) {
return c == 'a';
}
int main (int argc, char *argv[]) {
char * test1 = "123a45";
int len = 6;
std::cout << test1 << std::endl;
char * new_end = std::remove_if(&test1[0], &test1[len], is_bad);
*new_end = '\0';
std::cout << test1 << std::endl;
return 0;
}
This program compiles, however, I'm getting a Segmentation Fault
somewhere in remove_if
- here's the output from gdb
:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400914 in std::remove_copy_if<char*, char*, bool (*)(char const&)> (__first=0x400c2c "45", __last=0x400c2e "", __result=0x400c2b "a45",
__pred=0x4007d8 <is_bad(char const&)>) at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:1218
1218 *__result = *__first;
This is with gcc 4.1.2
on RedHat 4.1.2-52.
My understanding was that raw pointers can be used as ForwardIterators
, but perhaps not? Any suggestions?