简体   繁体   中英

Using Strings and Malloc/Realloc

I'll be honest, I'm a complete novice at c. Thus, things like malloc and realloc are alien concepts. I think I have the basics down, but I just can't quite get there 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 | ./program and is just a bunch of words of arbitrary length. 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 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.

Here are main problems:

  1. char* is not a string type. It's pointer to a place in memory, where string data lies char-by-char and terminates with char '\\0' (null terminator)
  2. Thus, strcpy just copies a bunch of chars from one place (string variable) to another. In your case, it copies them to array, starting with element count-1. So, if you read a string longer than 1 char, you lost the data. 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).

To simplify the process, I ended up going with a char ** array instead of a 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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