chilli said we need to know size of the array at the compile

The Partridge Family were neither partridges nor a family. Discuss.
AverageWhale
Posts: 57
Joined: August 13th, 2018, 2:33 pm

chilli said we need to know size of the array at the compile

Post by AverageWhale » August 31st, 2018, 10:51 am

chilli said we need to know size of the array at the compile time and it must be const. so if we did something like this

Code: Select all

const int a = downloadInt();
int array[a];
will it work? i think no. i cant test this because i don't know how to download something from web.

User avatar
cyboryxmen
Posts: 190
Joined: November 14th, 2014, 2:03 am

Re: chilli said we need to know size of the array at the com

Post by cyboryxmen » August 31st, 2018, 12:12 pm

Nope
Zekilk

randomGuy
Posts: 32
Joined: May 31st, 2018, 11:45 am

Re: chilli said we need to know size of the array at the com

Post by randomGuy » August 31st, 2018, 12:58 pm

You can use vectors, chili introduces them later in his tutorials and their size can be changed at run time.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: chilli said we need to know size of the array at the com

Post by albinopapa » August 31st, 2018, 9:36 pm

After all that discussion about const vs constexpr, you still have this quesiton?

I said: const int size = 32; would be considered a constant expression by the compiler, then you asked about the exact same thing, downloading info from the internet is not considered a constant expression because a constant expression is known at compile time, not runtime.

Compile time meaning when you compile your code into the executable file
Run time meaning when you when a user runs your executable file
Executable file meaning the file you double click on to run your program you compiled.

To avoid confusion, use constexpr size_t array_size = 32 instead of const size_t array_size 32, that way you know and the compiler knows that you are creating a constant expression variable.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

AverageWhale
Posts: 57
Joined: August 13th, 2018, 2:33 pm

Re: chilli said we need to know size of the array at the com

Post by AverageWhale » September 1st, 2018, 12:16 pm

i just confirm that it would not work. i was thinking that it would work so. i just confirmed.

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: chilli said we need to know size of the array at the com

Post by Yumtard » September 7th, 2018, 7:19 pm

like mentioned you can use a vector.

or

int* array = new int[DownloadInt()];

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: chilli said we need to know size of the array at the com

Post by albinopapa » September 7th, 2018, 7:46 pm

Yumtard wrote:like mentioned you can use a vector.

or

int* array = new int[DownloadInt()];
or
std::unique_ptr<int[]> values = std::make_unique<int[]>( Download() );

Prefer not to use raw pointers and allocations. Use std::vector or std::unique_ptr instead. This way you reduce or eliminate the chances of leaking memory or accessing memory that has already been released.

The advice to use operator new and operator new[] has got to stop, especially if you aren't going to also add the operator delete and operator delete[] calls to your suggestions.

Code: Select all

int* values = new int[ Download() ];

// use values

// Release values
delete [] values;
The std::vector and std::unique_ptr free memory for you when they are no longer in scope, so it is unnecessary to free them manually, using operator new requires a call to operator delete and a call to operator new[] requires a call to operator delete[], so I feel it is important to put that in examples and suggestions, at least a mention of it.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: chilli said we need to know size of the array at the com

Post by chili » September 8th, 2018, 10:42 am

Yeah new is pretty cancerous in modern C++. I think Yum was just showing what is possible.
I wouldn't even do unique_ptr<int[]>, when vector is so much easier and better.

When I said you need a constexpr to make an array, I am ofc talking about a standard C-array (which by necessity is going to be automatic storage class). There are ways to dynamically allocate a block of memory on the stack, but they are not native (standard) to C++.
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: chilli said we need to know size of the array at the com

Post by albinopapa » September 8th, 2018, 12:13 pm

Lol, yeah, array overload/specialization of unique_ptr is mostly useless, but for a buffer like image data it works well enough since vector seems to be overkill. What I mean is vector is geared for flat arrays with dynamic size properties. A 2D buffer of Color doesn't necessarily fit either case; vector nor unique_ptr, but if you wanted to build a type I would imagine a bare bones raw pointer wrapper such as unique_ptr would be a better suited base to start with than vector. The only downside to using unique_ptr over vector in this situation would be the fact that unique_ptr deletes the copy constructor and copy assignment operator.

This is actually okay though as you'd want to verify that the two 2D arrays were of equal dimensions and color depth and such. So using a unique_ptr forces you to implement your own copy solution whether it be a constructor or assignment or a clone function to avoid accidental pass by value situations.

That being said, I suppose there are ways to have the desired 2D array effect:

Code: Select all

	std::vector<std::vector<Color>> pixels( height, std::vector<Color>(width) ); and
	auto pPixels = std::make_unique<std::unique_ptr<Color[]>[]>( height );
	for( auto y = pPixels.get(), end = pPixels.get() + height; y != end; ++y )
	{
		*y = std::make_unique<Color[]>( width );
	}
	
	for( size_t y = 0; y < height; ++y )
	{
		for( size_t x = 0; x < width; ++x )
		{
			pixels[y][x] = Colors::Blue;
			pPixels[y][x] = Colors::Black;
		}
	}
Neither is pretty, but the latter is definitely less readable.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: chilli said we need to know size of the array at the com

Post by Yumtard » September 8th, 2018, 6:28 pm

I was just showing that what he's trying to do is possible if you allocate on the heap. ofc you can also do so using smart pointers.

I thought using delete after new was implied

what i should've added is that vector is the way to go

Post Reply