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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:14:21+00:00 2026-05-14T03:14:21+00:00

I have some C structures related to a ‘list’ data structure. They look like

  • 0

I have some C structures related to a ‘list’ data structure.
They look like this.

struct nmlist_element_s {
    void *data;
    struct nmlist_element_s *next;
};
typedef struct nmlist_element_s nmlist_element;

struct nmlist_s {
    void (*destructor)(void *data);
    int (*cmp)(const void *e1, const void *e2);
    unsigned int size;
    nmlist_element *head;
    nmlist_element *tail;
};
typedef struct nmlist_s nmlist;

This way I can have different data types being hold in “nmlist_element->data” .
The “constructor” (in terms of OOP) has the following signature:

nmlist *nmlist_alloc(void (*destructor)(void *data));

Where “destructor” is specific function that de-allocated “data” (being hold by the nmlist_element).

If I want to have a list containing integers as data, my “destructor” would like this:

void int_destructor(void *data)
{
    free((int*)data);
}

Still i find it rather “unfriendly” for me to write a destructor functions for every simple primitive data type. So is there a trick to write something like this ? (for primitives):

void "x"_destructor(void *data, "x")
{
    free(("x" *)data);
}

PS: I am not a macro fan myself, and in my short experience regarding C, i don’t use them, unless necessary.

  • 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-14T03:14:21+00:00Added an answer on May 14, 2026 at 3:14 am

    You shouldn’t need to cast it to an int * to free it. You can just free the void * directly.

    If you really want to do it with macros (or for future reference if you really do have a function that is not generic, unlike free()), the the best way is to use X macros.

    #define TYPES_TO_DESTRUCT \
     X(int) \
     X(char) \
     X(t_mytype)
    
    #define X(type) \
    void type##_destructor(void *data) { free((type *)data); }
    
    TYPES_TO_DESTRUCT
    
    #undef X
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 338k
  • Answers 338k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Does this help? VB.NET vs. C# - Delegates / Events May 14, 2026 at 4:23 am
  • Editorial Team
    Editorial Team added an answer As alex mentioned, definitely rebuild the search index, and dump… May 14, 2026 at 4:23 am
  • Editorial Team
    Editorial Team added an answer You should always close() ServerSocket if you want a predictable… May 14, 2026 at 4:23 am

Related Questions

So i am reorganizing a winforms C# solution to help decouple and make it
This is related to my other question , but I felt like I should
Debugging with gdb, any c++ code that uses STL/boost is still a nightmare. Anyone
I'm doing some Linux kernel development, and I'm trying to use Netbeans. Despite declared
I've created a copy utility in c# (.NET 2.0 Framework) that copies files, directories

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.