Search code examples
carraysdynamic-memory-allocation

Finding Total Memory in Bytes in C


In my assignment I am expected that Print the total memory in bytes required for the dynamic array

I have to read the file and find required byte. And also it is not my assignment main purpose

#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;
}

So I am wondering in 3rd line in main is valid for expectations or there is another way to find the usage of byte? Also I will do the same thing with using linked list. So we learning dynamic memory allocation.

little example of 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,

Solution

  • Third line in your code is enough for you, but if you want to make your code better you can something line;

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