Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/C++ access violation reading location - simple solution when encountering the error
Next
Answered
Tyson Evans
  • 8
Tyson Evans
Asked: May 18, 20222022-05-18T20:58:00+00:00 2022-05-18T20:58:00+00:00In: cpp

C++ access violation reading location – simple solution when encountering the error

  • 8

. Advertisement .

..3..

. Advertisement .

..4..

I encountered the following problem in completing my work:

First-chance exception at 0x00b02c76 in Programming Challenge 14.1.exe: 0xC0000005: Access violation reading location 0xcccccd80. Unhandled exception at 0x00b02c76 in Programming Challenge 14.1.exe: 0xC0000005: Access violation reading location 0xcccccd80

Below is the code I ran:

#ifndef NUMBERS_H
 #define NUMBERS_H
 
 #include <string>
 
 using namespace std;
 const int SIZE1 = 18;
 const int SIZE2 = 8;
 
 class Numbers
 {
 private:
  int number;
  string hundred;
  string thousand;
  string * one;
  string * ten;
 
 
 public:
  Numbers(int num)
  {
  number = num;
  hundred = "hundred";
  thousand = "thousand";
  string * one = new string[SIZE1];
  string * ten = new string[SIZE2];
  }
 
  void initializeArray()
  {
  // Intialize array "one"
  one[0] = "zero";
  one[1] = "one";
  one[2] = "two";
  one[3] = "three";
  one[4] = "four";
  one[5] = "five";
  one[6] = "six";
  one[7] = "seven";
  one[8] = "eight";
  one[9] = "nine";
  one[10] = "eleven";
  one[11] = "twelve";
  one[12] = "thirteen";
  one[13] = "fourteen";
  one[14] = "fifteen";
  one[15] = "sixteen";
  one[16] = "seventeen";
  one[17] = "eighteen";
  one[18] = "nineteen";
 
  // Initialize the ten array
 
  ten[0] = "ten";
  ten[1] = "twenty";
  ten[2] = "thirty";
  ten[3] = "forty";
  ten[4] = "fifty";
  ten[5] = "sixty";
  ten[6] = "seventy";
  ten[7] = "eighty";
  ten[8] = "ninety"; 
  }
 
  string determine()
  {
  string name = "";
 
  for (int i = 0; i <= number; i++)
  {
  if (number == i)
  {
  name = one[i];
  }
  }
 
  return name;
  }
 
  ~Numbers()
  {
  delete [] one;
  delete [] ten;
  }
 };
 
 #endif
#include <iostream>
 #include "Numbers.h"
 
 using namespace std;
 
 
 int main()
 {
 
 
  Numbers n(5);
  string name = n.determine();
 
  cout << "The number is " << name << endl;
 
  cin.ignore();
  cin.get();
 
  return 0;
 }

What’s causing it, and how can it be resolved in the “c++ access violation reading location“ in the cpp?

c++ access violation reading location
  • 2 2 Answers
  • 60 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-29T15:03:00+00:00Added an answer on June 29, 2022 at 3:03 pm

    The cause: Two issues exist in your code. You don’t even call “initializeArray().” As a result, the array is empty when you try to access it. Your array’s size is incorrect because you are attempting to add 19 values to an array with a size of 18.

    Solution: Do this way to solve c++ access violation reading location error:

    Within the constructor, call “initializeArray()” just like:

    Numbers(int num)
    {
    number = num;
    hundred = "hundred";
    thousand = "thousand";
    one = new string[SIZE1];
    ten = new string[SIZE2];
    initializeArray();
    }

    Let’s make the larger array size than expected:

    const int SIZE1 = 20;
     const int SIZE2 = 20;

    Also, see your determination (). Why not try the following and instead use a for loop:

    string name = one[number];
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Manon Picot
    2022-05-25T21:11:32+00:00Added an answer on May 25, 2022 at 9:11 pm
    const int SIZE1 = 18;

    The valid array indexes for SIZE1 array are 0-17. The 0 to indexes are generally valid for an array of size.

    std::vector<std::string> is a great option.

    • 12
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.