c版本與c++版本的動態數組代碼

來源:互聯網
上載者:User

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指向的是動態數組的首地址。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.