简体   繁体   English

Linux POSIX C动态字符串数组问题

[英]Linux POSIX C dynamic string array issue

I have the following code, it searches for some matches with regex (pcre) then adds the matches to a dynamically growing array (so I can make the matches unique)... the problem I get two warnings when compiling and when running the program crashes. 我有以下代码,它使用正则表达式(pcre)搜索一些匹配项,然后将匹配项添加到动态增长的数组中(这样我就可以使匹配项唯一)...问题:在编译和运行程序时出现两个警告崩溃。

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pcre.h>

int main() {
  pcre *myregexp;
  const char *error;
  int erroroffset;
  int offsetcount;
  int offsets[(0+1)*3]; // (max_capturing_groups+1)*3
  const char *result;
  int n;
  int count = 1;
  char **matches;
  char **more_matches;
  char *subject = "9,5,3,2,5,6,3,2,5,6,3,2,2,2,5,0,5,5,6,6,1,";
  myregexp = pcre_compile("\\d,", PCRE_MULTILINE|PCRE_DOTALL, &error, &erroroffset, NULL);

  if (myregexp != NULL) {
    offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), 0, 0, offsets, (0+1)*3);

    while (offsetcount > 0) {

      if (pcre_get_substring(subject, offsets, offsetcount, 0, &result) >= 0) {
        printf("%s\n", result);
        more_matches = (char *) realloc(matches, count * sizeof(char));
        if (more_matches!=NULL) {
          matches=more_matches;
          matches[count-1]=result;
          count++;
        }
        else {
          free(matches);
          puts("Error (re)allocating memory");
          exit(1);
       }
      }

      offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), offsets[1], 0, offsets, (0+1)*3);
    }
    for (n=0; n<count; n++) printf("%s\n", matches[n]);
    free(matches);
  } else {
      printf("Syntax error in REGEX at erroroffset\n");
  }

}

casting something wrong? 投错了什么?

$ gcc -o pcre_ex_arr pcre_ex_arr.c -lpcre
pcre_ex_arr.c: In function 'main':
pcre_ex_arr.c:29: warning: assignment from incompatible pointer type
pcre_ex_arr.c:32: warning: assignment discards qualifiers from pointer target type
$ ./pcre_ex_arr
2,
*** glibc detected *** ./pcre_ex_arr: realloc(): invalid pointer: 0xb7fcfb80 ***
======= Backtrace: =========

A number of things wrong: 一些错误的地方:

  1. Using the wrong sizeof() when resizing your matches pointer array 在调整matches指针数组的大小时使用错误的sizeof()
  2. Failing to initialize matches to NULL before the parsing begins. 在解析开始之前未能将matches初始化为NULL。
  3. Using the wrong type for the matches and more_matches pointers. 使用错误类型的matchesmore_matches指针。
  4. Using a volatile pointer to read-only memory for subject 使用易失性指针指向subject只读存储器
  5. Casting malloc and realloc is never good in C 在C中realloc转换mallocrealloc从来都不是一件好事
  6. The math on your count is flaky-at-best. 你的数学count是鳞片状的,最好的。 It should start at 0, not 1 它应该从0开始,而不是1

See below: 见下文:

int main()
{
    pcre *myregexp;
    const char *error;
    int erroroffset;
    int offsetcount;
    int offsets[(0+1)*3]; // (max_capturing_groups+1)*3
    const char *result;
    int n;
    int count = 0;
    const char **matches = NULL;
    const char **more_matches;

    char subject[] = "9,5,3,2,5,6,3,2,5,6,3,2,2,2,5,0,5,5,6,6,1,";
    myregexp = pcre_compile("\\d,", PCRE_MULTILINE|PCRE_DOTALL, &error, &erroroffset, NULL);

    if (myregexp != NULL) {
        offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), 0, 0, offsets, (0+1)*3);

        while (offsetcount > 0) {

            if (pcre_get_substring(subject, offsets, offsetcount, 0, &result) >= 0)
            {
                printf("%s\n", result);
                more_matches = realloc(matches, (count+1)* sizeof(*more_matches));
                if (more_matches!=NULL)
                {
                    matches=more_matches;
                    matches[count++]=result;
                }
                else
                {
                    free(matches);
                    puts("Error (re)allocating memory");
                    exit(1);
                }
            }

            offsetcount = pcre_exec(myregexp, NULL, subject, strlen(subject), offsets[1], 0, offsets, (0+1)*3);
        }

        for (n=0; n<count; n++) printf("%s\n", matches[n]);
        free(matches);

    } else {
        printf("Syntax error in REGEX at erroroffset\n");
    }
}

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

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