簡體   English   中英

Linux POSIX C動態字符串數組問題

[英]Linux POSIX C dynamic string array issue

我有以下代碼,它使用正則表達式(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. 在調整matches指針數組的大小時使用錯誤的sizeof()
  2. 在解析開始之前未能將matches初始化為NULL。
  3. 使用錯誤類型的matchesmore_matches指針。
  4. 使用易失性指針指向subject只讀存儲器
  5. 在C中realloc轉換mallocrealloc從來都不是一件好事
  6. 你的數學count是鱗片狀的,最好的。 它應該從0開始,而不是1

見下文:

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