2

I am defining a structer for my class. Basicly i want take an input and write it to member variable(array).

class DynArray
{

    int m_size;
    int* m_p_array;

public:
    DynArray(){
        std::cout << "Please enter size\n";
        std::cin >> m_size;
        m_p_array[m_size];
        for (int i{ 0 }; i < m_size; i += 1) {
            std::cout << "Please enter element " << i << "\n";
            std::cin >> m_p_array[i];   //"Exception thrown" message shows in this line
        }
    }
}

VS2019 shows me as a warning code : C26495 Variable 'DynArray::m_p_array' is uninitialized. Always initialize a member variable

I just searched like 1 hour but didn't figgure out the what is my mistake. Is pointer i used for "m_p_array" problem? Thanks in advance.

2
  • Use a memory allocator for your array - new/delete or some other safer method. Also, rule of 3 or 5 or others apply as this is in the constructor. Commented Jun 9, 2020 at 18:29
  • 1
    What do you think m_p_array[m_size]; is doing? Commented Jun 9, 2020 at 18:32

3 Answers 3

3

This is not how you allocate memory for the array:

m_p_array[m_size];

You need to do this:

m_p_array = new int[m_size];

later you need to free the memory in your destructor:

delete[] m_p_array;

but consider using std::vector instead because it is better.

Sign up to request clarification or add additional context in comments.

4 Comments

A brief note on what m_p_array[m_size]; actually did and how it contributed to the access violation would be educational for the questioner.
@user4581301 it didn't. std::cin >> m_p_array[i]; did. The title clearly states "Write access violation"
It did. There is absolutely nothing wrong with std::cin >> m_p_array[i];. bhristov has pinpointed where the the bug was. The fact that the bug manifested viable effects a few lines later is irrelevant. I'm just prompting for a better explanation of what went wrong and why.
3

A dynamic array in c++ is allocated with the new keyword like this:

m_p_array = new int[m_size];

Don't forget to delete this memory, usually in the destructor. You should also implement the special member functions correctly. Here's a reference for how to approach this.

Note that:

m_p_array[m_size];

is simply indexing into the array, and doesn't declare or initialize anything.

Since you've never initialized m_p_array, you are accessing memory out of bounds, which is undefined behavior.

2 Comments

but std::vector is much much better
@pm100 of course, but OP is writing a class called DynArray, so I assume they are implementing std::vector.
0

didn't figgure out the what is my mistake

The mistake is there in the warning message:

Variable 'DynArray::m_p_array' is uninitialized. Always initialize a member variable

Here:

std::cin >> m_p_array[i];

You indirect through the uninitialised pointer, and attempt to assign the sibling of pointed object, which results in undefined behaviour.

You can solve this by initialising the member variables. You can do this either using a default member initialiser, or by using the member initialisation list.

Basicly i want take an input and write it to member variable(array).

Actually, the member variable is not an array. It is a pointer. Perhaps this is the source of your problem. Maybe you should use an array member instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.