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

时间:2014-02-08 20:07:16

标签: c arrays posix

我有以下代码,它搜索与regex(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");
  }

}

投错了什么?

$ 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: =========

1 个答案:

答案 0 :(得分:1)

有很多错误:

  1. 调整sizeof()指针数组的大小时使用错误的matches
  2. 在解析开始之前未能将matches初始化为NULL。
  3. matchesmore_matches指针使用错误的类型。
  4. 将易失性指针用于subject
  5. 的只读内存
  6. 在{C
  7. 中投射mallocrealloc永远不会好
  8. count上的数学表达最佳。它应该从0开始,而不是1
  9. 见下文:

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