Thread: Help figuring out why the programming ends and doesn't continue through function

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

    Help figuring out why the programming ends and doesn't continue through function

    I am still very new to C programming. The line that I posed below is the one that ends the program. I am not sure why, but I want a dynamic array and I thought this was how I was supposed to allocate the size for it.

    Code:
    if (cache[block].block == NULL) {
      cache[block].block = (int *) malloc(blockSize * sizeof(int));
    }

    Code:
    struct node {
      int tag;
      int *block;                   //dynamic array
    } *cache = NULL;
    
    typedef struct node n;
    int *mm = NULL;
    int numLines, blockSize, cacheSize;
    
    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 = (int *) malloc(blockSize * sizeof(int));
      }
      //CASE 2: MISS DUE TO NON MATCHING TAGS
      printf("test3 \n");
      if (cache[block].tag != tag) {
        cache[block].tag = tag;
        //replace block in the cache
    
        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;
        }
      }
    }
    Last edited by Salem; 03-05-2019 at 10:08 PM. Reason: Removed crayola from code, remember - copy as text then paste as text

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,351
    That looks okay, though you could have written the line with malloc as:
    Code:
    cache[block].block = malloc(blockSize * sizeof(cache[block].block[0]));
    so that even if you change the type of the block member, the malloc call remains correct.

    The problem though seems to lie with the if statement's condition: it doesn't look like you actually set cache to point to the first element of an array, so cache[block] in that line results in the dereferencing of a null pointer.
    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

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    5
    Thank you for helping me out. So are you referring to the if statement about the malloc call?

    Should it look something like:

    Code:
    if(cache[block].block[0] == NULL)
    Cause that seems to come up with an error message. Sorry, I am still new to this, I have been using Java and Python mainly and never learned C, and a lot of C doesn't make sense to me.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,351
    Quote Originally Posted by shogunsak
    Should it look something like:
    No, my example is strictly for the line with malloc.

    Where you do assign a value to cache that is not NULL? For example, do you have a line like this:
    Code:
    cache = malloc(sizeof(*cache));
    Why is cache, mm, numLines etc global variables? Global variables make it more difficult for you to reason about your code. With your prior experience in Java and Python you should already know this.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-25-2015, 06:51 PM
  2. why function gets doesn't work in if statement in c programming?
    By Mohamed Shokry in forum C Programming
    Replies: 6
    Last Post: 05-11-2013, 05:47 PM
  3. Function calling before loop ends
    By jae5086 in forum C++ Programming
    Replies: 3
    Last Post: 11-10-2010, 10:15 AM
  4. instance of a class disappearing after function ends
    By combatdave in forum C++ Programming
    Replies: 14
    Last Post: 10-20-2006, 08:59 AM
  5. Socket abruptly closes after function ends
    By Sfpiano in forum Networking/Device Communication
    Replies: 1
    Last Post: 08-08-2005, 04:49 PM

Tags for this Thread