@AbstractionAnon
I'll try to minimize my dynamic memory allocation then. I guess it would be a smart idea for me to replace my vectors with arrays that have enough elements to handle any normal amount of values placed in it.
Usually I read all of my arrays at one time (drawing pixels to the screen, etc.), but I suppose there are sometimes when I may just need part of one. I had read somewhere that reading row-by-row was faster than column-by-column, but I guess not.
Well that's good to know about the files. Instead of one read statement into a char array, now I can read the file once for each variable and it will make it easier to work with.
I have been using \n for a bit instead of endl, but when would it be necessary to use cout.flush()?
@Peter87
Do you mean using \n instead of endl when writing a file? I have only used \n when writing files, so I didn't know you could use endl doing that.
@jonnin
I'll have to read over that PDF. Thanks for the link.
I see your point about all of the additional calculations, but if lookup tables have all that extra cost, then why were they ever used? Also, do most modern CPUs have the cos capability?
So as far as allocating and deallocating, it's better to minimize dynamic objects? I'm guessing it would be far better to create something like an array instead of a vector, and to assign enough elements to that array that it would cover any amount of elements your vector would have used, right? In other words, you reserve more memory, but then you don't have to waste time chasing it later? I usually make the mistake of declaring objects, structs and variables in the scope of a function instead of the main function (is this dynamic?). Would it be better to define all of these objects in main at the beginning?
As far as the image reading, I should basically read like [0][0], [0][1], ... [0][n] instead of [0][0], [1][0], ... [n][0], right? I tend to do my image arrays as the following:
1 2 3 4 5
|
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
draw_pixel(pixel[i][j];
}
|
I'll stick with references then. I often see people using pointers when creating an object, or a pointer to that object. See code:
1 2 3 4 5
|
// How I make an object.
MyObject object;
// What others usually do.
MyObject *object;
|
They then pass the pointer, whereas I would pass a reference to my object. What's the difference, and why do I see that pointer format so much? Is their way much better and can I still pass it by reference?
Is it worth me adding multi-threading to my loops, and is it hard to implement? Also, as I understand it each core has 2 threads, correct? What if I have 4 cores and I make my code to use 8 threads (can I use them all?) and then someone who only has 2 cores runs the program? Will it only use 4 threads, will it fail, or something else?
So a grayscale image is faster for searching? Would I create one of those by taking the average of the RGB values and then writing that value to all RGB for that pixel? Also, how can I use the grayscale to search for colors easily?
Sorry, you lost me after the grayscale part. What do you mean that I could create a map of colors? Would that be like a 2D vector where every color is represented in the first element and the next elements are the x,y points where that color can be found in the image?