c版本与c++版本的动态数组代码

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指向的是动态数组的首地址。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值