2d arrays using dynamic memory

Hey, I'm trying to make 2d arrays using dynamic memory in c++.

I have been looking up ways to do this but a lot of them seem pretty complicated (and disputed).

I am using a current g++ compiler for this.

I was hoping I could just do
cmdList = new char[5][21]
but I am pretty sure I need to use pointers for the compiler to be able to do it.

I need to do this in 2 seperate constructors, a default with a preset amount, and what you see below that will match it to [num_of_cmds][21].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//	ROBOT CONSTRUCTOR with 2 paramers
Robot::Robot(int num_of_cmds, const char *nm) {
	//	Load Robot name
	size = (nm == NULL) ? 0 : strlen(nm);

	try {
		name = new char[size + 1];
	}
	catch(std::bad_alloc) {
		cout << "Error allocating " << size + 1 << " bytes of memory\n";
		name = NULL;
	}
	
	if (name) {
		strcpy (name, nm);
	}
	
	//	Allocate memory for command array
	//	cmdList = new char[num_of_cmds][21] <-- Doesn't work

};


And for the sake of context, the relevent class:
1
2
3
4
5
6
7
8
9
10
11
12
//	ROBOT CLASS
class Robot {
	private:
		char *name;
		char *cmdList;
		int size;
	public:
		Robot( );
		Robot(int num_of_cmds, const char *nm);
		~Robot( );
	
};


Any help is appreciated, this topic has been stumping me for a bit now
You can save yourself a million headaches by using actual strings instead of char arrays. Then you don't need to have 2D arrays, either... just have a 1D array of strings.

Or better yet, a vector of strings:

1
2
3
4
5
6
std::vector< std::string > cmdList;

cmdList.resize(10);  // I want 10 strings in this array
cmdList[0] = "First string";
cmdList[1] = "Second string";
//... 


What's even better is that vector and string internally handle the cleanup, so you don't have to use new/delete.
Wow, that is so much simpler.

Will this allocate the memory needed with
cmdList.resize(10)

In my constructors I am only supposed to allocate enough memory for the commands, I have another function (I have already written) that will initialize the strings with actual data.
Yes, resize() will allocate all the necessary memory.

Of course if you are using std::string, do not use strcmp, strcpy, etc functions, as those are only to be used with char arrays. With strings you don't need them.
Now my program is functioning :)
I haven't used vector at all before this but I think it is definetly something I need to be looking into.
Thank you
I would try learning dynamic arrays as soon as possible if your vectors get large, because they get enormously inefficient, its not actually a limitless array, when it resizes it, it creates a new array transfers all the members up to the smallest size of the 2 arrays and uses the new array, so imagine transfering over 100 values, it gets bad when you get that high
vector is not inefficient when used properly.

it's true that you want to keep resizing to a minimum... or at least if you anticipate future growth, you should reserve() however much you need.

But if you do that, then vector performs very well.
closed account (o1vk4iN6)
This is no problem, you can use reserve() to make the vector big enough to not reallocate.
Topic archived. No new replies allowed.