I'm trying to create a class that constructs and manages a region of memory. The region of memory is an array, where each element has n bytes between them (padding).
Here's a diagram I made: http://postimage.org/image/qsun6ojo/ The diagram demonstrates how the array should look im memory.
Here's my issue: I don't know how to perform the computation to access each element individually with a for loop. Any ideas?
int main( )
{
int SizeofElement( 4 );
int ElementCount( 5 );
int SizeofPadding( 1 );
int *Region( nullptr );
Region = ( int * )malloc( ( ElementCount * SizeofElement ) + ( SizeofPadding + ( ElementCount - 1 ) ) );
if( Region == nullptr )
{
std::cout << "Allocation failed" << std::endl;
return 1;
}
int Block( SizeofElement + SizeofPadding );
for( int i( 0 ); i < ElementCount; i++ )
{
*( Region + ( i * Block ) ) = ( i + 1 );
std::cout << *( Region + ( i * Block ) ) << std::endl;
}
free( Region );
Region = nullptr;
return 0;
}
Note that I had to use std::malloc( ) so I could incorporate padding. Since this is a test code segment, it's open to modification and suggestions. How could I improve this?