[ThinkingInC ++] 23. A pocket C library, thinkinginc23

Source: Internet
Author: User
Tags array example

[ThinkingInC ++] 23. A pocket C library, thinkinginc23



/*** Function: a pocket-sized C-database * Time: August 17, 2014 08:05:26 * Author: cutter_point * // This header file is similar to a C library # ifndef CLIB_H_INCLUDED # define clib_h_1_dedtypedef struct CStashTag {int size; // the size of each small space int quantity; // used to indicate the number of bytes to be allocated. The total space size is int next; // The number of bytes already stored in the data // first, we do not know the type size to be allocated, therefore, the smallest unit is used as the space size unsigned char * storage;} CStash; void initialize (CStash * s, int size); // initialize the space size void cleanup (CStash * s ); // reclaim memory int add (CStash * s, const void * element); // add element void * fetch (CStash * s, int index ); int count (CStash * s); void inflate (CStash * s, int increase); # endif // clib_h_encoded DED


/*** Function: defines the implementation of a pocket C library. * Time: 08:05:55, January 1, August 17, 2014 * Author: cutter_point */# include "CLib. h "# include <iostream >#include <cassert> using namespace std; const int increment = 100; void initialize (CStash * s, int sz) {s-> size = sz; s-> quantity = 0; s-> storage = 0; s-> next = 0;} int add (CStash * s, const void * element) // void unknown type {if (s-> next> = s-> quantity) // The next empty space required is larger than the space of some types, so the inflate (s, increment); // copy the element into storage // Space start int startBytes = s-> next * s-> size; // knows where the space without data is unsigned char * e = (unsigned char *) element; for (int I = 0; I <s-> size; ++ I) s-> storage [startBytes + I] = e [I]; s-> next ++; return (s-> next-1); // Location index} void * fetch (CStash * s, int index) // {assert (0 <= index ); // The index location should be no less than 0 if (index> = s-> next) return 0; return & (s-> storage [index * s-> size]);} int count (CStash * s) {return s-> next;} void inflate (CStash * s, in T increase) // increase the memory space {assert (increase> 0); // the size of the space to be increased must be greater than 0 int newQuantity = s-> quantity + increase; // new space size: int newBytes = newQuantity * s-> size; // The new number of bytes, the memory size can be int oldBytes = s-> quantity * s-> size; unsigned char * B = new unsigned char [newBytes]; for (int I = 0; I <oldBytes; ++ I) B [I] = s-> storage [I]; // copy the original data to the new memory partition delete [] (s-> storage); // reclaim the old space s-> storage = B; // assign the header pointer of the new space to s-> storage s-> quantity = newQuan Tity;} void cleanup (CStash * s) {if (s-> storage! = 0) {cout <"freeing storage" <endl; delete [] s-> storage ;}}



/*** Function: defines the implementation of a pocket C library, using * Time: 08:06:38, January 1, August 17, 2014 * Author: cutter_point */# include "CLib. h "# include" CLib. cpp "# include <fstream> # include <iostream> # include <string> # include <cassert> using namespace std; int main () {CStash intStash, stringStash; int I; char * cp; ifstream in; string line; const int bufsize = 80; initialize (& intStash, sizeof (int); for (I = 0; I <100; ++ I) add (& intStash, & I); for (I = 0; I <count (& intStash ); ++ I) cout <"fetch (& intStash," <I <") =" <* (int *) fetch (& intStash, I) <endl; cout <"-------------------------------------" <endl; initialize (& stringStash, sizeof (char) * bufsize); in. open ("CLibTest. cpp "); assert (in); while (getline (in, line) add (& stringStash, line. c_str (); // c_str () is a pointer to line characters I = 0; while (cp = (char *) fetch (& stringStash, I ++ ))! = 0) cout <"fetch (& intStash," <I <") =" <cp <endl; cleanup (& intStash ); cleanup (& stringStash); return 0 ;}





A pocket C library to implement an array for storing arbitrary data

/* A pocket C library that stores an array of arbitrary data

Purpose:

1: Implement an extensible array;

2: stores any type of data;

3: familiar with related pointer operations, and understanding that unsigned char * represents the basic storage unit of memory.

4: Understand how C programs implement data types and a set of operations on Data Types

5: Prepare the array vector for future understanding class implementation

6: understand the basic method void * for implementing the model in C *,
*/

# Include <stdio. h>
# Include <malloc. h>
# Include <memory. h>

Void vector (void ** array, unsigned int size, unsigned int num)
{
If (array = NULL)
Return;

/* If there is space between them, release them first */
If (* array! = NULL)
{
Free (* array );
* Array = NULL;
}

If (size = 0 | num = 0)
{
* Array = NULL;
Return;
}

* Array = (void *) malloc (size * num );
Memset (* array, 0, size * num );
}

Void main ()
{
Int * iArray = NULL;
Unsigned char * cArray = NULL;
Int arraySize = 0;

Printf ("Enter the size of the array :");
Scanf ("% d", & arraySize );
Printf ("\ n ");

/* You can dynamically apply for an array example based on the input size */
Vector (void **) & iArray, sizeof (int), arraySize );
Printf ("generated as an array after initialization :");
For (int I = 0; I <arraySize; I ++)
Printf ("% d", iArray [I]);
Printf ("\ n ");
Free (iArray );
IArray = NULL;

/* Unsigned char "*" indicates that the example of the memory basic storage unit is somewhat far-fetched, just a demo */
Vector (void **) & cArray, sizeof (unsigned char), arraySize );
Printf ("cArray [0] address: 0X % X", cArray );
Printf ("\ ncArray [1] address: 0X % X \ n", & cArray [1]);
Printf ("the difference between the two address values is one byte, indicating that the unsigned char type is the basic memory unit \ n ");

Free (cArray );
CArray = NULL;
}... Remaining full text>

A problem in c ++ Programming

Question 1: Enter and print the Matrix. Question 2: enter a string and place all the letters in the string,-
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.