kanlian4095 2017-11-29 12:23 采纳率: 80%
浏览 882
已采纳

C++编程思想 4.1一个袖珍的C库例题

C++编程思想
4.1一个袖珍的C库例题:
//:CLib.h
typedef struct CstashTag {
int size;
int quantity;
int next;
unsigned char* storage;
}Cstash;

void initialize(Cstash* s, int size);
void cleanup(Cstash* s);
int add(Cstash* s, const void* element);
void* fetch(Cstash* s, int index);
int count(Cstash* s);
void inflate(Cstash* s, int increase);

//:CLib.cpp{o}
#include"CLib.h"
#include
#include
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){
if (s->next >= s->quantity)
inflate(s, increment);
int startBytes = s->next * s->size;
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);
}

void* fetch(Cstash* s, int index) {
assert(0 <= index);
if (index >= s->next)
return 0;
return &(s->storage[index * s->size]);
}

int count(Cstash* s) {
return s->next;
}

void inflate(Cstash* s, int increase) {
assert(increase > 0);
int newQuantity = s->quantity + increase;
int newBytes = newQuantity*s->size;
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];
delete ;
s->storage = b;
s->quantity = newQuantity;
}

void cleanup(Cstash* s) {

if (s->storage != 0) {
    cout << "释放存储"<< endl;
    delete []s->storage;
}

}

//:CLibTest.cpp
//{L}CLib.cpp
#include"CLib.h"
#include
#include
#include
#include

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++) //sign
add(&intStash, &i);
for (i = 0; i < count(&intStash); i++) {
cout << "fetch(&intStash)," << i << ") = "
<< (int)fetch(&intStash, i)
<< endl;
}
initialize(&stringStash, sizeof(char)*bufsize);
in.open("CLibTest.cpp");
assert(in);
while (getline(in, line))
add(&stringStash, line.c_str());
i = 0;
while ((cp = (char*)fetch(&stringStash, i++)) != 0)
cout << "fetch(&stringStash)," << i << " ) = "
<< cp << endl;
cleanup(&intStash);
cleanup(&stringStash);
}

直接编译运行 提示 :
0x00000000779D0901 (ntdll.dll) (Project1.exe 中)处有未经处理的异常: 0xC0000005: 读取位置 0x000000C237E24208 时发生访问冲突。
后来发现是 sing 处的问题 那行注释掉就可以了
请问是什么原因

但是运行时在执行 cleanup(&stringStash);的时候又出现了问题 直接卡住了
只有intStash 成功释放了
请问到底是什么原因

小白希望大家多多帮助谢谢
#include<>包含的头文件显示不出来不知道为什么请大家见谅

  • 写回答

3条回答

  • 真相重于对错 2017-11-29 23:00
    关注

    void inflate(Cstash* s, int increase) {
    assert(increase > 0);
    int newQuantity = s->quantity + increase;
    int newBytes = newQuantity*s->size;
    int oldBytes = s->quantity*s->size;
    unsigned char* b = new unsigned char[newBytes];//这里要 new char[size] 不是new char(size)
    for (int i = 0; i < oldBytes; i++)
    b[i] = s->storage[i];
    delete ;
    s->storage = b;
    s->quantity = newQuantity;
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?