Creating a Container Application

Chapter 18 - Complete I/O in C++

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

The memset( ) Function
The memset( ) function can be used to initialize a dynamically allocated byte, or bytes, to a specific character. The prototype for memset( ) looks like this:
void *memset(void *dest, int cchar, size_t count);
Once the memset( ) function is called, dest points to count bytes of memory initialized to the character cchar. The following example program demonstrates a dynamic structure declaration:
//
//  memset.cpp
//  C++ program demonstrating the function memset( ),
//  which can initialize dynamically allocated memory.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>
#include <memory.h>

struct keybits {
 unsigned char rshift, lshift,  ctrl,   alt,
               scroll, numlock, caplock, insert;
};
void main(void)
{
 keybits *pstkinitialized;

 pstkinitialized = new keybits;
 memset(pstkinitialized, 0, sizeof(keybits));
}
Because of the memset( ) function, the dynamically allocated structure pointed to by pstkinitialized contains all zeros. The call to the function memset( ) also used the sizeof( ) operator instead of hardwiring the statement to a “magic number.” The use of sizeof( ) allows the algorithm to automatically adjust to the size of any object passed to it. Remember, too, that C++ does not require the struct keyword to precede a structure tag field (keybits) when defining structure variables, as is the case with pstkinitialized.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net