Thread: How to take care of malloc.c:2372 error

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    5

    How to take care of malloc.c:2372 error

    Code:
      if(cache[block].block == NULL){
    
    
      cache[block].block = (int*)malloc(blockSize * sizeof(int));
    
    
      }
    This is the line that is having the error, but I am having a tough time figuring out how to allocate the memory correctly. I am trying to create a dynamic sized cache, based on user input and was told to put global variables in it even though I would have preferred not to. The majority of the code is below. It is a lot I know, I just don't know what I should be looking for.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct node
    {
       int tag;
       int *block; //dynamic array
    } *cache = NULL;
    
    
    typedef struct node n;
    int *mm = NULL;
    int numLines, blockSize, cacheSize;
    
    
    static void option1()
    {
       int mmSize, i, tag;
    
    
       printf("Enter main memory size (words): ");
       scanf("%d", &mmSize);
       printf("Enter cache size (words): ");
       scanf("%d", &cacheSize);
       printf("Enter block size (words/blocks): ");
       scanf("%d", &blockSize);
    
    
       /*allocate space for MM and initialize*/
    
    
       mm = (int *)malloc(mmSize * sizeof(int));
    
    
       for (i = 0; i < mmSize; i++)
       {
           mm[i] = mmSize - 1;
       }
    
    
       numLines = cacheSize / blockSize;
       /*allocate space for cache and initialize*/
       cache = (n *)malloc(numLines * sizeof(n));
    
    
       for (i = 0; i < cacheSize; i++)
       {
           cache[i].tag = -1;
           cache[i].block = NULL;
       }
       tag = ((cacheSize/blockSize) - 1);
       return;
    }
    
    
    static void option2()
    {
       int readOrWrite, mmAddress, writeValue, word, base, set, tag, block;
    
    
       printf("Enter 0 for read or 1 for write: ");
       scanf("%d", &readOrWrite);
    
    
       if (readOrWrite == 1)
       {
    
    
           printf("\nEnter main memory address to write to: ");
           scanf("%d", &mmAddress);
           printf("\nEnter value to write: ");
           scanf("%d", &writeValue);
       }
    
    
       else if (readOrWrite == 0)
       {
           printf("\nEnter main memory address to read from: ");
           scanf("%d", &mmAddress);
       }
       /*calculate tag, line of cache, and word associated w/ MM address*/
       printf("test1 \n");
       tag = mmAddress / cacheSize;
       block = 1;
       set = (mmAddress % cacheSize) / blockSize;
       word = mmAddress % blockSize;
       base = (mmAddress / blockSize) * blockSize;
    
    
       /*3 cases for hit/miss*/
       //CASE 1: MISS DUE TO UNALLOCATED BLOCK
       printf("test2 \n");
       if (cache[block].block == NULL)
       {
           cache[block].block = malloc(blockSize * sizeof(cache[block].block[0]));
       }
       //CASE 2: MISS DUE TO NON MATCHING TAGS
       if (cache[block].tag != tag)
       {
           cache[block].tag = tag;
    
    
           int i;
    
    
           for (i = 0; i < blockSize; i++)
           {
               cache[block].block[i] = mm[base + 1];
           }
    
    
           if (readOrWrite == 0)
           {
               printf("READ MISS!");
           }
    
    
           else if (readOrWrite == 1)
           {
               printf("WRITE MISS!");
               mm[mmAddress] = writeValue;
           }
       }
       //CASE 3: HIT IN THE CACHE, TAGS MATCH
       else if (cache[block].tag == tag)
       {
           if (readOrWrite == 0)
           {
               printf("YOU HAVE A READ HIT!");
               writeValue = cache[block].block[word];
           }
    
    
           else if (readOrWrite == 1)
           {
               printf("YOU HAVE A WRITE HIT!");
               cache[block].block[word] = writeValue;
               mm[mmAddress] = writeValue;
           }
       }
    }
    
    
    static void option4()
    {
       int i;
    
    
       for (i = 0; i < numLines; i++)
       {
           if (cache[i].block != NULL)
           {
               free(cache[i].block);
           }
    
    
           if (cache != NULL)
           {
               free(cache);
           }
       }
    }
    
    
    int main()
    {
       int option = 0;
    
    
       while (option != 4)
       {
    
    
           printf("\nCache memory allocation and mapping: \n");
           printf("------------------------------------\n");
           printf("1) Enter parameters \n");
           printf("2) Access cache \n");
           printf("3) Quit \n\n");
           scanf("%d", &option);
    
    
           switch (option)
           {
    
    
           case 1:
               option1();
               break;
    
    
           case 2:
               option2();
               break;
    
    
    case3:
               option3();
               break;
    
    
           default:
               printf("\nOption not available, please try again. \n");
               break;
           }
       }
       return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Seems OK to me.
    Code:
    $ gcc -Wall -Wextra main.c
    main.c: In function ‘option1’:
    main.c:19:19: warning: variable ‘tag’ set but not used [-Wunused-but-set-variable]
        int mmSize, i, tag;
                       ^
    main.c: In function ‘option2’:
    main.c:59:56: warning: variable ‘set’ set but not used [-Wunused-but-set-variable]
        int readOrWrite, mmAddress, writeValue, word, base, set, tag, block;
                                                            ^
    main.c: At top level:
    main.c:145:13: warning: ‘option4’ defined but not used [-Wunused-function]
     static void option4()
                 ^
    Since you're using M$ visual studio, you need to make sure your files are called prog.c and NOT prog.cpp.
    Otherwise, you're using the C++ compiler to compile C code.

    Then you can remove all those redundant casts on malloc/calloc/realloc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    5
    I’m using visual studio code, and while it does compile through, for some reason it halts when I try using the option 2 function and that’s where the malloc error happens. The error doesn’t show up on my gcc compiler, it just stops the program, but it did show up on an online compiler. I’m just not sure why this would happen. I only have the one program that is a .c so I don’t think it’s using the cpp compiler. I feel like I’m just allocating the memory for the cache wrong but then again I could be wrong about that too since I have no idea how to fix it.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is basically the same as your previous thread, but with more code.

    As I mentioned in your previous thread, you need to allocate memory for cache itself. You did that in the option1 function, but from reading your main function, the option1 function doesn't have to be called before the other functions. When testing, are you always entering option 1 first?

    (What I'd do is to put code at the start of option2 to check that cache is not a null pointer. If it is, inform the user that the cache parameters need to be entered first, then return early from the function.)
    Last edited by laserlight; 03-06-2019 at 01:41 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2019
    Posts
    5
    When run on my gcc compiler, there is no error, but instead the program stops at the same spot where I was having problems before, but after running it on an online compiler I received a malloc.c:2372 error. You asked why I had global variables before, I don't want them and would prefer not to have them, but they were asked to be put in there that way. I am just not sure how to get this to work properly so that the cache is actually created so that I can see if it is a hit or a miss.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read my edits.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    There is no such thing as a "malloc.c:2372 error". It's just telling you the line number in the malloc.c source where the error occurred. It's not very meaningful or useful.

    If you are still having problems you need to say exactly what your input is when you run the program.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Would anyone care to help me
    By errigour in forum C Programming
    Replies: 7
    Last Post: 02-11-2011, 12:48 PM
  2. eye care
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 08-23-2003, 02:12 PM
  3. you all care way too much
    By Overclocked in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 05-12-2002, 06:10 AM
  4. would anyone care if...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-01-2001, 10:54 PM

Tags for this Thread