0

Possible Duplicate:
Why do I get a segmentation fault when writing to a string?
What is the difference between char a[] = “string”; and char *p = “string”;

I have find the fault ,but I don't know why. so would you help me ?

If I define the char str[] instead the char * str in the main function , it can work normally , else the line of *pSlow = *pFast; will crash with "Unhandled exception at 0x012314f3 in shanchu.exe: 0xC0000005: Access violation writing location 0x0123583c."

Thanks

#include <stdio.h> #include <string.h> #include <iostream> char * delChar(char *s,int iLen) { if((s == NULL) || iLen <= 0) { return NULL; } int i; const int MAXLEN = 26; unsigned int min,hashTable[MAXLEN]; for(i = 0;i < MAXLEN;i ++) { hashTable[i] = 0; } for(i = 0;i < iLen;i ++) { hashTable[*(s + i) - 'a'] ++; } while(hashTable[i] == 0) { i ++; } min = hashTable[i]; for(i = 0;i < MAXLEN;i ++) { if(hashTable[i] != 0) { if(hashTable[i] < min) { min = hashTable[i]; } } } char *pSlow = s; char *pFast = s; while(*pFast != '\0') { if(hashTable[*pFast - 'a'] != min) { *pSlow = *pFast; pSlow ++; } pFast ++; } *pSlow = '\0'; return s; } int main() { char* str = "abadccdehigiktk"; int iLen = strlen(str)/sizeof(char); char *tmp = delChar(str,iLen); printf("%s\n",tmp); system("pause"); } 
1
  • consider using std::string instead Commented Sep 11, 2012 at 5:41

3 Answers 3

4
char* str = "abadccdehigiktk"; 

string-literal should not be modified. In your function delChar you are trying to modify string-literal. It is undefined behaviour.

You should use

char[] str = "abadccdehigiktk"; 

or mb std::string (since you write on C++).

Sign up to request clarification or add additional context in comments.

Comments

1

This line

char* str = "abadccdehigiktk"; 

defines a pointer to a constant string, i.e. the string can not be modified. If you declare it as an array (char str[]) it's an array on the stack, and can therefore be modified.

As for the deletion of characters, why not use e.g. memmove instead?

// "Delete" the fourth character of a string memmove(str + 3, str + 4, strlen(str) - 3); 

If you use std::string instead, it's suddenly much easier using std::string::erase:

std::string str = "abadccdehigiktk"; // Remove the fourth character str.erase(3, 1); 

You also don't have to worry about pointers versus arrays.

4 Comments

You helped me ,Thanks. but if I have defined the const string like "char* str = "abadccdehigiktk";" and I want to delete the 4th character, what should i do ? Thanks.
@crazyer You simply can't as the string when using a pointer is constant. You have to use an array.
@crazyer Of course, if you use std::string you don't have to worry about pointers or arrays, and removing a character becomes much simpler, as I updated my answer to show.
Wow, so beautiful.Yes, the std::string is the best method when using the C++ language.
0

This part is completely wrong:

while(hashTable[i] == 0) { i ++; } min = hashTable[i]; for(i = 0;i < MAXLEN;i ++) { if(hashTable[i] != 0) { if(hashTable[i] < min) { min = hashTable[i]; } } } 

Firstly i has not been initialised so it will be out of range here (it will initially be equal to 'iLen' because of the previous 'for' loop). Secondly the logic is a mess and you can do this in just one loop. I think what you're probably trying to do here is:

min = UINT_MAX; for (i = 0; i < MAXLEN; i++) { if (hashTable[i] > 0 && hashTable[i] < min) { min = hashTable[i]; } } 

i.e. find the minimum non-zero value in hashTable.

1 Comment

Yes, I should initialize i as 0, and my logic is a mess. Thanks for your method. it is perfect. Thanks . But my fault is not here, thanks for your answer and you help me on optimizing my program.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.