1

I'm learning Visual C++ using MFC and I need to create a dynamic int array without worry about memory location. The array size will be increasing during the run time.

int myArray[5]; // I want to change this as a dynamic array
int counter = 0;
int currentValue;
... more Code

void CScribbleView::OnLButtonUp(UINT, CPoint point) 
{
   myArray[counter] = currentValue;
   counter++;
   currentValue = 0;
... more Code
}
5
  • 1
    There are many ways. There are collection classes provided that you can use and you can also dynamically create an array of any size using new[] and delete[]. See this tutorial. But I suggest using an existing array collection or std::vector. Commented Mar 5, 2017 at 11:18
  • Yes, std:vector is definitely a better alternative to CArray. Commented Mar 5, 2017 at 12:24
  • 3
    @MichaelWalz: The code in question is from the Scribble sample program. This program implements serialization support. When doing MFC serialization, CArray is usually a better choice, as it comes with serialization support built in. Commented Mar 5, 2017 at 20:09
  • @IInspectable all right, I wasn't aware. Commented Mar 6, 2017 at 7:41
  • 1
    @IInspectable: Yes, you are right. It's a sample Scribble program. CArray is the better solution to make a dynamic array, because I'm using MFC. Commented Mar 6, 2017 at 8:06

1 Answer 1

3

I think what you are looking for is CArray Class, the changes in your code will be something like:

CArray<int, int> myArray;
int currentValue;
... more Code

void CScribbleView::OnLButtonUp(UINT, CPoint point) 
{
   myArray.Add(currentValue);
   currentValue = 0;

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

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.