John.W
John.W

Reputation: 11

store to null pointer of type 'char'

I get the problem when I solved the problem 3 on leetcode.I write the code and it works on Dev-C++ compiler,and I copy the code and paste on the leetcode.Get this error:Line 10: Char 9: runtime error: store to null pointer of type 'char' (solution.c).I checked the code, but can't find some problems.I wish get some help.The code is paste on the below.

int lengthOfLongestSubstring(char * s){
    int longestlength = 0;  // 最长子串的长度 
    int i = 0;
    char *temp; // 存储子串 
    char *p;
    char *base;

    * temp = * s; // *temp存储s的第一个字符 
    base = temp;

    while(*++s != '\0') {
        p = base;
        while(*p++ != '\0') {
            if (*p == *s) {
                *temp = '\0';
                break;
            }
        }
        if (*temp == '\0') {
            *temp++ = *s;
            base = temp;
            i = 0;  // 重新计数 
        }
        else {
            *++temp = *s;
            ++i;
        } 
        longestlength = i > longestlength ? i : longestlength;
    } 

    return longestlength;
}
Test Example
char* s = "pwwkew";

Upvotes: 0

Views: 871

Answers (2)

John.W
John.W

Reputation: 11

I modified my code to correct the errors that you pointed.

int lengthOfLongestSubstring(char * s){
    int longestlength = 0;  // 最长子串的长度 
    int i = 1;
    char *temp; // 存储子串 
    char *p;
    char *base;
    int nowsize = 0;

    temp = (char *)malloc(2*sizeof(char)); 

    *temp = *s; // *temp存储s的第一个字符 
    *(temp+1) = '\0';
    base = temp;
    nowsize = 2;
    longestlength = i;

    while(*++s != '\0') {
        p = base;
        while(*p != '\0') {
            if (*p == *s) {
//              *temp = '\0';   
                break;
            }
            p++;
        }
        if (*p != '\0') {
            free(base);
            base = (char *)malloc(2*sizeof(char));
            temp = base;
            *temp = *s;
            *(temp+1) = '\0'; 
            i = 1;  // 重新计数
            nowsize = 2; 
        }
        else {
            base = (char *) realloc(base, (++nowsize)*sizeof(char));
            temp = base + nowsize - 2;
            *temp = *s;
            *(temp+1) = '\0';
            temp++;
            ++i;        
        }   
        longestlength = i > longestlength ? i : longestlength;
    } 

    return longestlength;
}

I explain some varaibles in my program, temp pointed the longest unrepeated substring, and base pointed the base address, and p is for traverse.i is incremented only the character in s not repeated in temp.

The program run my well with the test example that the problem3 provided.But when I posted, I get the errors.AddressSanitizer: heap-buffer-overflow on address 0x602000000331 at pc 0x0000004018ab bp 0x7fff4145b010 sp 0x7f.I try my best and can't simulate the situation.I wish get some help,thank you!

Upvotes: 0

chmike
chmike

Reputation: 22174

The function is the proposed solution to the problem suggested by leetcode here.

As I understand it, we have to return the length of the longest substring without repeating character. A substring ends when the after last character appears at any place in the substring.

There are many problems with the presented code.

  • The error is because there is no storage for temp.
  • base is a copy of temp which is not a terminated string, but it is scanned with pointer p.
  • i is incremented in certain conditions, but never reset to zero. When a new substring is started, it should be reset to 0.
  • etc.

I don't see how this code can provide the expected answer.

The algorithm is as follow.

we start with a substring whose first letter is at index i (initialized to 0), and we consider the after last character of the substring at index j. We check if the character is present in the substring. if yes, we met the end of the substring. It's length is j-i. longestLength is then set to the max of longestLength and j-i. We start a new substring by setting i to the letter after the duplicate letter in the substring. Finally, we increment j.

Here is the code

int lengthOfLongestSubstring(char *s){
    if (s == NULL || *s == '\0')
        return 0;
    int longestlength = 1;
    int i = 0, j = 1;
    while (s[j] != '\0') {
        // check if s[j] is in substring
        int k = i;
        while (k < j) {
            if(s[k] == s[j])
                break;
            k++;
        }
        // if we found a duplicate letter at index k
        if (k != j) {
            // we reached the end of the substring
            longestLength = j-i > longestLength ? j-i : longestLength;
            // next substring start at k+1
            // and is one character long
            i = k+1;
            j = i;
        }
        j++;
    }
    return longestLength;
}

Upvotes: 1

Related Questions