Trouble with arrays

I'm having some trouble understanding arrays in c++. I'm new to the language and come from a c# background.

I'm trying to take an array, containing objects. These objects have a property called "ShaderId".

Example array: { 1, 2, 3, 1, 3, 3 }
I want to sort that into the following...
{ { 1, 1 }, { 2 }, { 3, 3, 3 } } (Doesn't have to go 1, 2, 3 - So long as all common Id's are grouped).

When I pass in an array of GameObjects, the array within the function only contains the first element?

// MasterRenderer.h
1
2
3
4
5
6
7
8
9
10
11
12
  #pragma once

#include <vector>
#include "GameObject.h"

class MasterRenderer
{

public:
	MasterRenderer();
	void RenderScene(GameObject Objects[]); // Renders the given game objects
};


// MasterRenderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "MasterRenderer.h"
#include <iostream>

MasterRenderer::MasterRenderer()
{
}

void MasterRenderer::RenderScene(GameObject Objects[])
{
	std::vector<std::vector<GameObject>> Batches;

	// Grouping gameObjects into groups based on shader (set shader, draw group).
	int length = sizeof(Objects) / sizeof(Objects[0]);
	for (int i = 0; i < length; i++)
	{
		if (Batches.size() == 0)
		{
			Batches.push_back(std::vector<GameObject>());
			Batches.back().push_back(Objects[i]);
			continue;
		}

		for (int j = 0; j < Batches.size(); j++)
		{
			if (Objects[i].ShaderId == Batches[j].back().ShaderId)
			{
				Batches[j].push_back(Objects[i]);
				continue;
			}

			if (j == Batches.size() - 1)
			{
				Batches.push_back(std::vector<GameObject>());
				Batches.back().push_back(Objects[i]);
				break;
			}
		}
	}

	std::cout << "Batches" << Batches.size() << std::endl;
}


// Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
GameObject obj1(1);
GameObject obj2(2);
GameObject obj3(3);
GameObject obj4(1);
GameObject obj5(3);

GameObject objects[] = {
	obj1,
	obj2,
	obj3,
	obj4,
	obj5
};


MasterRenderer renderer;
renderer.RenderScene(objects);
Last edited on
Arrays are different in C++, they are not objects like in Java or C# that knows its size.
When you pass an array to a function only the address of the first element is passed, so you need to pass the size of the array as well.
1
2
3
4
5
6
7
void MasterRenderer::RenderScene(GameObject Objects[], size_t size)
{
  for (size_t i = 0; i < size; i++)
  {
    // use Objects[i];
  }
}

There are better options than plain C style arrays like std::array or std::vector.
Last edited on
Thanks!

Is there a performance impact of using std::vector (would it be better to use stock arrays, even though they're a bit more confusing?)
To be sure you have to do some profiling.
Have a look at this thread.
https://stackoverflow.com/questions/381621/using-arrays-or-stdvectors-in-c-whats-the-performance-gap#
Is there a performance impact of using std::vector

Very very small.

(would it be better to use stock arrays, even though they're a bit more confusing?)

The short answer is no. Just use a vector.
The longer answer is still no. Just use a vector.
The long long answer is that if you're so pressed for performance that you find yourself actually having to worry about the difference in performance because you've used a vector rather than an array, you must have proven that through some impressive profiling and measurement and you must have proved beyond a shadow of a doubt that there is no other way to get this tiny, tiny, tiny performance improvement that might not even exist depending on exactly what you're doing with it. If you don't have that proof, get it first.

Just use a vector.
Topic archived. No new replies allowed.