Exercise 3-26 (Set bytes in arrays to a specified value)
Chapter_3 Exercise_3-25 | Exercise_3-27 |
Exercise 3-26 TCP1, p. 229
Exercise 3-26. Define an array of int. Take the starting address of that array and use static_cast to convert it into a void*. Write a function that takes a void*, a number (indicating a number of bytes), and a value (indicating the value to which each byte should be set) as arguments. The function should set each byte in the specified range to the specified value. Try out the function on your array of int.
CONTENTS: arrays.c Arrays.cpp
arrays.c download
#include <stdio.h> // for printf(), putchar()
#include <math.h> // for pow()
#define SIZE 10
// we assume sizeof(char) = 1 byte
void set(char* array, int bytes, int value); // initialize or set array
void print(void* array, int size, int type); // print array
// type: sizeof(data type)
int main()
{
char charArray[SIZE];
// automatically convert char* (charArray) to void* in function call:
print(charArray, SIZE, sizeof(char)); // print uninitialized array
set(charArray, SIZE, 65); // initialize array (65 is ASCII for `A')
print(charArray, SIZE, sizeof(char)); // print initialized array
short shArray[SIZE];
// automatically convert short* (shArray) to void* in function call:
print(shArray, SIZE, sizeof(short)); // print uninitialized array
char* cp = (char*)shArray; // &shArray[0]
/*
// Alternative conversion:
void* p = (void*)shArray; // &shArray[0]
char* cp = (char*)p;
*/
set(cp, SIZE*sizeof(short), 0); // initialize array to 0
print(shArray, SIZE, sizeof(short)); // print initialized array
set(cp, SIZE*sizeof(short), 1); // set each byte to 00000001
int i = 1 + pow(2, 8);
printf("1 + pow(2, 8) = %d\n", i);
print(shArray, SIZE, sizeof(short)); // print initialized array
set(cp, SIZE*sizeof(short), 0xff); // set array to 1...1
print(shArray, SIZE, sizeof(short)); // print array
int intArray[SIZE/2];
// automatically convert int* (intArray) to void* in function call:
print(intArray, SIZE/2, sizeof(int)); // print uninitialized array
cp = (char*)intArray; // &intArray[0]
set(cp, (SIZE/2)*sizeof(int), 0); // initialize array to 0
print(intArray, SIZE/2, sizeof(int)); // print initialized array
set(cp, (SIZE/2)*sizeof(int), 1); // set each byte to 00000001
i = 1 + pow(2, 8) + pow(2, 16) + pow(2, 24);
printf("1 + pow(2, 8) + pow(2, 16) + pow(2, 24) = %d\n", i);
print(intArray, SIZE/2, sizeof(int)); // print initialized array
set(cp, (SIZE/2)*sizeof(int), 0xff); // set array to 1...1
print(intArray, SIZE/2, sizeof(int)); // print array
return 0;
}
void set(char* array, int bytes, int value) // initialize or set array
{ // set each byte of the contiguous array to `value'
for (int i = 0; i < bytes; i++)
{array[i] = value;}
}
void print(void* array, int size, int type) // print array
{ // type: sizeof(data type)
int i;
char* charArray;
short* shArray;
int* intArray;
switch(type)
{
case sizeof(char):
charArray = (char*)array;
for (i = 0; i < size; i++)
{printf("%c, ", charArray[i]);}
break;
case sizeof(short):
shArray = (short*)array;
for (i = 0; i < size; i++)
{printf("%d, ", shArray[i]);}
break;
case sizeof(int):
intArray = (int*)array;
for (i = 0; i < size; i++)
{printf("%d, ", intArray[i]);}
break;
default:
printf("Not implemented");
break;
}
putchar('\n');
}
/*
gcc arrays.c -o arrays -lm // link math library
./arrays
, , @, Q, �, p, 6, , , b, // garbage
A, A, A, A, A, A, A, A, A, A,
640, 0, 0, 0, -1, 0, 1, 0, 20496, -26493, // garbage
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1 + pow(2, 8) = 257
257, 257, 257, 257, 257, 257, 257, 257, 257, 257,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2017608424, 32552, -298109536, 22041, 0, // garbage
0, 0, 0, 0, 0,
1 + pow(2, 8) + pow(2, 16) + pow(2, 24) = 16843009
16843009, 16843009, 16843009, 16843009, 16843009,
-1, -1, -1, -1, -1,
*/
Note:
After setting each byte to 1 (00000001),
shArray[0] = 0000000100000001 = 1+2^8 = 257,
intArray[0] = 00000001000000010000000100000001 =
1+2^8+2^16+2^24 = 16843009.
Arrays.cpp download
#include <iostream>
#include <cmath> // for pow()
using std::cout;
using std::endl;
#define SIZE 10
// we assume sizeof(char) = 1 byte
void set(char* array, int bytes, int value); // initialize array
template <typename T> // generics
void print(T* array, int size); // print array
int main()
{
char charArray[SIZE];
print<char>(charArray, SIZE); // print uninitialized array
set(charArray, SIZE, 65); // initialize array (65 is ASCII for `A')
print<char>(charArray, SIZE); // print initialized array
short shArray[SIZE];
print<short>(shArray, SIZE); // print uninitialized array
void* p = static_cast<void*>(shArray); // &shArray[0]
char* cp = static_cast<char*>(p);
set(cp, SIZE*sizeof(short), 0); // initialize array to 0
print<short>(shArray, SIZE); // print initialized array
set(cp, SIZE*sizeof(short), 1); // set each byte to 00000001
int i = 1 + pow(2, 8);
cout << "1 + pow(2, 8) = " << i << endl;
print<short>(shArray, SIZE); // print array
set(cp, SIZE*sizeof(short), 0XFF); // set array to 1...1
print<short>(shArray, SIZE); // print array
int intArray[SIZE/2];
print<int>(intArray, SIZE/2); // print uninitialized array
p = static_cast<void*>(intArray); // &intArray[0]
cp = static_cast<char*>(p);
set(cp, (SIZE/2)*sizeof(int), 0); // initialize array to 0
print<int>(intArray, SIZE/2); // print initialized array
set(cp, (SIZE/2)*sizeof(int), 1); // set each byte to 00000001
i = 1 + pow(2, 8) + pow(2, 16) + pow(2, 24);
cout << "1 + pow(2, 8) + pow(2, 16) + pow(2, 24) = " << i << endl;
print<int>(intArray, SIZE/2); // print array
set(cp, (SIZE/2)*sizeof(int), 0XFF); // set array to 1...1
print<int>(intArray, SIZE/2); // print array
return 0;
}
void set(char* array, int bytes, int value) // initialize array
{ // set each byte of the contiguous array to `value'
for (int i = 0; i < bytes; i++)
{array[i] = value;}
}
template <typename T>
void print(T* array, int size) // print array
{
for (int i = 0; i < size; i++)
{cout << array[i] << ", ";}
cout << endl;
}
/*
g++ Arrays.cpp -o Arrays
./Arrays
, , @, Q, �, p, 6, , , b, // garbage
A, A, A, A, A, A, A, A, A, A,
640, 0, 0, 0, -1, 0, 1, 0, 20496, -26493, // garbage
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1 + pow(2, 8) = 257
257, 257, 257, 257, 257, 257, 257, 257, 257, 257,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
2017608424, 32552, -298109536, 22041, 0, // garbage
0, 0, 0, 0, 0,
1 + pow(2, 8) + pow(2, 16) + pow(2, 24) = 16843009
16843009, 16843009, 16843009, 16843009, 16843009,
-1, -1, -1, -1, -1,
*/
Notes: See Generics_in_C++ on GeeksForGeeks.
Using static_cast,
we need a two-steps conversion:
void* p = static_cast<void*>(shArray); // &shArray[0]
char* cp = static_cast<char*>(p);
compared to the C style conversion used in arrays.c:
char* cp = (char*)shArray; // &shArray[0]
Alternatively, we can use a
reinterpret_cast:
char* cp = reinterpret_cast<char*>(shArray); // &shArray[0]
Chapter_3 Exercise_3-25 | BACK_TO_TOP | Exercise_3-27 |
Comments
Post a Comment