Jump to content

Help with Access Violation Writing Location C++

FaiL___

I am getting an access violation writing location error when trying to run this line of code:

directions[keyCount] = direction;

This is where I am declaring the array:

int directions[1600] = { 0 };

And this is the rest of the portion of code I am trying to run:

for (int count = 0; count < 4; count++)
		{
			if (room.direction[count] == DIRECTION_LOCKED)
			{
				key = findKey(count);
				if (key == 0)
				{
					unlocked_direction = count;
					correctKey = bruteForce(count);

					keys[keyCount] = correctKey;
					floorNum = room.name[0];
					floorNumInt = floorNum - '0';
					floors[keyCount] = floorNumInt;
					roomNum[0] = room.name[2];
					roomNum[1] = room.name[3];
					roomNumInt = atoi(roomNum);
					rooms[keyCount] = roomNumInt;
					directions[keyCount] = direction;
				}
				else
				{
					sentOption(count + 6, key);
				}
			}
			if (room.direction[count - 1] == DIRECTION_OPEN)
			{
				if (count == unlocked_direction)
				{
					keyCount++;
				}
			}
		}

Thanks in advance, quote me if you have any questions.

There are 10 types of people in the world. Those that understand binary and those that don't.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, FaiL___ said:

-snip-

Where did you initially declare the value of keyCount and what is the value?

Intel® Core™ i7-12700 | GIGABYTE B660 AORUS MASTER DDR4 | Gigabyte Radeon™ RX 6650 XT Gaming OC | 32GB Corsair Vengeance® RGB Pro SL DDR4 | Samsung 990 Pro 1TB | WD Green 1.5TB | Windows 11 Pro | NZXT H510 Flow White
Sony MDR-V250 | GNT-500 | Logitech G610 Orion Brown | Logitech G402 | Samsung C27JG5 | ASUS ProArt PA238QR
iPhone 12 Mini (iOS 17.2.1) | iPhone XR (iOS 17.2.1) | iPad Mini (iOS 9.3.5) | KZ AZ09 Pro x KZ ZSN Pro X | Sennheiser HD450bt
Intel® Core™ i7-1265U | Kioxia KBG50ZNV512G | 16GB DDR4 | Windows 11 Enterprise | HP EliteBook 650 G9
Intel® Core™ i5-8520U | WD Blue M.2 250GB | 1TB Seagate FireCuda | 16GB DDR4 | Windows 11 Home | ASUS Vivobook 15 
Intel® Core™ i7-3520M | GT 630M | 16 GB Corsair Vengeance® DDR3 |
Samsung 850 EVO 250GB | macOS Catalina | Lenovo IdeaPad P580

Link to comment
Share on other sites

Link to post
Share on other sites

What environment are you running this code on?

 

Are you trying to declare an array with a variable assigned to its dimension?  Generally, you can't do that.

Link to comment
Share on other sites

Link to post
Share on other sites

@FaiL___ There's not enough code to tell but the most likely scenario is 'keyCount' being out of range.

You can try adding a assert that checks if keyCount is in the range [ 0, 1600 )

Link to comment
Share on other sites

Link to post
Share on other sites

Just add "Public" to the start of wherever it's declared. Trust me xD it's that simple

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, AJTerminator said:

Just add "Public" to the start of wherever it's declared. Trust me xD it's that simple

Care to elaborate what a access modifier, that is used to perform compile time access checking, has to do with a runtime error ?

Link to comment
Share on other sites

Link to post
Share on other sites

Have you tried it? Also, it's not at compile time, it's just based on the access rights of the code, so if it's private, then it can only be accessed by that script and nothing else, but sometimes it messes up and denies all access rights. Just add to where the array is declared and make it public. Also, what compiler are you using? I'm assuming Visual Studio?

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, AJTerminator said:

Have you tried it? Also, it's not at compile time, it's just based on the access rights of the code, so if it's private, then it can only be accessed by that script and nothing else, but sometimes it messes up and denies all access rights. Just add to where the array is declared and make it public. Also, what compiler are you using? I'm assuming Visual Studio?

In C++, access is evaluated at compile time, perhaps you're confusing with some other language?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×