我试图将其插入C中,我不知道为什么我的代码在做strcat时会崩溃

发布于 2025-02-10 05:31:21 字数 1552 浏览 5 评论 0原文

 我试图将其插入C中,我不知道为什么我的代码在做strcat时会崩溃
 
  char** split(const char* str, char delimiter)
    {
        int index=0;
        int size=num_items(str,delimiter);
        char split[size+1];

//单元格代表阵列中的单元格

        char* cell=(char*)malloc(1);
        for(int i=0; i<strlen(str); i++)
        {
            if(str[i]!=delimiter)
            {

在添加下一个炭之前的返回大小的大小增加了每个未介绍的char

            cell=realloc(cell,sizeof(cell)+8);
  
    ***the program get crashed after the strcat***
 
                strcat(cell,(unsigned const char*)str[i]); 
            }else{
                split[index]= (char) cell;
                free(cell);
                char* cell=(char*)malloc(0);
            }
        }
        return split;
    }

*** ***,在再次写入后,我对strcat也有同样的问题,可以向我解释我错误?***

char** split(const char* str, char delimiter)
{
    int ch=0;
    int word=0;
    const char * zero="\0";
    unsigned int size=num_items(str,delimiter);
    //char* cell=calloc(1,sizeof(char));
    char** split= calloc(size+1,sizeof(char*));
    for(int i=0; i<strlen(str); i++) //i=colum
    {
        if(ch==0)
        {   //strcat(split[word],zero);
            memset(split[word],'\0',1);
            ch++;
        }
        if(str[i]!=delimiter)
        {
            strcat(split[word],&str[i]);
            ch++;
        }else{
            ch=0;
            word++;
        }
    }
    return split;
}
    i am trying to implemet split in c , i doesnt know why the my code get crashed when he do strcat
  char** split(const char* str, char delimiter)
    {
        int index=0;
        int size=num_items(str,delimiter);
        char split[size+1];

// cell represent the cell in the array to return

        char* cell=(char*)malloc(1);
        for(int i=0; i<strlen(str); i++)
        {
            if(str[i]!=delimiter)
            {

Increases the size of the cell before adding to it the next char every unsinged char is 8bits

            cell=realloc(cell,sizeof(cell)+8);
  
    ***the program get crashed after the strcat***
 
                strcat(cell,(unsigned const char*)str[i]); 
            }else{
                split[index]= (char) cell;
                free(cell);
                char* cell=(char*)malloc(0);
            }
        }
        return split;
    }

*** after writting again, i have the same problem with strcat, can somone explain to me my mistakes?***

char** split(const char* str, char delimiter)
{
    int ch=0;
    int word=0;
    const char * zero="\0";
    unsigned int size=num_items(str,delimiter);
    //char* cell=calloc(1,sizeof(char));
    char** split= calloc(size+1,sizeof(char*));
    for(int i=0; i<strlen(str); i++) //i=colum
    {
        if(ch==0)
        {   //strcat(split[word],zero);
            memset(split[word],'\0',1);
            ch++;
        }
        if(str[i]!=delimiter)
        {
            strcat(split[word],&str[i]);
            ch++;
        }else{
            ch=0;
            word++;
        }
    }
    return split;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

横笛休吹塞上声 2025-02-17 05:31:21

在动态分配内存的范围内

cell=realloc(cell,sizeof(cell)+8);

您正在使用魔术表达式sizeof(cell)+8(这是没有意义的),

。因此,由于内存的范围不包含字符串,因此您不得使用函数strcat除此之外,

strcat(cell,(unsigned const char*)str[i]);

在任何情况下,函数都不是有意义的,并且具有未定义的行为。例如,数组split是该函数的本地数组,

char split[size+1];

该数组在此返回语句中

return split;

具有自动存储持续时间,该数组指定器被隐式转换为指针,转换为其类型char *。但是,函数返回类型为char **

char** split(const char* str, char delimiter)
^^^^^^

这是返回表达式的类型,函数的返回类型不兼容。此外,本地数组 split 退出功能后将不会活着。因此,无论如何,返回的指针都是无效的。

否则此陈述

split[index]= (char) cell;

没有意义。

您需要重新重写功能。

You are allocating dynamically an uninitialized extent of memory

cell=realloc(cell,sizeof(cell)+8);

using the magic expression sizeof(cell)+8 (that does not make a sense).

So as the extent of memory does not contain a string you may not use the function strcat like

strcat(cell,(unsigned const char*)str[i]);

Apart from this the function in any case does not make a sense and has undefined behavior. For example the array split is a local array of the function with automatic storage duration

char split[size+1];

In this return statement

return split;

the array designator is implicitly converted to a pointer to its first element of the type char *. However the function return type is char **.

char** split(const char* str, char delimiter)
^^^^^^

That is the type of the returned expression and the return type of the function are not compatible. Moreover the local array split will not be alive after exiting the function. So in any case the returned pointer will be invalid.

Or this statement

split[index]= (char) cell;

does not make a sense.

You need to rewrite the function anew.

差↓一点笑了 2025-02-17 05:31:21
  1. 您正在重新分配未分配的内存。
  2. 您正在尝试将字符串与非初始化的缓冲区相结合。您需要在分配后的第一个缓冲区的第一个元素“ memset()”此缓冲区或设置'\ 0'字符。
  3. 您正在尝试将字符串拆分为字符串数组,但是您没有有关分裂后在数组中写入多少令牌的信息。当您需要此数组的迭代时,这也将是一个问题。您可以在最后一个数组元素中写入null,也可以将第三个指针参数传递到拆分函数,您将能够保存数组长度。

PS已经实现了分裂的实现,您可以检查一下或将其用作片段。

这是项目: libxutils 。在src/xstr.c文件中检查xstrsplit()函数。

示例代码看起来像这样:

const char *pMyString = "this:is:test:strig";

xarray_t *pTokens = xstrsplit(myString, ":");
size_t i, nUsed = XArray_Used(pTokens);

for (i = 0; i < nUsed; i++)
{
    char *pTok = (char *)XArray_GetData(pTokens, i);
    printf("token: %s, order id: %zu\n", pTok, i);
}

XArray_Destroy(pTokens);
  1. You are reallocating unallocated memory.
  2. You are trying to catenate string to the uninitialized buffer. You need to 'memset()' this buffer or set '\0' character to the first element of this buffer after allocation.
  3. You are trying to split a string into a string array, but you do not have information on how many tokens are written in the array after splitting. This will be also a problem when you need an iteration of this array. You can either write NULL in the last array element or pass the third pointer argument to the split function where you will be able to save array length.

P.S. there is an already made implementation of split and you can check this out or use it as a snippet.

This is the project: libxutils. Check xstrsplit() function in src/xstr.c file.

The example code is looking something like that:

const char *pMyString = "this:is:test:strig";

xarray_t *pTokens = xstrsplit(myString, ":");
size_t i, nUsed = XArray_Used(pTokens);

for (i = 0; i < nUsed; i++)
{
    char *pTok = (char *)XArray_GetData(pTokens, i);
    printf("token: %s, order id: %zu\n", pTok, i);
}

XArray_Destroy(pTokens);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文