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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:39:25+00:00 2026-06-18T02:39:25+00:00

Strange behaviour when passing argument sem_t to constructor A. Expected output was 5555 but

  • 0

Strange behaviour when passing argument sem_t to constructor A. Expected output was 5555 but i got 5055. Please point out if there are design problems too.

  1 #include <iostream>
  2 #include <pthread.h>
  3 #include <semaphore.h>
  4 using namespace std;
  5 
  6 class A {
  7   public:
  8     pthread_t thr_id;
  9     int& k;
 10 
 11     A(sem_t& sem, int k) : k(k){}
 12     A(int k) : k(k){}
 13 
 14     void start(){
 15       cout << k;
 16       pthread_create(&thr_id, NULL, foo2, NULL);
 17       cout << k;
 18     }
 19     void join(){
 20       pthread_join(thr_id, NULL);
 21     }
 22     static void* foo2(void* i){}
 23 };
 24 
 25 int main() {
 26   sem_t sem;
 27   A* ac1 = new A(sem, 5);
 28   ac1->start();
 29   ac1->join();
 30   A* ac2 = new A(5);
 31   ac2->start();
 32   ac2->join();
 33  return 0;
 34 }
  • 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-18T02:39:26+00:00Added an answer on June 18, 2026 at 2:39 am
    int& k;
    A(int k) : k(k){}
    

    You are initializing member k as reference to local k in constructor. After constructor finishes it becomes dangling reference and using it is undefined behavior.

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

Sidebar

Related Questions

I'm experiencing som strange behaviour by the Silverlight ComboBox. I started out with some
I have very strange behaviour of Java Date class: System.out.println(new Date().toGMTString()); long l =
I've got very strange behaviour when I run playframework in scala. I used anorm
I got a strange behaviour in PHP with arrays and objects, that I don't
A behaviour i'm observing w.r.t passing serializable data as intent extra is quite strange,
Really strange behaviour here. I've got a view with a scroll view inside, which
I've got strange behaviour and I can't realize how to fix it. I've got
There is some strange behaviour in parsing KML produced by Google Earth using a
I just see strange behaviour in Eclipse . I am developing one android project
I'm seeing strange behaviour when using PostgreSQL in a Hibernate/JPA environment with a single

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.