简体   繁体   English

带结构数组的Malloc

[英]Malloc with a struct array

I am trying to create a dynamically allocated array of type struct, but am running into some issues. 我正在尝试创建一个动态分配的struct类型的数组,但是遇到了一些问题。 Namely the program crashes at the fclose command at the end of the program. 即程序在程序末尾的fclose命令中崩溃。 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. 在注释掉这一行之后,程序在返回0时崩溃,因此我假设文件I / O编码存在一些问题,并且内存分配也存在问题。 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! 'ipArray'是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! 编辑:我错了,希望我没有混淆任何人!

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

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