Reputation: 45
Basically I have some code that keeps seg faulting. The reason is because the current size of my data structure is getting changed and I don't know how. In the newMap function I set the size of the map to 0 then once I've set the values of the function pointers the size is completely different. I'm not sure what I'm doing wrong here.
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;
}
Sample output
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
Upvotes: 2
Views: 77
Reputation: 75150
On this line:
Map* map = malloc(sizeof(map));
map
is a Map*
, so you are allocating the size of a pointer of memory on the heap, not the size of a Map
. It should be either
Map* map = malloc(sizeof(*map));
or
Map* map = malloc(sizeof(Map));
That is one of the problems. If you fix that and there is more wrong, try to solve it yourself, and if you can't, edit your question with the extra information.
Upvotes: 6