0

because I am new in C, I am not sure how to ask it, but here is my Code:

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

#define ARRAY_SIZE 500

int main(int argc, char *argv[]) {

    for (int j=0; j<ARRAY_SIZE; ++j) {
        printf("Memory Size: %d\n", j);
        int bytes = (1024*1024);
        char *data;
        data = (char *) malloc(bytes);
        for(int i=0;i<bytes;i++){
            data[i] = (char) rand();
        }
    }
    //Free all Char*data that I have declared inside the for loop here
    return 0;

}

So I need to free my data variables that I have allocated inside the for loop. How is it possible? I am testing some portion of my memory blocks. So I am running it because I wanna see how far it goes. So the above code gets me to that point. Now I am trying to run a loop below threshold point so that I can assure, the memory that I am working with is good and can sustain. To do so, I need to clear the memory that I have created inside the loop.

Thanks in advance

  • You might like to review [Do I cast the result of malloc?](https://stackoverflow.com/q/605845/2410359) – chux - Reinstate Monica Jan 09 '21 at 03:23
  • 1
    What OS are you targeting? If it's a *nix or Windows OS, then your code may not ever detect bad RAM, unless you get lucky. To test RAM, you'll need access to the physical memory of the device, and that requires the use of non-portable OS API's. – jwdonahue Jan 09 '21 at 04:00
  • I am working on Linux... SO the issue is, it can not un-tar a file above 40MB. I am pretty sure its a memory issue. But to be sure, I am running this test. And my partition is mapped to RAM – Subbir Rahman Jan 09 '21 at 05:14
  • Compile your code with [GCC](http://gcc.gnu.org/) invoked as `gcc -Wall -Wextra -g`, fix all the warnings, then use [valgrind](http://valgrind.org/) – Basile Starynkevitch Jan 09 '21 at 12:46

2 Answers2

2

I think you'll want an array of pointers and then free those pointers after the loop.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
    
#define ARRAY_SIZE 500
    
int main(int argc, char *argv[]) {
    char * data[ARRAY_SIZE] = {0};
    for (int j=0; j<ARRAY_SIZE; ++j) {
        printf("Memory Size: %d\n", j);
        int bytes = (1024*1024);
        data[j] = (char *) malloc(bytes);
        for(int i=0;i<bytes;i++){
            data[j][i] = (char) rand();
        }
    }
    for (int j=0; j<ARRAY_SIZE; ++j) {
        free(data[j]);
    }
    return 0;
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
mydisplayname
  • 336
  • 1
  • 5
  • Actually it didn't worked. because data is representing bytes. Hence I got these errors.memtest_dynamic.c: In function 'main': memtest_dynamic.c:20:25: warning: assignment makes pointer from integer without a cast [-Wint-conversion] data[i] = (char) rand(); ^ memtest_dynamic.c:20:25: warning: iteration 420u invokes undefined behavior [-Waggressive-loop-optimizations] data[i] = (char) rand(); ^ memtest_dynamic.c:19:13: note: containing loop for(int i=0;i – Subbir Rahman Jan 09 '21 at 03:30
  • My bad, I've updated the example. I incorrectly had `data[j] = (char) rand();` when I should have had `data[j][i] = (char) rand();` my incorrect code was blowing away the pointer :( – mydisplayname Jan 09 '21 at 04:06
0

Since you assigned the return value of malloc to data which is defined inside of the loop, that memory is lost at the end of each iteration of the loop.

You need to add a call to free at the bottom of the loop.

for (int j=0; j<ARRAY_SIZE; ++j) {
    printf("Memory Size: %d\n", j);
    int bytes = (1024*1024);
    char *data;
    data = (char *) malloc(bytes);
    for(int i=0;i<bytes;i++){
        data[i] = (char) rand();
    }
    free(data);
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • I need to clear the data variable outside of the first loop. Otherwise if I free it inside, then everytime the same memory block will be used. I need to to do it right before the reurn 0 line. – Subbir Rahman Jan 09 '21 at 03:12
  • @SubbirRahman You can't because the variable no longer exists. – dbush Jan 09 '21 at 03:13
  • I know. Any suggestion how I can write this code? Basically I want to use up to 500MB ram to test if my RAM is ok or not – Subbir Rahman Jan 09 '21 at 03:13
  • Do you perhaps want an array of pointers? Then you can assign the allocated memory to a member of an array defined outside the loop, then later use another loop to free the array members. – dbush Jan 09 '21 at 03:15
  • Actually it did not solved my problem. How can I use the pointer to the allocated memory? – Subbir Rahman Jan 09 '21 at 03:31