【问题标题】:Finding Total Memory in Bytes in C在 C 中以字节为单位查找总内存
【发布时间】:2020-05-13 02:39:55
【问题描述】:

在我的作业中,我希望 以字节为单位打印动态数组所需的总内存

我必须读取文件并找到所需的字节。 这也不是我的任务主要目的

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

#define N 100 //Change this acording to the size of the file.
#define PATH "list.txt"



int * read_array(char *filename,int *count);


int main(void){
    int *arr,count;
    arr=read_array(PATH,&count);
    int byte_used= count * sizeof(int);
    printf("%d number of byte used\n",byte_used);
    free(arr);
}

int * read_array(char *filename, int *i){
    FILE *fp=fopen(filename, "rb");
    if (fp==NULL){ printf("File does not exist\n"); *i=0 return 0;  }
    int a=0,counter=1;

    int *arr;
    arr=(int*)malloc(counter*N*sizeof(int));

    while(fscanf(fp,"%d,",&arr[a]) == 1){
        a++;
        /* When all allocated memory is filed, do this */
        if (counter*N<=a){
            counter++;
            arr=(int*)realloc(arr,counter*N*sizeof(int));
        }

    }

    *i=a; //returns how many numbers in this file
    return arr;
}

所以我想知道 main 中的第 3 行对预期有效还是有另一种方法可以找到字节的用法? 我也会用链表做同样的事情。所以我们学习动态内存分配。

list.txt 的小例子

107557,55092,62318,70428,176047,80485,54378,196442,189223,30437,60540,192159,33269,76106,45347,157100,146714,148826,117640,101563,76183,37355,182131,177126,194497,183527,194987,38558,33858,32845,125977,63521,119818,152605,188990,76983,129104,6248,16584,166616,130089,18922,24273,156750,101204,196746,188582,120890,74983,112513,129785,77139,173710,29203,113654,65771,190139,133410,130882,120492,50775,137392,165089,105591,147175,57138,61213,112020,180819,90229,89965,90348,113128,61010,141313,6982,59301,136882,16740,35281,120563,10840,112388,72077,136377,26905,171965,101848,112372,107374,153418,113139,37599,122005,53904,101215,86164,38777,122907,16655,115439,153224,25420,193267,115505,89156,156848,137102,31725,19419,25086,105131,144132,40022,143155,99377,31892,831,139379,191996,162653,82880,170260,61363,80250,115883,117688,92847,184991,49799,122085,21224,48320,48359,119315,96627,159768,80137,93466,15103,174821,83863,129759,173513,118821,63486,77311,87648,23289,12885,39730,89500,58910,136438,118109,175974,7664,160314,98305,78389,31732,24061,160832,3729,61192,112101,129654,180326,59920,165289,58643,107106,44683,54488,175689,7988,70030,52949,89169,112724,181046,144363,56838,100020,26208,118902,197054,186915,2010,76672,194157,23433,166280,88101,85856,91119,57961,33077,198816,111023,55898,75583,58333,153127,20589,14342,61971,127062,1377,34588,131515,121206,110805,3851,172936,72173,73173,175725,21785,68748,6056,80365,24673,125787,105702,181725,175734,32495,178036,137669,63675,444,164708,191622,156968,62629,131119,84594,8586,124567,116197,75871,49064,1049,27621,108219,16135,176144,43276,198582,117926,33090,128903,70690,11719,43055,42658,46582,150291,14896,138883,44807,83779,26870,192992,183926,54984,165114,138820,139714,20630,85346,16490,96596,111548,138552,159941,48643,160220,149088,126113,173179,79943,7088,187176,36160,53923,111483,91996,38794,78371,148969,43052,56343,32518,1922,64291,23974,191324,1011,182138,197160,154265,15338,140128,76576,51349,150257,149451,5498,184812,178437,56529,179679,158530,165798,141758,84579,188457,7807,154972,51980,78319,13982,131312,143826,143181,33524,15063,81554,15272,124150,28990,168269,40077,112055,1340,90010,144856,152767,131248,59260,73587,36615,36189,167944,145935,68244,180668,35806,4456,152727,57047,120876,41564,193295,176540,168954,23092,28061,1541,58999,115210,97097,148034,54665,102469,152367,56568,145834,33905,44792,173362,16842,78441,174358,34009,174751,114538,122805,167504,179079,58505,28972,164371,136677,39996,179655,24916,199296,189433,108802,183450,136978,109015,167131,29554,12661,68967,189654,27690,45662,149812,48602,17899,130212,102263,89393,133828,178975,52897,120831,32563,95749,109746,184058,197708,140014,164609,145017,103794,98801,148181,102247,55108,128686,75497,127044,185145,66265,67860,140704,10899,114890,103232,12392,116319,180102,132965,146363,177084,191390,190615,44790,51776,31505,135222,12767,113621,40095,73462,142896,152514,

【问题讨论】:

  • 不确定您的问题到底是什么。你的代码有用吗?作为建议,也许您可​​以使用fseek(...)ftell(...) 和相关函数检查文件的大小?另外,您可能想阅读realloc(...) 函数。
  • @AdrianMole 实际上,我的代码运行良好,而且找到大小也不是本次作业的重点。我想知道,这是以字节为单位的总内存吗?我在第 4 行的主要操作是正确找到以字节为单位的总内存?
  • fscanf 应该测试== 1,而不是!= EOF
  • 几乎所有documentation 都会告诉您sizeof 运算符的作用;您似乎很高兴count 是您的项目数,那么还有什么值得怀疑的呢?也许您应该使用size_t 类型,而不是int 用于byte_usedcount,但这只会对非常大的数据集造成问题。
  • @muhammedoğuz 您必须确定要计算的是分配给数组的大小与您读入的数字实际占用的(可能更小)大小。或者添加一个在返回之前if (a &lt; counter*N) arr=(int*)realloc(arr,a*sizeof(int));,这将丢弃数组末尾未使用的部分。

标签: c arrays dynamic-memory-allocation


【解决方案1】:

代码中的第三行对你来说已经足够了,但是如果你想让你的代码更好,你可以写一些东西;

if( a < counter*N )    arr=(int*)realloc( arr, a*sizeof(int) );

【讨论】:

    【解决方案2】:

    根据问题正文:

    赋值:打印动态数组所需的总内存字节数

    通过该分配,您可以大大简化代码。

    类似:

    int getIntsRequired(char *filename)
    {
        FILE *fp = fopen(filename, "rb");
        if (fp == NULL) return 0;
    
        int tmp;
        int counter = 0;
        while(fscanf(fp, "%d,", &tmp) == 1) ++counter;
        fclose(fp);
        return counter;
    }
    
    size_t getBytesRequired(char *filename)
    {
        return sizeof(int) * getIntsRequired(filename);
    }
    
    int main(void){
        size_t bytes_required = getBytesRequired(YOUR_FILE_NAME);
        printf("%zu number of byte required\n", bytes_required);
        return 0;
    }
    

    【讨论】:

    • 感谢您的回答,但这不是我的作业目的,只是最小的部分之一。尽管我编辑了我的问题,但我在评论部分提到了。
    • @muhammedoğuz 如果您的作业要求您还将整数保存在数组中,您应该在问题中明确说明。
    猜你喜欢
    • 2011-11-30
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多