c语言运行debug一半卡住了,为啥下面这段代码debug时一切正常,运行的时候就崩掉...

为什么下面这段代码debug时一切正常,运行的时候就崩掉。

麻烦大家帮我检查一下代码。已经努力了很长时间,调试的时候跟着所有变量跑了一遍,没有发现错误。直接运行的时候就会崩掉...  发生这个现象的原因是什么?

驱动程序:

#include"stdafx.h"

#include 

#include 

#include 

#include "JSON.h"

int TestSomeFuncs() {

int score = 10;

// 调用同学们实现的接口

JSON *root = CreateObject();

// 评测1 是否能正确创建Object

if (root->type != JSON_OBJECT) { // 类型不对

score -= 2;

}

AddItemToObject(root, "name", CreateString("Hello World"));

// 评测2 是否能正确AddItemToObject并且读取

JSON *value = GetItemInObject(root, "name");

// 类型不对或者值不对

if (value->type != JSON_STRING || strcmp(value->valuestring, "Hello World")) {

score -= 2;

}

JSON *array = CreateArray();

AddItemToArray(array, CreateBool(0));

AddItemToArray(array, CreateNumber(2.3));

// 评测3 是否能正确AddItemToArray并且读取

JSON *item = GetItemInArray(array, 1);

if (item->type != JSON_NUMBER || item->valuedouble != 2.3) {

score -= 2;

}

AddItemToObject(root, "array", array);

// 现在root的状态

/*

{

"name": "Hello Wrold",

"array": [

false,

2.3

]

}

*/

// 评测4 是否能正确地根据路径读取值

item = GetItemInJSON(root, "/array/0");

if (item->type != JSON_FALSE) {

score -= 2;

}

PrintJSONToFile(root, "test.json");

/*

// 评测5 是否与标准答案文件相同

if (!IsSame("test.json", "test.json.ans")) {

score -= 2;

}*/

return score;

}

int main() {

printf("Score: %d\n", TestSomeFuncs());

getchar();

return 0;

}

头文件:

#ifndef JSON_H

#define JSON_H

/* cJSON Types: */

#define JSON_FALSE 0

#define JSON_TRUE 1

#define JSON_NULL 2

#define JSON_NUMBER 3

#define JSON_STRING 4

#define JSON_ARRAY 5

#define JSON_OBJECT 6

//user-defined

#define LEN sizeof(struct JSON)

/* The cJSON structure: */

typedef struct JSON {

int type;/* The type of the item, as above. */

char *valuestring;/* The item's string, if type==JSON_STRING */

int valueint;/* The item's number, if type==JSON_TRUE||JSON_FALSE */

double valuedouble;/* The item's number, if type==JSON_NUMBER  */

char *key;

JSON *pthead;    //只在数组或对象的索引里出现,代表数组或对象链表的头结点的地址

JSON *ptnext;    //只在数组或对象的具体项里出现,代表下个结点的地址

JSON *ptend;     //只在数组或对象的索引里出现,代表数组或对象链表的尾结点的地址

int size;        //只在数组或对象的索引里出现,代表数组或对象链表中结点的个数

} JSON;

/* Parse & Print */

extern JSON *ParseJSON(const char *value);

extern JSON *ParseJSONFromFile(const char *file_name);

extern void PrintJSON(JSON *item);

extern void PrintJSONToFile(JSON *item, const char *file_name);

/* Create */

extern JSON *CreateNULL(void);

extern JSON *CreateTrue(void);

extern JSON *CreateFalse(void);

extern JSON *CreateBool(int b);

extern JSON *CreateNumber(double num);

extern JSON *CreateString(const char *string);

extern JSON *CreateArray(void);

extern JSON *CreateObject(void);

/* Append */

extern void AddItemToArray(JSON *array, JSON *item);

extern void AddItemToObject(JSON *object, const char *key, JSON *value);

/* Update */

extern void ReplaceItemInArray(JSON *array, int which, JSON *new_item);

extern void ReplaceItemInObject(JSON *object, const char *key, JSON *new_value);

/* Remove/Delete */

extern JSON *DetachItemFromArray(JSON *array, int which);

extern void DeleteItemFromArray(JSON *array, int which);

extern JSON *DetachItemFromObject(JSON *object, const char *key);

extern void DeleteItemFromObject(JSON *object, const char *key);

extern void DeleteJSON(JSON *item);

/* Duplicate */

extern JSON *Duplicate(JSON *item, int recurse);

/* Read */

extern JSON *GetItemInArray(JSON *array, int which);

extern JSON *GetItemInObject(JSON *object, const char *key);

extern JSON *GetItemInJSON(JSON *json, const char *path);

#endif

实现函数的代码(有的函数还没有实现,不过不影响驱动程序里的函数调用):

#include

#include

#include

#include

#include "JSON.h"

#define SETPT JSON *ptJSON=(JSON *)malloc(LEN); /*set pointer (太懒了,不愿意打字orz)*/

static JSON *DeepCopy(JSON *from);

static int IsArray(const JSON *json);

static int IsObject(const JSON *json);

static void CreakAndLink(JSON *front,JSON *prev,JSON *curr);

static JSON *Detach(JSON* front, JSON *detached);

static JSON *ParseA(const char *value);

static JSON *ParseO(const char *value);

static JSON *ParseOthers(const char *value);

static char *StringCopy(const char *item);

static JSON *SetPointer(JSON *ptJSON);

static void PrintA(JSON *item,FILE *to,int flag,int time);

static void PrintO(JSON *item,FILE *to,int flag,int time);

static void PrintJSONTo(JSON *item, FILE *to);

static void PrintSpaceTo(int num,FILE *to);

另外如果大家发现代码的错误或者不好的代码风格也请提出来,帮我纠正一下错误,感激不尽!

------解决思路----------------------

char *StringCopy(const char *item)

{

char *ptchar = (char *)malloc(strlen(item)+1);//看看这句吧

strcpy(ptchar, item);

return ptchar;

}

------解决思路----------------------

崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。

------解决思路----------------------

刚才试了一下,我把LZ代码中的malloc 全部替换成 MALLOC,然后在 JSON.h中加宏定义 #define MALLOC(x) malloc(x + 1),程序就正常了。所以,应该是分配内存不够。从1楼找到的代码看来,sizeof 不能用来数字符串长度。

------解决思路----------------------

数组越界或者内存申请的问题啊

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值