简体   繁体   中英

Dynamic Memory Allocation

I'm having trouble dynamically allocating memory for an array. I've been debugging for hours, any pointers?

I posted the rest of the code. It is simply supposed to exchange the swap the first row with the second, and the third with the forth. I am getting strange results like:

Enter string: hello

Enter string: how are you

Enter string: i'm good thanks

Enter string: bye

Enter string: bai

Enter string: xx

=========================

how are you

!i'm good thanks

hello

!how are you

bye

!bai

i'm good thanks

!bye

bai

!xx

    int count = 0;
    char *lines[MAX_LINES];
    char *tmp[50]; 


    printf("Enter string: ");
    fgets(tmp, 50, stdin);
    lines[count] = (char *) malloc((strlen(tmp)+1) * sizeof(char));
    strcpy(lines[count], tmp); 

    while(strcmp("xx\n", lines[count])){
            count++;
            printf("Enter string: ");
            fgets(tmp, 50, stdin); 
            lines[count] = (char *) malloc((strlen(tmp)+1)* sizeof(char));

            strcpy(lines[count], tmp); 
    }

void exchange(char * records[])
{
    char * temp;
    temp = records[0];
    records[0] = records[1];
    records[1] = temp;

    temp = records[2];
    records[2] = records[3];
    records[3] = temp; 

}

void printArray(char * inputs[], int row, int col)
{
    int i, j;

    for(i = 0; i < row; i++){
        for(j = 0; j < col; j++){
            printf("%c", inputs[i][j]);
        }

    }
}   

This code seems to work:

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

enum { MAX_LINES = 50 };
static void printArray(char *inputs[], int rows);
static int readLine(char *buffer, size_t buflen);

int main(void)
{
    int count = 0;
    char *lines[MAX_LINES];
    char  tmp[50]; 

    for (count = 0; count < MAX_LINES; count++)
    {
        if (readLine(tmp, sizeof(tmp)) == EOF)
            break;
        lines[count] = (char *) malloc((strlen(tmp)+1)* sizeof(char));
        if (lines[count] == 0)
            break;
        strcpy(lines[count], tmp); 
    }

    putchar('\n');
    printArray(lines, count);

    return(0);
}

static int read_line(char *buffer, size_t buflen)
{
    printf("Enter string: ");
    if (fgets(buffer, buflen, stdin) == 0 || strcmp("xx\n", buffer) == 0)
        return EOF;
    return 0;
}

static void printArray(char *inputs[], int rows)
{
    for (int i = 0; i < rows; i++)
        printf("%d: %s", i, inputs[i]);
}  

Sample run 1 (using EOF):

$ ./rl
Enter string: Abyssinia
Enter string: Wimbledon Common
Enter string: ^D
0: Abyssinia
1: Wimbledon Common
$

Sample run 2 (using ' xx '):

$ ./rl
Enter string: Abyssinia
Enter string: Wimbledon Common
Enter string: Strawberry Lemonade
Enter string: xx

0: Abyssinia
1: Wimbledon Common
2: Strawberry Lemonade
$

What's different? I fixed the type on tmp as noted in a comment. I created a function readLine() to manage the prompt and read and compare with "xx\\n" process to avoid repetition. I avoided using strdup() but do check that malloc() succeeds before using the pointer returned. I ensure that there are not too many lines read in (the for loop). The printArray() code only takes the number of rows because the strings are of varying length. I removed the exchange() function since it was not being used and I couldn't see how it was supposed to be used. The code is complete and compilable (so it is an SSCCE — Short, Self-Contained, Correct Example ).

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