![](/img/trans.png)
[英]How to share an existing dynamic array within Linux POSIX model in c language?
[英]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: =========
一些錯誤的地方:
matches
指針數組的大小時使用錯誤的sizeof()
matches
初始化為NULL。 matches
和more_matches
指針。 subject
只讀存儲器 realloc
轉換malloc
和realloc
從來都不是一件好事 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.