How to avoid this memory leak? - c++

This is my code:
void MIDITest::CreateNoteBlock() {
IMidiMsgExt* midiMessage = new IMidiMsgExt;
midiMessage->MakemidiMessageMsg(57, 100, 0, 0, 0);
queuedNotes.insert(*midiMessage);
midiMessage = new IMidiMsgExt;
midiMessage->MakemidiMessageMsg(60, 100, 0, tickSize * 38, 0);
queuedNotes.insert(*midiMessage);
midiMessage = new IMidiMsgExt;
midiMessage->MakemidiMessageMsg(62, 100, 0, 0, 0);
queuedNotes.insert(*midiMessage);
midiMessage = new IMidiMsgExt;
midiMessage->MakemidiMessageMsg(65, 100, 0, tickSize * 32, 0);
queuedNotes.insert(*midiMessage);
midiMessage = new IMidiMsgExt;
midiMessage->MakemidiMessageMsg(57, 0, tickSize * 111, 0);
queuedNotes.insert(*midiMessage);
midiMessage = new IMidiMsgExt;
midiMessage->MakemidiMessageMsg(60, 0, tickSize * 111, 0);
queuedNotes.insert(*midiMessage);
midiMessage = new IMidiMsgExt;
midiMessage->MakemidiMessageMsg(62, 0, tickSize * 75, 0);
queuedNotes.insert(*midiMessage);
midiMessage = new IMidiMsgExt;
midiMessage->MakemidiMessageMsg(65, 0, tickSize * 105, 0);
queuedNotes.insert(*midiMessage);
}
so at every new operator it will allocate a block of memory.
Should I use free after any insert inside the queuedNotes? Or it will be released after void function return? (i.e. the brackets of CreateNoteBlock).
Or I can "reuse" each time the midiMessage pointer for a new IMidiMsgExt?

The answer is not to use new at all. Create a object with automatic storage duration
IMidiMsgExt midiMessage;
And then you can keep calling MakemidiMessageMsg and insert a copy of the message into the multiset.
midiMessage.MakemidiMessageMsg(57, 100, 0, 0, 0);
queuedNotes.insert(midiMessage);
midiMessage.MakemidiMessageMsg(60, 100, 0, tickSize * 38, 0);
queuedNotes.insert(midiMessage);
//...
Now the multiset has a copy of all of the messages and at the end of the function midiMessage is destroyed and no memory management needs to be done.
If IMidiMsgExt has a constructor that is like MakemidiMessageMsg where you can construct a complete message then you could boild this down even further and use something like
queuedNotes.insert(IMidiMsgExt(57, 100, 0, 0, 0));
queuedNotes.insert(IMidiMsgExt(60, 100, 0, tickSize * 38, 0));
And now we don't even need midiMessage.

Do you come from a Java or C# background? In C++ you don't have to use new to create objects, just declaring them will work fine:
IMidiMsgExt midiMessage;
midiMessage.MakemidiMessageMsg(57, 100, 0, 0, 0);
queuedNotes.insert(midiMessage);
It's either that (which is my recommended solution), or you have to explicitly free the objects:
IMidiMsgExt* midiMessage = new IMidiMsgExt;
midiMessage->MakemidiMessageMsg(57, 100, 0, 0, 0);
queuedNotes.insert(*midiMessage);
delete miniMessage;

It seem redundant to dynamically allocate IMidiMsgExt using new. You can just allocate it directly on the stack (no pointers), and then it will be destroyed when your method returns. I.e.:
void MIDITest::CreateNoteBlock() {
IMidiMsgExt midiMessage();
midiMessage.MakemidiMessageMsg(57, 100, 0, 0, 0);
queuedNotes.insert(midiMessage);
midiMessage = IMidiMsgExt();
midiMessage.MakemidiMessageMsg(60, 100, 0, tickSize * 38, 0);
queuedNotes.insert(midiMessage);
midiMessage = IMidiMsgExt();
midiMessage.MakemidiMessageMsg(62, 100, 0, 0, 0);
queuedNotes.insert(midiMessage);
midiMessage = IMidiMsgExt();
midiMessage.MakemidiMessageMsg(65, 100, 0, tickSize * 32, 0);
queuedNotes.insert(midiMessage);
midiMessage = IMidiMsgExt();
midiMessage.MakemidiMessageMsg(57, 0, tickSize * 111, 0);
queuedNotes.insert(midiMessage);
midiMessage = IMidiMsgExt();
midiMessage.MakemidiMessageMsg(60, 0, tickSize * 111, 0);
queuedNotes.insert(midiMessage);
midiMessage = IMidiMsgExt();
midiMessage.MakemidiMessageMsg(62, 0, tickSize * 75, 0);
queuedNotes.insert(midiMessage);
midiMessage = IMidiMsgExt();
midiMessage.MakemidiMessageMsg(65, 0, tickSize * 105, 0);
queuedNotes.insert(midiMessage);
}

Try to make your API more C++ like.
Use one object on the stack instead of creating a lot of new ones in the heap.
void MIDITest::CreateNoteBlock() {
IMidiMsgExt midiMessage;
midiMessage.MakemidiMessageMsg(57, 100, 0, 0, 0);
queuedNotes.insert(midiMessage);
midiMessage.MakemidiMessageMsg(60, 100, 0, tickSize * 38, 0);
queuedNotes.insert(midiMessage);
// ...
}
Initialize your objects in the constructor. Define an operator = (const IMidiMsgExt&) for IMidiMsgExt.
void MIDITest::CreateNoteBlock() {
IMidiMsgExt midiMessage(57, 100, 0, 0, 0);
queuedNotes.insert(midiMessage);
midiMessage = IMidiMsgExt(60, 100, 0, tickSize * 38, 0);
queuedNotes.insert(midiMessage);
// ...
}
I guess, that insert() expects a const IMidiMsgExt&. So you can directly pass freshly initialized objects:
void MIDITest::CreateNoteBlock() {
queuedNotes.insert({57, 100, 0, 0, 0});
queuedNotes.insert({60, 100, 0, tickSize * 38, 0});
// ...
}
BTW: you should prefer to use e.g. std::queue<> for queuedNotes. Then you'll not use insert(), but push(), or emplace(). The advantage of emplace() is, that it constructs the object in the container instead of first creating it and then copying it to the container:
void MIDITest::CreateNoteBlock() {
queuedNotes.emplace(57, 100, 0, 0, 0);
queuedNotes.emplace(60, 100, 0, tickSize * 38, 0);
// ...
}
Also your typename IMidiMsgExt signals to me, that you are trying to mimic the C# thinking in C++. It is possible, but usually it is not the preferred solution. From your question I don't know enough about your class tree and the underlying requirements to provide a suggestion, but in C++ that usually is a code smell.

Related

SDL - how to render text from an alpha-only bitmap?

I'm trying to render text using SDL. Obviously SDL does not support rendering text by itself, so I went with this approach:
load font file
raster glyphs in the font to a bitmap
pack all bitmaps in a large texture, forming a spritesheet of glyphs
render text as a sequence of glyph-sprites: copy rectangles from the texture to the target
First steps are handled using FreeType library. It can generate bitmaps for many kinds of fonts and provide a lot of extra info about the glyphs. FreeType-generated bitmaps are (by default) alpha channel only. For every glyph I basically get a 2D array of A values in range 0 - 255. For simplicity reasons the MCVE below needs only SDL, I already embedded FreeType-generated bitmap in the source code.
Now, the question is: how should I manage the texture that consists of such bitmaps?
What blending mode should I use?
What modulation should I use?
What should the texture be filled with? FreeType provides alpha channel only, SDL generally wants a texture of RGBA pixels. What values should I use for RGB?
How do I draw text in specific color? I don't want to make a separate texture for each color.
FreeType documentation says: For optimal rendering on a screen the bitmap should be used as an alpha channel in linear blending with gamma correction. SDL blending mode documentation doesn't list anything named linear blending so I used a custom one but I'm not sure if I got it right.
I'm not sure if I got some of SDL calls right as some of them are poorly documented (I already know that locking with empty rectangles crashes on Direct3D), especially how to copy data using SDL_LockTexture.
#include <string>
#include <stdexcept>
#include <SDL.h>
constexpr unsigned char pixels[] = {
0, 0, 0, 0, 0, 0, 0, 30, 33, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 169, 255, 155, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 83, 255, 255, 229, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 189, 233, 255, 255, 60, 0, 0, 0, 0, 0,
0, 0, 0, 0, 33, 254, 83, 250, 255, 148, 0, 0, 0, 0, 0,
0, 0, 0, 0, 129, 227, 2, 181, 255, 232, 3, 0, 0, 0, 0,
0, 0, 0, 2, 224, 138, 0, 94, 255, 255, 66, 0, 0, 0, 0,
0, 0, 0, 68, 255, 48, 0, 15, 248, 255, 153, 0, 0, 0, 0,
0, 0, 0, 166, 213, 0, 0, 0, 175, 255, 235, 4, 0, 0, 0,
0, 0, 16, 247, 122, 0, 0, 0, 88, 255, 255, 71, 0, 0, 0,
0, 0, 105, 255, 192, 171, 171, 171, 182, 255, 255, 159, 0, 0, 0,
0, 0, 203, 215, 123, 123, 123, 123, 123, 196, 255, 239, 6, 0, 0,
0, 44, 255, 108, 0, 0, 0, 0, 0, 75, 255, 255, 77, 0, 0,
0, 142, 252, 22, 0, 0, 0, 0, 0, 5, 238, 255, 164, 0, 0,
5, 234, 184, 0, 0, 0, 0, 0, 0, 0, 156, 255, 242, 8, 0,
81, 255, 95, 0, 0, 0, 0, 0, 0, 0, 68, 255, 255, 86, 0,
179, 249, 14, 0, 0, 0, 0, 0, 0, 0, 3, 245, 255, 195, 0
};
[[noreturn]] inline void throw_error(const char* desc, const char* sdl_err)
{
throw std::runtime_error(std::string(desc) + sdl_err);
}
void update_pixels(
SDL_Texture& texture,
const SDL_Rect& texture_rect,
const unsigned char* src_alpha,
int src_size_x,
int src_size_y)
{
void* pixels;
int pitch;
if (SDL_LockTexture(&texture, &texture_rect, &pixels, &pitch))
throw_error("could not lock texture: ", SDL_GetError());
auto pixel_buffer = reinterpret_cast<unsigned char*>(pixels);
for (int y = 0; y < src_size_y; ++y) {
for (int x = 0; x < src_size_x; ++x) {
// this assumes SDL_PIXELFORMAT_RGBA8888
unsigned char* const rgba = pixel_buffer + x * 4;
unsigned char& r = rgba[0];
unsigned char& g = rgba[1];
unsigned char& b = rgba[2];
unsigned char& a = rgba[3];
r = 0xff;
g = 0xff;
b = 0xff;
a = src_alpha[x];
}
src_alpha += src_size_y;
pixel_buffer += pitch;
}
SDL_UnlockTexture(&texture);
}
int main(int /* argc */, char* /* argv */[])
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
throw_error("could not init SDL: ", SDL_GetError());
SDL_Window* window = SDL_CreateWindow("Hello World",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
1024, 768,
SDL_WINDOW_RESIZABLE);
if (!window)
throw_error("could not create window: ", SDL_GetError());
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer)
throw_error("could not create renderer: ", SDL_GetError());
SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, 512, 512);
if (!texture)
throw_error("could not create texture: ", SDL_GetError());
SDL_SetTextureColorMod(texture, 255, 0, 0);
SDL_Rect src_rect;
src_rect.x = 0;
src_rect.y = 0;
src_rect.w = 15;
src_rect.h = 17;
update_pixels(*texture, src_rect, pixels, src_rect.w, src_rect.h);
/*
* FreeType documentation: For optimal rendering on a screen the bitmap should be used as
* an alpha channel in linear blending with gamma correction.
*
* The blending used is therefore:
* dstRGB = (srcRGB * srcA) + (dstRGB * (1 - srcA))
* dstA = (srcA * 0) + (dstA * 1) = dstA
*/
SDL_BlendMode blend_mode = SDL_ComposeCustomBlendMode(
SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD);
if (SDL_SetTextureBlendMode(texture, blend_mode))
throw_error("could not set texture blending: ", SDL_GetError());
while (true) {
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
SDL_RenderClear(renderer);
SDL_Rect dst_rect;
dst_rect.x = 100;
dst_rect.y = 100;
dst_rect.w = src_rect.w;
dst_rect.h = src_rect.h;
SDL_RenderCopy(renderer, texture, &src_rect, &dst_rect);
SDL_RenderPresent(renderer);
SDL_Delay(16);
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
return 0;
}
break;
case SDL_QUIT:
return 0;
}
}
}
return 0;
}
Expected result: red letter "A" on yellow background.
Actual result: malformed red lines inside black square on yellow background.
I suspect that lines are broken because there is a bug within pointer arithmetics inside update_pixels but I have no idea what's causing the black square.
First of all, part of this stuff is already done in SDL_ttf library. You could use it to rasterise glyphs to surfaces or generate multichar text surface.
Your src_alpha += src_size_y; is incorrect - you copy row by row, but skip by column length, not row length. It should be src_size_x. That results in incorrect offset on each row and only first row of your copied image is correct.
Your colour packing when writing to texture is backwards. See https://wiki.libsdl.org/SDL_PixelFormatEnum#order - Packed component order (high bit -> low bit): SDL_PACKEDORDER_RGBA, meaning R is packed at highest bits while A is at lowest. So, when representing it with unsigned char*, First byte is A and fourth is R:
unsigned char& r = rgba[3];
unsigned char& g = rgba[2];
unsigned char& b = rgba[1];
unsigned char& a = rgba[0];
You don't need custom blending, use SDL_BLENDMODE_BLEND, that is 'standard' "src-alpha, one-minus-src-alpha" formula everyone uses (note that it does not blend dst alpha channel itself, nor uses it to any extent; when blending, we only care about src alpha).
Finally one more approach to this: you could put your glyph luminance value (alpha, whatever it is called, the point is it only have one channel) and put it into every channel. That way you could do additive blending without using alpha at all, don't even need RGBA texture. Glyph colour could still be multiplied with colour mod. SDL_ttf implements just that.

Cairo gradient pattern not using set colors

I have been trying to use the code below to set up a gradient, but it displays a gradient from black to white (which are not the colors, obviously).
cairo_t *cr;
cr = gdk_cairo_create(widget->window);
cairo_pattern_t *pat1;
pat1 = cairo_pattern_create_linear(0.0, 0.0, 50.0, 512);
cairo_pattern_add_color_stop_rgb(pat1, 0, 0, 0, 0);
cairo_pattern_add_color_stop_rgb(pat1, 1, 1, 254, 255);
cairo_pattern_add_color_stop_rgb(pat1, 2, 2, 253, 255);
cairo_pattern_add_color_stop_rgb(pat1, 3, 3, 252, 255);
cairo_pattern_add_color_stop_rgb(pat1, 4, 4, 251, 255);
cairo_pattern_add_color_stop_rgb(pat1, 5, 5, 250, 255);
cairo_pattern_add_color_stop_rgb(pat1, 6, 6, 249, 255);
cairo_pattern_add_color_stop_rgb(pat1, 7, 6, 249, 255);
cairo_pattern_add_color_stop_rgb(pat1, 8, 7, 248, 255);
cairo_pattern_add_color_stop_rgb(pat1, 9, 8, 247, 255);
cairo_rectangle(cr, 0, 0, 50, 512);
cairo_set_source(cr, pat1);
cairo_fill(cr);
cairo_pattern_destroy(pat1);
cairo_destroy(cr);
However, this code displays a gradient from red to purple to blue:
cairo_t *cr;
cr = gdk_cairo_create(widget->window);
cairo_pattern_t *pat1;
pat1 = cairo_pattern_create_linear(0.0, 0.0, 50.0, 512);
cairo_pattern_add_color_stop_rgb(pat1, 0, 256, 0, 0);
cairo_pattern_add_color_stop_rgb(pat1, 1, 0, 0, 256);
cairo_pattern_add_color_stop_rgb(pat1, 2, 0, 256, 256);
cairo_rectangle(cr, 0, 0, 50, 512);
cairo_set_source(cr, pat1);
cairo_fill(cr);
cairo_pattern_destroy(pat1);
cairo_destroy(cr);
Why does the first one display a grayscale, while the second one does not?
The top has non-grayscale colors, so I have no clue why it wouldn't work.
EDIT: An answer explained that values above 1 are clamped so I changed my code to this:
cairo_pattern_add_color_stop_rgb(pat1, 0, 0, 0, 0);
cairo_pattern_add_color_stop_rgb(pat1, 1, (1/256), (254/256), (255/256));
cairo_pattern_add_color_stop_rgb(pat1, 2, (2/256), (253/256), (255/256));
cairo_pattern_add_color_stop_rgb(pat1, 3, (3/256), (252/256), (255/256));
cairo_pattern_add_color_stop_rgb(pat1, 4, (4/256), (251/256), (255/256));
cairo_pattern_add_color_stop_rgb(pat1, 5, (5/256), (250/256), (255/256));
cairo_pattern_add_color_stop_rgb(pat1, 6, (6/256), (249/256), (255/256));
cairo_pattern_add_color_stop_rgb(pat1, 7, (6/256), (249/256), (255/256));
cairo_pattern_add_color_stop_rgb(pat1, 8, (7/256), (248/256), (255/256));
cairo_pattern_add_color_stop_rgb(pat1, 9, (8/256), (247/256), (255/256));
The bar is now completely black.
https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-set-source-rgb
The color and alpha components are floating point numbers in the range 0 to 1. If the values passed in are outside that range, they will be clamped.
The 256s in the one that work are clamped to 1. So you get (1,0,0) to (0,0,1) to (0,1,1).
In the one that doesn't work, the first one remains (0,0,0) and every other stop is clamped to (1,1,1). In short, black to white are the colors used in the first one. So cairo displays black to white.

Accessing Lua global tables with C++

How can I access a global table that already exists in Lua using C++ ?
Below are code which I tried. I tried creating a global variable and try modifying a that local to the local in Lua but things dont seem to work
lua_State *lua_state = luaL_newstate();
luaL_openlibs(lua_state);
// lua_createtable(lua_state, 0, 81);
// for (int i = 1; i <= 81; i++)
// {
// lua_pushnumber(lua_state, i);
// lua_pushnumber(lua_state, grid_[i - 1]);
// lua_settable(lua_state, -3);
// }
//
// lua_setglobal(lua_state, "arg");
// lua_createtable(lua_state, 81, 1);
//
// for (int i = 1; i <= 81; i++)
// {
// lua_pushnumber(lua_state, i);
// lua_pushnumber(lua_state, grid_[i - 1]);
// lua_settable(lua_state, -3);
// }
// lua_setglobal(lua_state, "arg");
luaL_loadfile(lua_state, "main.lua");
lua_call(lua_state, 0, 0);
int t = 2;
/* table is in the stack at index 't' */
lua_pushnil(lua_state); /* first key */
while (lua_next(lua_state, t) != 0) {
/* uses 'key' (at index -2) and 'value' (at index -1) */
printf("%s - %s\n",
lua_typename(lua_state, lua_type(lua_state, -2)),
lua_typename(lua_state, lua_type(lua_state, -1)));
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(lua_state, 1);
}
Lua
problem =
{
{9, 0, 0, 1, 0, 0, 0, 0, 5},
{0, 0, 5, 0, 9, 0, 2, 0, 1},
{8, 0, 0, 0, 4, 0, 0, 0, 0},
{0, 0, 0, 0, 8, 0, 0, 0, 0},
{0, 0, 0, 7, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 2, 6, 0, 0, 9},
{2, 0, 0, 3, 0, 0, 0, 0, 6},
{0, 0, 0, 2, 0, 0, 9, 0, 0},
{0, 0, 1, 9, 0, 4, 5, 7, 0},
}
Update 1
int main()
{
lua_State *lua_state = luaL_newstate();
luaL_openlibs(lua_state);
luaL_loadfile(lua_state, "main.lua");
lua_getglobal(lua_state, "problem");
//lua_pushglobaltable(lua_state); // Get global table
lua_pushnil(lua_state); // put a nil key on stack
while (lua_next(lua_state, -2) != 0) { // key(-1) is replaced by the next key(-1) in table(-2)
std::string name = lua_tostring(lua_state, -2); // Get key(-2) name
std::cout << name << std::endl;
lua_pop(lua_state, 1); // remove value(-1), now key on top at(-1)
}
lua_pop(lua_state, 1); // remove global table(-1)
lua_call(lua_state, 0, 0);
return 0;
}
Lua
problem =
{
{9, 0, 0, 1, 0, 0, 0, 0, 5},
{0, 0, 5, 0, 9, 0, 2, 0, 1},
{8, 0, 0, 0, 4, 0, 0, 0, 0},
{0, 0, 0, 0, 8, 0, 0, 0, 0},
{0, 0, 0, 7, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 2, 6, 0, 0, 9},
{2, 0, 0, 3, 0, 0, 0, 0, 6},
{0, 0, 0, 2, 0, 0, 9, 0, 0},
{0, 0, 1, 9, 0, 4, 5, 7, 0},
}
print("Lua Works")
user_input = io.read();
You don't have any values to iterate on Lua stack.
That int t=2; doesn't reflect anything, and your script doesn't return values to be left on stack.
See PIL book: 25.1 – Table Manipulation for examples on accessing global table.

Reverse an array with preprocessor

I want to use pre-processor to fill some arrays in some ways. I can use preprocessor only for newly declared arrays. However, I need to change the array p which I declared and used before. Time optimization is very important for my purpose.
#define Reverse(x) {x[63], x[62], x[61], x[60], x[59], x[58], x[57], x[56], x[55], x[54], x[53], x[52], x[51], x[50], x[49], x[48], x[47], x[46], x[45], x[44], x[43], x[42], x[41], x[40], x[39], x[38], x[37], x[36], x[35], x[34], x[33], x[32], x[31], x[30], x[29], x[28], x[27], x[26], x[25], x[24], x[23], x[22], x[21], x[20], x[19], x[18], x[17], x[16], x[15], x[14], x[13], x[12], x[11], x[10], x[9], x[8], x[7], x[6], x[5], x[4], x[3], x[2], x[1], x[0] }
int main()
{
int p[64] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int q[64] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
//doThings
p= Reverse(q); // line A - Gives error
int s[64] = Reverse(q); // line B - Works properly
//doThings
}
I got this error:
Error 11 error C3079: an initializer-list cannot be used as the right operand of this assignment operator c:\users\ferda\documents\visual studio 2013\projects\consoleapplication3\consoleapplication3\consoleapplication3.cpp 39‌​3 1 ConsoleApplication3
Built in array type does not allow to assign to it another array or use aggregate initialization on already initialized array, you would have to use memcpy or for loop to update it with new values. Your code will compile if you use std::array instead. It provides operator=:
operator= (implicitly declared) overwrites every element of the
array with the corresponding element of another array (public member
function)
http://coliru.stacked-crooked.com/a/8e664210b7f7f73b
i am not sure if this will work as fast as you expect, gcc will generate lots of mov instructions : https://godbolt.org/g/tzUqC3. I suppose it might be faster to use a for loop which will require less cache memory. As always profile your code.

How to show more than one year on x-axis in Google chart's Annotated Time Line?

I am using Google's Annotated Time Line tool to plot data which spans over two years, for example 2010 and 2011.
The timeline on x-axis only shows entries on 2011. It skips all the values of 2010.
Take following data table for example:
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Mac Client');
data.addColumn('number', 'Win Client');
data.addColumn('number', 'Total');
data.addRows(7)
data.setValue(0, 0, new Date(2010, 12, 16, 11, 0, 0, 0));
data.setValue(0, 1, 0);
data.setValue(0, 2, 1);
data.setValue(0, 3, 1);
data.setValue(1, 0, new Date(2010, 12, 24, 16, 0, 0, 0));
data.setValue(1, 1, 0);
data.setValue(1, 2, 5);
data.setValue(1, 3, 5);
data.setValue(2, 0, new Date(2010, 12, 16, 12, 0, 0, 0));
data.setValue(2, 1, 0);
data.setValue(2, 2, 19);
data.setValue(2, 3, 19);
data.setValue(3, 0, new Date(2011, 3, 30, 2, 0, 0, 0));
data.setValue(3, 1, 0);
data.setValue(3, 2, 17);
data.setValue(3, 3, 17);
data.setValue(4, 0, new Date(2011, 4, 11, 13, 0, 0, 0));
data.setValue(4, 1, 0);
data.setValue(4, 2, 37);
data.setValue(4, 3, 37);
data.setValue(5, 0, new Date(2011, 10, 2, 0, 0, 0, 0));
data.setValue(5, 1, 1);
data.setValue(5, 2, 21);
data.setValue(5, 3, 22);
data.setValue(6, 0, new Date(2011, 4, 19, 2, 0, 0, 0));
data.setValue(6, 1, 0);
data.setValue(6, 2, 6);
data.setValue(6, 3, 6);
The resulting graph starts from 2011, instead of 2010. Google code playground
How can I make it to include data points for 2010 too?
The graph ends at November 02, 2011, although my last data point in October 2, 2011. How can I make the x-axis of graph end at October 30.
The JavaScript Date object's 'month' value is indexed at 0 (i.e.: 0=January, 1=February), so right now, everything is a month off.
Change
new Date(2011, 10, 2, 0, 0, 0, 0));
to
new Date(2011, 9, 2, 0, 0, 0, 0));
across the board and you should get what you want!