简体   繁体   中英

Malloc with a struct array

I am trying to create a dynamically allocated array of type struct, but am running into some issues. Namely the program crashes at the fclose command at the end of the program. After commenting this line out, the program crashes on return 0, so I am assuming there are some issues with my file I/O coding, and also an issue with my allocation of memory. My goal is to fill the values that are included in the struct host with values from a text file. Code below:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include <assert.h>
#include <stdlib.h>

typedef struct host_struct {
    int x, y, z, w;
    char os[8];
} host;


int main(void)
{
    host *ipArray = NULL;
    int numOfIps = 0;
    char tempFileCharVal = 'a';
    char fileString[8];
    int test;
    int i;
    int j;
    int k;
FILE *hostFile;

    hostFile = fopen("hosts.txt", "r");

    while (fscanf(hostFile, "%c", &tempFileCharVal) != EOF)      //Nested loops to find number of ips, relies on there being a new line at the end of each IP address.
    {
        if (tempFileCharVal == '\n')
        {
            numOfIps++;
        }
    }

    ipArray = malloc(numOfIps * sizeof(*ipArray));                    //Allocates memory to array of ip addresses based on number of IP addresses and size of type host.


    fclose(hostFile);                                   //Reset hostFile to beginning of file.
    hostFile = fopen("hosts.txt", "r");


    for (i = 0; i < (numOfIps); i++)                  //Iterate through IPs, values within IPs, and values within hostFile to assign values to ipArray.
    {
        for (j = 0; j < 5; j++)
        {
            for (k = 0; fscanf(hostFile, "%c", &tempFileCharVal) != '.'; k++)  //?? doesn't end loop if tempFileCharVal = '.'
            {
                if ((tempFileCharVal == ' ' || tempFileCharVal == '\n' || tempFileCharVal == '.') & j != 0) //?? Had to add this if statement to compensate for strange loop behavior.
                {
                    break;
                }
                if ((j == 0) & tempFileCharVal == '\n')
                {
                    fscanf(hostFile, "%c", &tempFileCharVal);
                }
                if ((j == 0) & (tempFileCharVal == '.' || tempFileCharVal == EOF))
                {
                    break;
                }
                fileString[k] = tempFileCharVal;
            }
            fileString[k] = '\0';
            switch (j)
            {
            case 0:
                ipArray[i].x = atoi(fileString);
                break;
            case 1:
                ipArray[i].y = atoi(fileString);
                break;
            case 2:
                ipArray[i].z = atoi(fileString);
                break;
            case 3:
                ipArray[i].w = atoi(fileString);
                break;
            case 4:
                strcpy(ipArray[i].os, fileString);
                ipArray[i].os[k] = '\0';
                break;
            }

        }
    }
    //fclose(hostFile);
    free(ipArray);
    return(0);

}

You have to be careful in this line:

 ipArray = malloc(numOfIps * sizeof(*ipArray)); 

'ipArray' is of type Pointer! You need considerably more memory though. The correct yay to do it would be to write:

 ipArray = malloc(numOfIps * sizeof(struct host_struct)); 

EDIT: I now see this has been commented before, seems I'm late to the party, sorry! ;-)

EDIT: I was wrong, hope I have not confused anyone!

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