difficulty initializing dynamic array

when I look at the following code in the debugger (looking at the memory window in VS), it appears only a single WORD is being initialized to 0 in the bits element. the variable height is 12, as confirmed in the debugger.

I am trying to get a continuous 12 WORDS (24 bytes) set to zero on every pass through the loop.

EDIT: I've since learned to do it with a () after the new statement. I'm still confused why my initial method only initializes the first WORD of the array

1
2
3
4
5
6
 for (int i = 0; i < 127; i++)
		{
		characters[i].AsciiVal = i;
		characters[i].bits = new WORD[height];
		*characters[i].bits = { 0 };
		}
Last edited on
*characters[i].bits = { 0 };
I don't think that's doing what you think it's doing. That is in fact, as you said, only setting a single value to 0.
The only time you can initialize an array with curly braces is during the declaration/new statement itself.
i.e. characters[i].bits = new WORD[height] { };

That will correctly set all array elements to 0.

PS: I guess you're working on Windows or something, but if you need exact byte lengths, you should be using the <cstdint> header types.
https://en.cppreference.com/w/cpp/header/cstdint

1
2
3
4
int8_t  // 1 byte
int16_t // 2 bytes
int32_t // 4 bytes
int64_t // 8 bytes  

signed integer type with width of exactly 8, 16, 32 and 64 bits respectively
with no padding bits and using 2's complement for negative values
(provided only if the implementation directly supports the type)
(typedef)

Last edited on
Thanks. Makes sense.

Next question. In my destructor, I clean up the same memory as follows:

1
2
3
4
5
for (int i = 0; i < 127; i++)
		{
		delete[] characters[i].bits;
		characters[i].bits = nullptr;
		}


I'm trying to delete every element of this array (which as you can see, as a member of a structure inside another array)

This look right, because I'm getting some sort of unexplained exception thrown by this on around the 65th element's deletion. The array is 128 elements. Visual Studio is just saying some breakpoint was hit. Not one of mine, it must be a breakpoint in a library or .dll or something.
BTW, I am working on Windows. Would it be better form to use those types than the API provided defines? I thought the whole point of using the API defines was kinda to solve the same problem those types you suggested also solve. I should check, but my understanding is that WORD is a guaranteed 2 bytes, no matter what kind of machine you compile for.

I'm just a hobbyist, but would like to do things in a way that pros do when I can.
You're not showing enough code. The 5 lines in your post before last look perfectly legal to me, in isolation.

I'm getting some sort of unexplained exception thrown by this on around the 65th element's deletion. The array is 128 elements.
This is the important bit. It sounds like you're going out of bounds of your characters array.

This is partially a guess, but please show the initialization of your characters array and any time you modify it.
Also, if the array size is 128, why are you only going up to i < 127?
That might be a potential source of the problem.

_____________________________________________

I should check, but my understanding is that WORD is a guaranteed 2 bytes, no matter what kind of machine you compile for
The 2nd part of your statement here doesn't make sense, because WORD is a macro defined in <windows.h>, i.e. it isn't portable C++ code. You can continue to use WORD if you want, just know that it's a Windows-only macro.

WORD is a macro who's substitution is unsigned short. Depending on the system, it's possible for shorts to be 32-bit. I'm just letting you know. This is most likely unrelated to your problem, although I can't be sure of that since I don't know all the code.
Last edited on
Topic archived. No new replies allowed.