基本上我有一些代码可以保持seg错误。原因是我的数据结构的当前大小正在变化,我不知道如何。在newMap函数中,我将map的大小设置为0,然后一旦我设置了函数指针的值,大小就完全不同了。我不确定我在这里做错了什么。
Map.h
#ifndef MAP_H
#define MAP_H
typedef struct MapS Map;
struct MapS{
void* mapPrivateVars;
void (*add)(Map*, void*, void*);
void* (*getValue)(Map*, void*);
long (*getSize)(Map*);
};
Map* newMap();
#endif
Map.c
#include "Map.h"
#include <stdlib.h>
#include <stdio.h>
void addKey(Map* map, void* key, void* value);
void* getKey(Map* map, void* key);
long sizeMap(Map* map);
#define INITIAL_KEY_VALUE_ARRAY_SIZE 100
typedef struct{
void* key;
void* value;
}KeyValuePair;
typedef struct{
int size;
int maxSize;
KeyValuePair** keyValuePairArray;
}MapPrivate;
Map* newMap(){
Map* map = malloc(sizeof(map));
if(map==NULL){
exit(5);
}
map->add = NULL;
map->getValue = NULL;
map->getSize = NULL;
MapPrivate* mapPrivate = malloc(sizeof(MapPrivate));
if(mapPrivate==NULL){
exit(6);
}
KeyValuePair** kvpa = (KeyValuePair**)malloc(INITIAL_KEY_VALUE_ARRAY_SIZE*sizeof(KeyValuePair*));
mapPrivate->keyValuePairArray = kvpa;
if(mapPrivate->keyValuePairArray==NULL){
exit(7);
}
map->mapPrivateVars = (void*)mapPrivate;
MapPrivate* mpv = (MapPrivate*) map->mapPrivateVars;
mpv->maxSize = INITIAL_KEY_VALUE_ARRAY_SIZE;
mpv->size = 0;
printf("size in constructor after assignment is %d\r\n", mpv->size);
map->add = addKey;
map->getValue = getKey;
map->getSize = sizeMap;
printf("size in constructor before end is %d\r\n", mpv->size);
return map;
}
void addKey(Map* map, void* key, void* value){
MapPrivate* privateVars = (MapPrivate*) map->mapPrivateVars;
if(privateVars->size == privateVars->maxSize){
//TODO: realloc with more space
exit(100);
}
KeyValuePair* kvp = malloc(sizeof(KeyValuePair));
if(kvp==NULL){
exit(8);
}
printf("addKey malloced kvp\r\n");
kvp->key = key;
kvp->value = value;
printf("addKey assigned key and value kvp\r\n");
printf("size is %d\r\n", privateVars->size);
privateVars->keyValuePairArray[privateVars->size] = kvp;
printf("addKey added new kvp to kvparray \r\n");
privateVars->size++;
printf("addKey incremented size kvp\r\n");
}
void* getKey(Map* map, void* key){
MapPrivate* privateVars = (MapPrivate*) map->mapPrivateVars;
int i;
for(i = 0; i < privateVars->size;i++){
if(privateVars->keyValuePairArray[i]->key == key){
return privateVars->keyValuePairArray[i]->value;
}
}
return NULL;
}
long sizeMap(Map* map){
MapPrivate* privateVars = (MapPrivate*) map->mapPrivateVars;
return privateVars->size;
}
MapTest.c
#include "Map.h"
#include <stdio.h>
int main(void){
Map* map = newMap();
char* dude = "dude";
char* awesome = "awesome";
char* meh = "meh";
map->add(map, dude, meh);
map->add(map, awesome, dude);
map->add(map, meh, awesome);
return 0;
}
示例输出
size in constructor after assignment is 0
size in constructor before end is 180657104
addKey malloced kvp
addKey assigned key and value kvp
size is 180657104
Segmentation fault: 11
答案 0 :(得分:6)
在这一行:
Map* map = malloc(sizeof(map));
map
是Map*
,所以你要在堆上分配内存指针的大小,不是 Map
的大小。它应该是
Map* map = malloc(sizeof(*map));
或
Map* map = malloc(sizeof(Map));
这是其中一个问题。如果您解决了这个问题并且有更多错误,请尝试自行解决,如果不能解决,请使用额外信息编辑您的问题。