第一次循环迭代后将字符串更改为 NULL。内存分配

标签 c string visual-c++ memory memory-management

程序应该从文件中获取字符串并将它们分开,在其中找到字符串“delim”。我应该使用动态内存,但抱歉我不能完美地使用它。在循环的第一次迭代后将“cpystr”从“SUCCESSZOMBIEAAAAAAAaaaAZOMBIEaaAZOMBIEdf”更改为 NULL 的原因是什么?“SUCCESSZOMBIEAAAAAAAaaaAZOMBIEaaAZOMBIEdf”,其中“ZOMBIE”是“delim”。帮我解决这个问题。感谢您提前提供帮助!

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string>

char** split(const char *str, const char *delim)
{
    int cntr1, cntr2, cntr3, cntdelim=0;
    bool check;
    //counting "delim" in the str

    char **maspntr=NULL;
    char *cpystr=NULL;
    cpystr=_strdup(str); //reserve copy of the "cpy"
    strcpy(cpystr,str);
    for (cntr1=0;cntr1<(strlen(cpystr)+1);cntr1++) //THE PROBLEM WITH THIS CYCLE!
    {

    check=1;
    for (cntr2=0, cntr3=cntr1;cntr2<strlen(delim); cntr2++, cntr3++) //searching for "delim" 
        if (!(cpystr[cntr3]==delim[cntr2]))
            {

                check=0;
                break;
            }
        if (check) //if it's foound, it copies the first N characters as the first element of pointers' array 
        {

            maspntr=(char**)realloc(maspntr,(cntdelim+1)*sizeof(char*));
            maspntr[cntdelim]=(char*)calloc(cntr1, sizeof(char));
            cntdelim++;
            for (cntr3=0;cntr3<=(strlen(cpystr)-(cntr1+strlen(delim)-2));cntr3++)
            {
                cpystr[cntr3]=cpystr[cntr3+cntr1+strlen(delim)];
                cpystr=(char*)realloc(cpystr,(strlen(cpystr)+1-cntr1-strlen(delim))*sizeof(char)); //delete first N characters +"delim" by copying the characters from the end then modifying memory
            }

        }
         if(cpystr[cntr1]='\0') //if it is the end of string return
         {

             for (cntr1=0; cntr1<sizeof(maspntr); cntr1++)
         {
             printf(maspntr[cntr1],'\n');
         }
             free(cpystr);

             return(maspntr);
         }

    }   //Changing "cpystr" to NULL right here






}
int main()
{
    char singlech;
    int len=0;

    //copy string from the file to the variable
    FILE* foo;
errno_t err;
    err=fopen_s(&foo,"input.txt","r");
    if( err == 0 )
  {
      printf( "The file 'input.txt' was opened\n" );
  }
  else
  {
      printf( "The file 'input.txt' was not opened\n" );
  }
    while (!feof(foo))  //rather stupid way of getting memory
    {
        fscanf_s(foo, "%c", &singlech);
        len++;
    }
    char *str=NULL, *delim=NULL;

    str=(char*)calloc(len,sizeof(char));
    rewind(foo);
    fgets(str,len,foo);
    delim=(char*)calloc(sizeof("ZOMBIE")+1, sizeof(char));
    strcpy(delim,"ZOMBIE");
    split(str,delim);
    printf(str);
    free(str);
    free(delim);

    getchar();

}

最佳答案

我没有阅读你的所有代码,但是

cpystr = (char*)malloc(sizeof(cpystr))

分配“指针大小”字节(可能是 4 或 8),因此以下 strcpy() 写入超出分配的内存。

你的意思可能是

cpystr = malloc(strlen(str) + 1); // casting the return value of malloc not needed

但是为什么不简单地写

cpystr = strdup(str);

而不是malloc() + strcpy()

关于第一次循环迭代后将字符串更改为 NULL。内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19348453/

相关文章:

C编程 : reate 10 Element Array, 冒泡排序算法,返回数组的最小值和最大值

c++ - 名字和姓氏不一起读入集合

c++ - VC++ 中 sqrt() 的值始终为 0

visual-studio - 如何在 Visual Studio 2010 中进行并行构建?

c++ - 如何在 Visual C++ 中手动修改名称?

c - Backspace 不删除字符,类似于 BASH

c - C中函数指针的递归声明

二维数组上下文中的 C 指针

java - 如何比较两段文字?

string - 如何在 Windows 上将 OsStr 转换为 &[u8]/Vec<u8> ?