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

  • Home
  • SEARCH
  • 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 8777475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:17:15+00:00 2026-06-13T19:17:15+00:00

Pertaining to C programming. Suppose i do this: struct myStruct { pthread_mutex_t myMutex; sem_t

  • 0

Pertaining to C programming.

Suppose i do this:

struct myStruct
{
    pthread_mutex_t myMutex;
    sem_t mySemaphore;
};

`

And i malloc the appropriate size and initialize the Semaphore:

myStruct *create_myStruct()
{
    myStruct *temp; 

    temp = (myStruct *) malloc(sizeof(myStruct));
    sema_init(&sema, 0, 0);
    pthread_mutex_init(&(temp->myMutex), NULL);
    return temp;
}

Do I still need to manually destroy the semaphore or just calling free will do?

  • 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-13T19:17:16+00:00Added an answer on June 13, 2026 at 7:17 pm

    You need to destroy both the mutex and the semaphore, and that needs to happen before you free the memory. Otherwise you have a resource leak, and your program has undefined behaviour.

    You should mentally distinguish between objects and the state they represent on one hand, and the memory in which the object is stored on the other hand. Neither one knows about the other, and both need to be set up and torn down individually (and in reverse order).

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

Sidebar

Related Questions

This is a general programming question, not pertaining to any specific language. A new
I have two tables pertaining to this question: conversations has many messages . The
I'm gonna try to reword this more properly as the last two questions pertaining
I'm fairly new to OO programming specifically with Java. I have a question pertaining
I know there are many similar threads out there pertaining to this but I
This question is quite popular, and there are already lot of questions pertaining to
This question is specifically pertaining to Couchbase, but I believe it would apply to
I am new to javascript plugins. So this is my question pertaining to it
All of this is pertaining to WebHttp binding, hosted in a custom Service Host
I've found several responses to this, but none pertaining to PHP (which is an

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.