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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:59:51+00:00 2026-05-24T13:59:51+00:00

I’ve been playing with C today, and something I never had the chance to

  • 0

I’ve been playing with C today, and something I never had the chance to play with, that is use a struct with pointers to functions…well all went good, until I started to get some strange bug, when I was cleaning the whole thing (BTW: I was compiling in x86_64 arch, in a Mac)
Looking and looking I figured out that is the memory alignment, in the node_vtable struct.

In i386, it works fine..no issues whatsoever. However, as I said in x86_64, it doesn’t work.

/* NOT WORKING */
struct node_vtable {
    void    (*add_node)     (linked_list *, node *);
    node *  (*create_node)  (linked_list *, float, int, int );
    void    (*print)        (linked_list *llist);

};

/* WORKING */
struct node_vtable {
    void    (*print)        (linked_list *llist);
    void    (*add_node)     (linked_list *, node *);
    node *  (*create_node)  (linked_list *, float, int, int );  

};

Now, I fixed this moving the pointer node * (*create_node) (linked_list *, float, int, int ); to the end of the struct, as that one has a size of 24 bytes, which is the largest in that struct. However, I really think that there is for sure a more elegant solution, and also I am looking for a clear explanation. Hence, if someone can give a hint or some explanation, that would be great 😉 ….as my brain now is really stuck 🙂

The whole code:

#include <stdio.h>
#include <stdlib.h>

struct node {
    int     id;
    int     var;                  
    float   f_var;
    struct  node *next;
};
typedef struct node node;
typedef struct linked_list linked_list;

/* Structs */
struct node_vtable {
    void    (*add_node)     (linked_list *, node *);
    node *  (*create_node)  (linked_list *, float, int, int );
    void    (*print)        (linked_list *llist);

};

struct linked_list {
    /************************/
    node *head;
    node *tail;
    /************************/
    node *data;
    /* VTable to Methods*/
    struct node_vtable *method;
};


/*Prototypes*/
linked_list *constructor_linked_list();

void print(linked_list *llist);
void add_node(linked_list *this, node *node);
node *create_node(linked_list *this, float f_var, int var, int _id);


/***************/
linked_list *constructor_linked_list() {
    printf("calling constructor_linked_list...\n");
    linked_list *this = (linked_list *)malloc(sizeof(linked_list));

    this->head = NULL;
    this->tail = NULL;
    this->method = NULL;
    this->method = (struct node_vtable *)malloc(sizeof(struct node_vtable *));

    this->method->print         = &print;
    this->method->add_node      = &add_node;
    this->method->create_node   = &create_node;

    return this;
}

void print(linked_list *llist) {
    printf("calling print ...\n");
    node *iter = llist->head;
    while (iter){
        printf("\tnode %d\n", iter->id);
        iter = iter->next;
    }
}

void add_node(linked_list *this, node *node) {
    printf("calling add_node_...\n");
    if (this->head == NULL) {
        node->next = NULL;
        this->head = node;
        this->tail = node;   
    } else {
        node->next = NULL;
        this->tail->next = node;
        this->tail = node; 
    }
}

node *create_node(linked_list *this, float f_var, int var, int _id) {
    printf("calling create_node ...%d\n",(int)sizeof(struct node_vtable));
    node *ret_node = (node *)malloc(sizeof(struct node));  
    ret_node->var = var;   
    ret_node->id = _id;
    ret_node->f_var = f_var; 
    ret_node->next = NULL;

    return ret_node;
}

int main(int argc, char *argv[]) {  

    linked_list *obj = constructor_linked_list();
    int i;
    for (i = 0; i < 10; i++) {
        obj->method->add_node(obj, create_node(obj, 5.0, 3, i));
    }
    obj->method->print(obj);
    return EXIT_SUCCESS;
}

Cheers

  • 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-24T13:59:51+00:00Added an answer on May 24, 2026 at 1:59 pm

    Alignment shouldn’t matter at all, so it definitely has one or more bugs.

    This line:

     this->method = (struct node_vtable *)malloc(sizeof(struct node_vtable *));
    

    should be:

     this->method = (struct node_vtable *)malloc(sizeof(struct node_vtable));
    

    Unfortuntaly I cannot check whether it fixes the problem (plain cygwin x86 here), if not then definitely check it with valgrind.

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

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.