简体   繁体   English

使用字符串和Malloc / Realloc

[英]Using Strings and Malloc/Realloc

I'll be honest, I'm a complete novice at c. 老实说,我是c的新手。 Thus, things like malloc and realloc are alien concepts. 因此,诸如malloc和realloc之类的东西是陌生的概念。 I think I have the basics down, but I just can't quite get there 100%. 我认为我的基础知识已经下降,但是我不能完全达到100%。

while (int args = scanf("%s", string)) {
    if (args < 0) break;
    count++;

    if (array == NULL) {
        array = (char *) malloc(strlen(string));

        if (array == NULL) {
            printf("Error allocating memory");
            exit(1);
        }
    } else {
        printf("%s %d\n", string, strlen(string));
        array = (char *) realloc(array, (sizeof(array) + strlen(string) + 1));

        if (array == NULL) {
            printf("Error allocating memory");
            free(array);
            exit(1);
        }

        printf("%lu\n", sizeof(array));
    }

    strcpy(&array[count - 1], string);
}

It's reading from terminal - cat file | 从终端读取-cat文件| ./program and is just a bunch of words of arbitrary length. ./program,只是一堆任意长度的单词。 I'm trying to get them all into an array (array). 我正在尝试将它们全部放入一个数组(数组)。

Edit: I should mentino that I'm apparently trying to access memory I didn't allocated: malloc: *** error for object 0x7fe9e04039a0: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug Segmentation fault: 11 编辑:我应该mentino我显然试图访问未分配的内存: malloc: *** error for object 0x7fe9e04039a0: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug Segmentation fault: 11 malloc: *** error for object 0x7fe9e04039a0: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug Segmentation fault: 11

Looks like you don't understand what pointers, strings and char*s are in C. For example, here is some description. 似乎您不了解C中的指针,字符串和char * s。例如, 是一些描述。

Here are main problems: 主要问题如下:

  1. char* is not a string type. char *不是字符串类型。 It's pointer to a place in memory, where string data lies char-by-char and terminates with char '\\0' (null terminator) 它指向内存中某个位置的指针,其中字符串数据逐个字符放置,并以char'\\ 0'结尾(空终止符)
  2. Thus, strcpy just copies a bunch of chars from one place (string variable) to another. 因此,strcpy只是将一堆字符从一个地方(字符串变量)复制到另一个地方。 In your case, it copies them to array, starting with element count-1. 在您的情况下,它将它们复制到数组(从元素count-1开始)。 So, if you read a string longer than 1 char, you lost the data. 因此,如果您读取的字符串长于1个字符,则会丢失数据。 What you probably want to do is sum lengths of all preceding strings and write starting with this place. 您可能想要做的是所有前面的字符串的总和,然后从该位置开始写。
  3. The remaining problem is consequence: you don't allocate space for null terminator during the first iteration (which causes strcpy to access non-allocated memory and probably leads to the message you see after program's termination). 剩下的问题是后果:您在第一次迭代期间没有为null终止符分配空间(这会导致strcpy访问未分配的内存,并可能导致在程序终止后看到消息)。

To simplify the process, I ended up going with a char ** array instead of a char * array . 为了简化过程,我最终使用了char ** array而不是char * array For each iteration of my while loop (which, by the way, is now while (scanf("%s", string) > 0) to comply with gcc standards (I had originally compiled with g++)), I realloc using count x sizeof(char *) and then I can array[count - 1] = (char *) malloc(sizeof(string + 1) finally, strcpy(array[count - 1], string) 对于我的while循环的每次迭代(顺便说一句,现在是while (scanf("%s", string) > 0)以符合gcc标准(我最初用g ++编译)),我使用count x重新realloc sizeof(char *) ,然后我可以array[count - 1] = (char *) malloc(sizeof(string + 1)最后, strcpy(array[count - 1], string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM