Dynamic Array code of C and C ++ versions

Source: Internet
Author: User

C version:

Vim stash. h

 

# Ifndef stash_h
# Define stash_h

Typedef struct stashtag {
Int size;/* size of each space */
Int quantity;/* Number of storage spaces */
Int next;/* Next empty space */
/* Dynamically allocated array of bytes :*/
Unsigned char * storage;
} Stash;

Void initialize (stash * s, int size );
Void cleanup (stash * s );
Int add (stash * s, void * element );
Void * fetch (stash * s, int index );
Int count (stash * s );
Void inflate (stash * s, int increase );

# Endif // stash_h

 

 

Vim stash. c

 

/* Error testing macros :*/
# Include <assert. h>
/* Dynamic memory allocation functions :*/
# Include <stdlib. h>
# Include <string. h>/* memcpy ()*/
# Include <stdio. h>

# Include "stash. H"

# Define step 10

Void initialize (stash * s, int size ){
S-> size = size;
S-> quantity = 0;
S-> storage = 0;
S-> next = 0;
}

Void cleanup (stash * s ){
If (S-> storage ){
Puts ("freeing storage ");
Free (S-> storage );
}
}

Int add (stash * s, void * element ){
/* Enough space left? */
If (S-> next> = s-> quantity)
Inflate (S, step );
/* Copy element into storage,
Starting at next empty space :*/
Memcpy (& (S-> storage [S-> next * s-> size]),
Element, S-> size );
S-> next ++;
Return (S-> next-1);/* Index Number */
}

Void * fetch (stash * s, int index ){
If (index> = s-> next | index <0)
Return 0;/* not out of bounds? */
/* Produce pointer to desired element :*/
// Return & (S-> storage [Index * s-> size]);
Return (S-> storage + Index * s-> size );
}

Int count (stash * s ){
/* Number of elements in stash */
Return S-> next;
}

Void inflate (stash * s, int increase ){
Void * V =
Realloc (S-> storage,
(S-> quantity + increase)
* S-> size );
/* Was it successful? */
Assert (v );
S-> storage = V;
S-> quantity + = increase;
}

 

 

 

Vim main. c

 

# Include "stash. H"
# Include <stdio. h>

Int main ()
{
Stash intstash;
Int I;

Initialize (& intstash, sizeof (INT ));
For (I = 0; I <100; I ++ ){
Add (& intstash, & I );
}
For (I = 0; I <count (& intstash); I ++ ){
Printf ("Fetch (& intstash, % d) = % d/N", I,
* (Int *) Fetch (& intstash, I ));
}
Cleanup (& intstash );
Return 0;
}

 

Compile gcc-O main. c stash. c

 

Run./main

 

C ++ version

 

Vim stash ++. h

 

 

# Ifndef stash_h
# Define stash_h

Class stash {
PRIVATE:
Int size;/* size of each space */
Int quantity;/* Number of storage spaces */
Int next;/* Next empty space */
/* Dynamically allocated array of bytes :*/
Unsigned char * storage;
Public:
// Void initialize (stash * s, int size );
// Void cleanup (stash * s );
Stash (INT size );
~ Stash ();
Int add (stash * s, void * element );
Void * fetch (stash * s, int index );
Int count (stash * s );
Void inflate (stash * s, int increase );
};

# Endif // stash_h

 

 

 

 

Vim stash. cpp

/* Error testing macros :*/
# Include <assert. h>
/* Dynamic memory allocation functions :*/
# Include <stdlib. h>
# Include <string. h>/* memcpy ()*/
# Include <stdio. h>

# Include "stash ++. H"

# Define step 10

/* Void Stash: Initialize (stash * s, int size ){
S-> size = size;
S-> quantity = 0;
S-> storage = 0;
S-> next = 0;
}*/
Stash: Stash (INT size)
{
Size = size;
Quantity = 0;
Storage = 0;
Next = 0;
}
/* Void Stash: cleanup (stash * s ){
If (S-> storage ){
Puts ("freeing storage ");
Free (S-> storage );
}
}
*/
Stash ::~ Stash ()
{
If (storage ){
Puts ("freeing storage ");
Free (storage );
}
}
Int Stash: add (stash * s, void * element ){
/* Enough space left? */
If (S-> next> = s-> quantity)
Inflate (S, step );
/* Copy element into storage,
Starting at next empty space :*/
Memcpy (& (S-> storage [S-> next * s-> size]),
Element, S-> size );
S-> next ++;
Return (S-> next-1);/* Index Number */
}

Void * Stash: Fetch (stash * s, int index ){
If (index> = s-> next | index <0)
Return 0;/* not out of bounds? */
/* Produce pointer to desired element :*/
// Return & (S-> storage [Index * s-> size]);
Return (S-> storage + Index * s-> size );
}

Int Stash: Count (stash * s ){
/* Number of elements in stash */
Return S-> next;
}

Void Stash: inflate (stash * s, int increase ){
Void * V =
Realloc (S-> storage,
(S-> quantity + increase)
* S-> size );
/* Was it successful? */
Assert (v );
S-> storage = (unsigned char *) V;
S-> quantity + = increase;
}
 

Vim main. cpp

# Include "stash ++. H"
# Include <stdio. h>

Int main ()
{
Stash intstash (sizeof (INT); // Ding Yi DUI Xiang de Tong Shi Chuan Ti can Shu. Can Shu geile Gou Zao Han Shu
Int I;

// Intstash. initialize (& intstash, sizeof (INT ));
For (I = 0; I <100; I ++ ){
Intstash. Add (& intstash, & I );
}
For (I = 0; I <intstash. Count (& intstash); I ++ ){
Printf ("intstash. Fetch (& intstash, % d) = % d/N", I,
* (Int *) intstash. Fetch (& intstash, I ));
}
// Intstash. Cleanup (& intstash );
Return 0; // Zi Dong Diao Yong Xi Gou Han Shu
}

Compile gcc-O main. cpp stash. cpp

Run./main

 

 

To learn about dynamic arrays, you must have a deep understanding of the size, quantity, and next in the header file, and especially the storage variables. Storage points to the first address of the dynamic array ..

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.