Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:20:20+00:00 2026-05-31T15:20:20+00:00

Basically I have some code that keeps seg faulting. The reason is because the

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:20:20+00:00Added an answer on May 31, 2026 at 3:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that does custom drawing. Basically it is form fill program
Basically I have some code to check a specific directory to see if an
I have inherited some code: Process p = new ProcessBuilder(/bin/chmod, 777, path).start(); p.waitFor(); Basically,
Basically I have some variables that I don't want to preinitialize: originalTime = None
I have some localization problems in my webpage. There are basically two problems (that
I have a nice DataGridView showing what is basically some kind of log data
I have an odd problem. I have a unit test that keeps getting stuck
I have this piece of code that basically checks if one or more files
We have a big winforms C# application, that's basically a frontend for some databases
Basically I have some class objects, each with three properties. Once one class object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.