简体   繁体   English

C中带有malloc的子字符串函数返回不正确的值

[英]Substring function in C with malloc returning incorrect value

I am implementing a parser in C for first order logic formulas. 我正在用C实现用于一阶逻辑公式的解析器。 To check for a binary connective formula (ie in the form (A BinaryConnective B)) I need to split the string and check if A and B are formulas. 要检查二进制连接式(即形式为(A BinaryConnective B)),我需要分割字符串并检查A和B是否为公式。 I have done this using a subString function and I call it from partone and parttwo (A and B respectively): 我使用subString函数完成了此操作,并从partone和parttwo(分别为A和B)调用它:

char *partone(char *g) {
    //Given the formula (A*B) this returns A
    return subString(g, 1, binPosition(g));
}

char *parttwo(char *g) {
    //Given the formula (A*B) this returns B
    return subString(g, binPosition(g) + 1, strlen(g) - 1);
}

The substring function is the following: 子字符串函数如下:

char *subString(char *g, int start, int end) {
    //the substring includes index start but does not include the end index.
    char *substr = malloc(sizeof(char)*(end - start));
    int i;
    for(i = 0; i < (end - start); i++) {
        substr[i] = g[start + i];
    }
    return substr;
}

This works when I pass any function except a negated formula (we are using the character "-" to denote negation). 当我传递除否定公式之外的任何函数时,此方法有效(我们使用字符“-”表示否定)。 For example when I pass (-X[xz]>X[yz]), the program returns "Not a Formula", but if I write the same without the negation it works perfectly. 例如,当我通过(-X [xz]> X [yz])时,程序将返回“ Not a Formula”,但是如果我在不加取反的情况下编写相同的代码,则它会完美运行。 The problem is that the substr that is returned for partone() is "-X[xz]$" where $ can be any random character that I think was stored in memory before. 问题是,为partone()返回的substr是“ -X [xz] $”,其中$可以是我认为之前存储在内存中的任何随机字符。 Any ideas why this is happening ONLY in this case? 有什么想法为什么只会在这种情况下发生? I am new to C and I have looked everywhere. 我是C语言的新手,到处都是。

Thanks in advance. 提前致谢。

You forgot the terminating NUL in your subString function. 您在subString函数中忘记了终止NUL。 Your malloc should look like 您的malloc应该看起来像

char* substring = (char*)malloc (sizeof (char)*(end-start+1));

And at the end you need to terminate the result with 0: 最后,您需要使用0终止结果:

substring [end-start]=0;

And since arrays in c are zero-indexed, I'm suprised that you don't lose the leading "-" of part A, since partone calls subString with 1 as start . 而且由于c中的数组是零索引的,所以我很惊讶您不会丢失A部分的前导“-”,因为partone调用subString1作为start

EDIT: there are a lot of built-in functions for string manipulation in C/C++. 编辑:在C / C ++中有很多内置函数可用于字符串处理。 You should use them since they are tested and optimised. 您应该使用它们,因为它们已经过测试和优化。 I did not use C so much for many years now so I'm not up to date what are the appropriate methods in C++11 today, but Google may tell you. 我已经很多年没有使用C了,所以我不了解C ++ 11中今天合适的方法,但是Google可能会告诉您。

EDIT: As chux pointed out, the meaning of your end parameter is not fully clear. 编辑:正如chux指出的那样,您的end参数的含义还不完全清楚。 So you need to check the strlen (g)-1 in parttwo or the loop conditions in subString and increase to +2 in my malloc statement`. 因此,您需要检查第二部分的strlen (g)-1parttwo的循环条件,并在我的malloc语句subString其增加到+2

OP's use of start and end is not formalized, but I'll assume they are indexes into the string of the first and last character of the desired sub-string. OP的startend使用尚未正式确定,但我假设它们是所需子字符串的第一个和最后一个字符的字符串的索引。

Recall in C, array indexes begin with 0 as in g[0] is the first character of the string stored at g . 在C中,数组索引以0开头,如g[0]是存储在g处的字符串的第一个字符。

If that case, code needs to allocate end - start + 1 + 1 space and insure the allocated character array has a null character '\\0' at the end. 在这种情况下,代码需要分配end - start + 1 + 1空间,并确保分配的字符数组在结尾处具有空字符'\\0'

char *subString(const char *g, int start, int end) {
  if (end < start || start < 0) {
    return NULL;
  }
  size_t size = 2u + end - start;
  char *substr = malloc(size);
  if (substr) {
    int i;
    for(i = 0; i < (end - start); i++) {
      substr[i] = g[start + i];
    }
    substr[i] = '\0';
  }
  return substr;
}

Other simplifications/improvements possible 可能的其他简化/改进

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM