C版本:
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;
}
编译 gcc -o main main.c stash.c
运行 ./main
C++ 版本
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
}
编译 gcc -o main main.cpp stash.cpp
运行 ./main
学习动态数组要深刻理解头文件中的size;quantity;next;还有尤其是storage变量。storage指向的是动态数组的首地址。。