Thread: Write Access Violation Error - Help Please

  1. #1
    Registered User
    Join Date
    Mar 2016

    Write Access Violation Error - Help Please

    Hello Everyone,

    I hope you are all doing well and thanks in advance for helping me out with the below issue.

    I have the below code which is a cache memory simulation.

    I am getting the following error "Write Access Violation" which seems to stem from this area of my code right where I allocate space and initialize using n* at the bottom (line 51).

    #include <stdio.h>
    #include <stdlib.h>
    /* define structure of dynamic cache and initialize var */
    /* define dynamic array for main memory */
    struct node
        int tag;
        int *block;//dynamic array
    } *cache = NULL;
    /* declare global var's */
    typedef struct node n;
    int *mm = NULL;
    int numLines, blockSize, cacheSize;
    void enterParameters()
        /* declare local var's */
        /* Prompt for main memory size, cache size, block size */
        int mmSize, i;
        printf("\nEnter Parameters\n-------------------------------\n");
        printf("Enter The Size Of Main Memory (Words): ");
        scanf("%d", &mmSize);
        printf("Enter The Cache Size (Words): ");
        scanf("%d", &cacheSize);
        printf("Enter The Block Size (Blocks/Words): ");
        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].block = NULL;
    After staring at it and trying a few different things I cant for life of me figure out how to fix the error.

    Any help would be greatly appreciated, thanks in advance.

  2. #2
    Registered User
    Join Date
    Dec 2017
    You made cache numLines in size but are looping for cacheSize iterations.
        for(i = 0; i < cacheSize; i++)
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation error?
    By rousse09 in forum C++ Programming
    Replies: 2
    Last Post: 12-21-2011, 09:42 AM
  2. Access Violation Error
    By Neurofiend in forum C Programming
    Replies: 3
    Last Post: 08-22-2005, 06:19 PM
  3. Access Violation error
    By thedoofus in forum C Programming
    Replies: 2
    Last Post: 04-30-2005, 11:33 AM
  4. Access Violation Error
    By Scott in forum C++ Programming
    Replies: 6
    Last Post: 07-24-2003, 05:00 AM
  5. Help! Access violation error in C++
    By runlevel007 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2003, 07:56 PM

Tags for this Thread