Firstly, decide whether your program actually needs to be C and not C++. It's much safer and generally easier to use a std::vector<check_t> than a manually allocated region, as std::vector does automatic memory management for you (it makes it hard to memory leak) and it makes it hard to index the vector outside of bounds, which would cause a potentially exploitable memory corruption vulnerability in your program.
If for some reason you must do this without the standard template, next decide whether you can't use new[] in C++. This will simplify your code and be more clear what you're doing:
void main()
{
check_t* myArray = new check_t[count];
}
Finally, if you're absolutely positively 100% sure you can't use std::vector or new[], ask yourself if you can do it via calloc:
void main()
{
check_t* myArray = calloc(nElements, sizeof(check_t));
}
But if, heaven forbid, you're doing some assignment that explicitly states that you must use malloc on pain of death that you ever use any of the multiple better alternatives that have so kindly been made available to you, this is how you do it:
#include <a_safe_int_library.h>
check_t* CreateAndInitializeCheckTArray
(
/* IN */ size_t nElements
)
{
size_t cbSize;
check_t* array = NULL;
size_t i;
// avoid an exploitable integer/heap overflow first:
if(SafeMultiply(&cbSize, sizeof(check_t), nElements) == OVERFLOW)
return NULL;
array = (check_t*)malloc(cbSize);
if(array == NULL)
return NULL;
// clear all of the elements:
memset(array, 0, cbSize);
// initialize the array:
for(i = 0; i < nElements; i++)
{
check_init(&array[i]);
}
return array;
}
check_t **enqueued;
is how you'd declare a pointer to a 2D array ofcheck_t
. Is that what you want? – Fiddling Bits Dec 9 '13 at 0:46*enqueued
andenqueued[0]
mean the same thing. – Fiddling Bits Dec 9 '13 at 1:11