简体   繁体   English

在C中为大程序分配内存

[英]Allocating memory for big program in C

The problem is that the .exe file stops if the input file of at least 20 lines; 问题是,如果输入文件至少20行,则.exe文件将停止; When I exclude the 4 big for loops that contain operations with the following matrices: 当我排除包含以下矩阵的操作的4个大for loops

int rounded_marks[42][100];
int reassessed[22][k];
int averagecounter[14][k];

...program works fine. ...程序运行正常。 Termination of .exe comes when I these big loops are added (I tired to unite them together, but then there will be extremely big loop, and the same .exe termination). 添加这些大循环时就会出现.exe终止(我很累将它们合并在一起,但随后将出现非常大的循环,并且具有相同的.exe终止)。

Total number of lines of each for loop is almost 300 in the code. 代码中每个for loop数几乎为300。

Can you please check whether the memory allocated is enough for doing operations? 您能否检查分配的内存是否足以进行操作?

Should I allocate memory for the matrices? 我应该为矩阵分配内存吗? How should it look like? 看起来怎么样?

Thank You for the attention. 谢谢您的关注。

PS I can't debug, no software available on the PC's (installation prohibited):( PS,我无法调试,PC上没有可用的软件(禁止安装):(

input.dat is multiline csv file. input.dat是多行csv文件。

Here is the .c code: 这是.c代码:

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

int main(void)

    {
    int lines_allocated = 100;
    int max_line_len = 150;
    double c[42][1000]={0};
    int print;

    char **words = (char **)malloc(sizeof(char*)*lines_allocated);
    if (words==NULL)
        {
        fprintf(stderr,"Out of memory (1).\n");
        exit(1);
        }

    FILE *fp = fopen("input.dat", "r");
    if (fp == NULL)
        {
        fprintf(stderr,"Error opening file.\n");
        exit(2);
        }

    int i;
    for (i=0;1;i++)
        {
        int j;

        if (i >= lines_allocated)
            {
            int new_size;

            new_size = lines_allocated*2;
            words = (char **)realloc(words,sizeof(char*)*new_size);
            if (words==NULL)
                {
                fprintf(stderr,"Out of memory.\n");
                exit(3);
                }
            lines_allocated = new_size;
            }
        words[i] = malloc(max_line_len*sizeof(*(words[i])));
        if (words[i]==NULL)
            {
            fprintf(stderr,"Out of memory (3).\n");
            exit(4);
            }
        if (fgets(words[i],max_line_len-1,fp)==NULL)
            break;

        for (j=strlen(words[i])-1;j>=0 && (words[i][j]=='\n' || words[i][j]=='\r');j--)

        words[i][j]='\0';
        }

    int j;
    int k=i; // k is number of lines
    for(j = 0; j < k; j++)
    {

        char *pptr = words[j];
        int l;
        for (l = 0; l < 42; l++)
        {
            char *ptr = strchr(pptr, ',');
            if (ptr) 
            {
                *ptr = 0;
                c[l][j] = atof(pptr);
                pptr = ptr + 1;
            }
            else if (isdigit(*pptr)) 
            {
                c[l][j] = atof(pptr);
            }
        }    
    }

    int rounded_marks[42][100];
    int averagecounter[14][k];
    int reassessed[22][k];
    for (j=0;j<k;j++)
    {
             //doing some complicated operations for 1st time
    }
    for (j=0;j<k;j++)
    {
             //doing some complicated operations for 2nd time
    }
    for (j=0;j<k;j++)
    {
             //doing some complicated operations for 3rd time
    }
    for (j=0;j<k;j++)
    {
             //doing some complicated operations for 4th time
    }
}

Most implementations have a relatively small limit on the size of a stack frame, so you should not try to allocate large arrays as local variables. 大多数实现对堆栈帧的大小都有相对较小的限制,因此您不应尝试将大数组分配为局部变量。 However, static arrays and the heap can be very large. 但是,静态数组和堆可能非常大。 So move your declaration of c out of main and make it a global static variable. 因此,将c的声明移出main并使其成为全局静态变量。

In the case of variables whose sizes are determined at run time, like the arrays dependent on k , you should allocate the on the heap using malloc() . 如果变量的大小在运行时确定,例如依赖于k的数组,则应使用malloc()在堆上分配变量。

I am more of an ObjC kinda guy, but the first loop : 我更像是一个ObjC家伙,但是第一个循环是:

for (i=0;1;i++)

Isn't that going to be running forever (since 1 is always true) and eventually run out of memory because the first loop allocates memory? 那不是会永远运行(因为1始终为true)并最终由于第一个循环分配内存而耗尽内存吗?

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

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