2

I'm attempting to allocate memory for each entry of the array and initialize the members part1 and part2 to 0.

#include <iostream>
using namespace std;

class Two {
public:
    int part1;
    int part2;
};


int main() {

    Two * dp[10]; //array of 10 pointers to objects of class Two

    part1 = 0;
    part2 = 0;

    for (int i = 0; i < 10; i++) { 
        dp[i] = 0;                        

    }
    return 0; 
}

Any help is appreciated. I'm new to c++ and I'm trying to understand the basic concepts. Thank you in advance!

14
  • Do you now how to access a class member from an object? Commented Sep 19, 2017 at 19:13
  • 2
    Why not use std::vector instead of this array? Commented Sep 19, 2017 at 19:13
  • 6
    Nothing will give you understanding of basic concepts better than time with a good book. I strongly suggest you get one. Commented Sep 19, 2017 at 19:14
  • 1
    1. I see no dynamic memory allocation here 2. modern C++ relies on standard containers first, smart pointers second, and raw memory allocations a distant last. Commented Sep 19, 2017 at 19:15
  • 2
    There's nothing wrong with teaching the basics. How to new things, how to delete things. A thorough, solid knowledge and understanding of the fundamentals of how objects are managed in dynamic scope is a required skill for every C++ developer. Only once there is good understanding of how to correctly implement arrays, and manual linked list, then you get to move on to std::vector, std::list, et. al., and use your newly-acquired knowledge to write logically correct code. Commented Sep 19, 2017 at 19:19

4 Answers 4

5

Here's a really basic version of that code that uses new to allocate memory:

#include <iostream>

class Two {
public:
  Two() : part1(0), part2(0) { };
  int part1;
  int part2;
};


int main() {
  const size_t count = 10;
  Two *dp = new Two[count];

  // Do stuff?

  for (size_t i = 0; i < count; ++i) {
    std::cout << dp[i].part1 << "/" << dp[i].part2 << std::endl;
  }

  delete[] dp;

  return 0; 
}

Note that for a multitude of reasons this is a bad idea, but if you need to side-step the Standard Library because teacher then this is where you go.

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

1 Comment

I am here "because teacher". Thanks. Why in the world my assigned project won't allow vectors, I'll never know.
1

In c++ for your example is preferable to have a default constructor that initialize your data and a std::vector to hold objects of your custom class. This will save you a lot of memory handling problems.

class Two {
public:
  Two() : part1(0), part2(0) { };
  int part1;
  int part2;
};


int main() {
    std::vector<Two> twoVec(10);
    return 0; 
}

Comments

0

You created 10 pointers to instances of Two , but those pointers are not initalized and doesn't point to any instances.

Here is one way to create those instances, store pointers to them in your array, and then set their part1 member to 1 and the part2 member to 2;

for (int i = 0; i < 10; i++) { 
    dp[i] = new Two();
    dp[i]->part1= 1;              
    dp[i]->part2= 2;              

}

5 Comments

One should not use plain new in modern C++.
@Jodocus That's your subjective opinion, and it is not particularly relevant to the question.
There is nothing wrong with using new in modern C++.
There is nothing wrong with using new in modern C++ if used correctly, which is harder than one might think, when considering exception safety. Since C++14, there is nothing that can justify the use of raw owning pointers or the use of plain new for allocation.
Just like goto you will find times and places where new is exactly the right tool. They may not be common. They may raise eyebrows. They may require some Here Be Dragons comments. But they are out there, lurking in the shadows and ready to strike.
-2

You need to allocate objects of the type class Two pointed to by the elements of the array. Otherwise the program will have undefined behavior.

You can do it either using standard algorithm std::generate like this

#include <iterator>
#include <algorithm>

//...

Two * dp[10]; //array of 10 pointers to objects of class Two

std::generate( std::begin( dp ), std::end( dp ), 
               [] { return new Two { 0, 0 };} );    

Or you can use the range based for loop. For example

Two * dp[10]; //array of 10 pointers to objects of class Two

for ( Two * &p : dp ) p = new Two { 0, 0 }; 

Here is a demonstrative program

#include <iostream>

using namespace std;

class Two {
public:
    int part1;
    int part2;
};


int main() 
{

    Two * dp[10]; //array of 10 pointers to objects of class Two

    for ( Two * &p : dp ) p = new Two { 0, 0 }; 

    // processing of the array

    for ( Two * &p : dp ) 
    {
        delete p;
        p = nullptr;
    }       

    return 0; 
}

1 Comment

@AliciaSabatino No, they are enough.:) The class has two data members has not it?

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.