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 9165009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:48:58+00:00 2026-06-17T14:48:58+00:00

I have written a straightforward C code that uses an engine to run two

  • 0

I have written a straightforward C code that uses an engine to run two different algorithms depending on user input. It uses function pointers to the algorithm methods and objects. There is a nasty memory bug somewhere that I can not track down, so maybe I am allocating memory in the wrong way. What is going wrong?

Below is (the relevant parts of) a minimal working example of the code.

main.c

#include "engine.h"

int main()
{
  char *id = "one";

  Engine_t eng;

  Engine_init(&eng);
  Engine_select_algorithm(eng, id);
  Engine_run(eng);
}

engine.h

typedef struct _Engine *Engine_t;

engine.c

#include "engine.h"
#include "algorithm_one.h"
#include "algorithm_two.h"

typedef struct _Engine
{ 
  void *p_algorithm;

  void (*init)(Engine_t);
  void (*run)(Engine_t);

} Engine;

void Engine_init(Engine_t *eng)
{
  *eng = malloc(sizeof(Engine));

  (*eng)->p_algorithm = NULL;
}

void Engine_select_algorithm(Engine_t eng, char *id)
{
  if ( strcmp(id, "one") == 0 )
    {
      eng->init = Algorithm_one_init;
      eng->run  = Algorithm_one_run;
    }
  else if ( strcmp(id, "two") == 0 )
    {
      eng->init = Algorithm_two_init;
      eng->run  = Algorithm_two_run;
    }
  else
    {
      printf("Unknown engine %s.\n", id); exit(0);
    }

  eng->init(eng);
}

void Engine_run(Engine_t eng)
{
  eng->run(eng);
}

void Engine_set_algorithm(Engine_t eng, void *p)
{
  eng->p_algorithm = p;
}

void Engine_get_algorithm(Engine_t eng, void *p)
{
  p = eng->p_algorithm;
}

algorithm_one.h

typedef struct _A_one *A_one_t;

algorithm_one.c

#include "engine.h"
#include "algorithm_one.h"

typedef struct _A_one
{ 
  float value;
} A_one;

void Algorithm_one_init(Engine_t eng)
{
  A_one_t aone;
  aone = malloc(sizeof(A_one));
  aone->value = 13.0;

  //int var = 10;

  Engine_set_algorithm(eng, &aone);
}

void Algorithm_one_run(Engine_t eng)
{  
  A_one_t aone;
  Engine_get_algorithm(eng, &aone);

  printf("I am running algorithm one with value %f.\n", aone->value);
  // The code for algorithm one goes here.
}

The code for algorithm_two.h and algorithm_two.c are identical to the algorithm one files.

There must be a memory bug involved, because the code runs as given, but if I uncomment the

//int var = 10;

line in algoritm_one.c the code crashes with a segmentation fault.

  • 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-06-17T14:49:00+00:00Added an answer on June 17, 2026 at 2:49 pm

    You pass the wrong thing to Engine_set_algorithm. You are passing the address of a local variable rather than the address of the algorithm. You need to write:

    Engine_set_algorithm(eng, aone);
    

    And also Engine_get_algorithm is wrong. You are passed a pointer by value and modify that pointer. So the caller cannot see that modification. You need it to be:

    void Engine_get_algorithm(Engine_t eng, void **p)
    {
      *p = eng->p_algorithm;
    }
    

    I think your code would be easier if you defined a type to represent an algorithm. That type would be just a void*, but it would make the code much easier to read. What’s more, I would make Engine_get_algorithm return the algorithm.

    algorithm Engine_get_algorithm(Engine_t eng)
    {
        return eng->p_algorithm;
    }
    
    void Engine_set_algorithm(Engine_t eng, algorithm alg)
    {
        eng->p_algorithm = alg;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a function that sorts a big scale of data. To test
I have written a function that extract the domain from hostname. e.g. www.domain.com ->
We have an app written in ASP.NET MVC 3, that uses @Html.AntiForgeryToken() . We
I have an application written in Java that performs some straightforward but time consuming
I have a Windows Workflow application that uses classes I've written for COM automation.
I have written this code inside a servlet to delete certain records from three
I have written this code in JavaScript and works perfectly fine when I include
I have written a very simple CTE expression that retrieves a list of all
I have legacy win.forms application written in pretty straightforward approach where forms communicate with
Fairly straightforward question. I have a map that I wish to initialize by calling

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.