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");

}    
Community
  • 1
  • 1
crazyer
  • 1
  • 1

3 Answers3

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++).

ForEveR
  • 55,233
  • 2
  • 119
  • 133
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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 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 Sep 11 '12 at 06:19
  • @crazyer You simply can't as the string when using a pointer is _constant_. You have to use an array. – Some programmer dude Sep 11 '12 at 06:22
  • @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. – Some programmer dude Sep 11 '12 at 06:31
  • Wow, so beautiful.Yes, the std::string is the best method when using the C++ language. – crazyer Sep 11 '12 at 07:04
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.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 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. – crazyer Sep 11 '12 at 06:11