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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:07:39+00:00 2026-06-09T02:07:39+00:00

I’m trying to declare a constant variable in C++ ; #include <iostream> #include <pthread.h>

  • 0

I’m trying to declare a constant variable in C++;

        #include <iostream>
        #include <pthread.h>
        #include <stdlib.h>

        using namespace std;

        //
        // declare the mutex
        //
        static pthread_mutex_t mutex    = PTHREAD_MUTEX_INITIALIZER;

        //
        // AVOID NEGATIVE NUMBERS
        //
        unsigned int MAXSIZE = 0;
        unsigned int head = 0;
        unsigned int tail = 0;  

        //
        // return a empty circular queue
        //
        int* initialize(int size){

           MAXSIZE = size;
           pthread_mutex_lock( &mutex );
           int* queue = new int[ MAXSIZE ];
           // initialize each position to zero ( explicitly )
           for(int i = 0; i < MAXSIZE; i++){
            queue[i] = 0;
           } 

           pthread_mutex_unlock( &mutex );
           return queue;
        }

        //
        // enqueue number into the queue
        // returns the position it was stored
        //
        void* enqueue( void* local_queue, void* local_data ){
           // ASSERT ONLY ONE THREAD EACH  TIME
           pthread_mutex_lock( &mutex );
           // convert back to int
           int data = *((int*)(&local_data));
           int* queue = (int*)local_queue;

           queue[tail] = data;
           tail = (tail+1) % MAXSIZE;

           pthread_mutex_unlock( &mutex );
           cout << "Tail: " << tail << endl;
        }

        //
        // dequeue, given the queue
        //
        void* dequeue( void* queue ){
           int temp;
           pthread_mutex_lock( &mutex );
           int* local_queue = ( int* )queue; 
           temp = local_queue[ head ];
           head = ( head + 1 ) % MAXSIZE;

           pthread_mutex_unlock( &mutex );
           cout << "Removed: " << temp << endl;
    }

    // 
    // print the queue as it is
    //
    void* display( void* local_queue ){
       pthread_mutex_lock( &mutex );
       int* queue = (int*)local_queue;
       if( head == tail ){
        cout << "Queue underflow" << endl;
       }
       else{
        //
        // prints each element in the queue
        //
        for( unsigned int i = head; i < tail; i = (i+1) % MAXSIZE ){
            cout << queue[i] << endl;
        }
       }
       pthread_mutex_unlock( &mutex );
    }

    //
    // delete the memory allocated to the queue
    //
    void remove( int* queue){
       delete queue;
    }

    //
    // explain the user how to run the program
    //
    void usage(){
      cout << "Usage: " << endl; 
      cout << "    ./queue [size] [elements]" << endl;
      cout << "ex: ./queue 5 0 1 2 3 4 5" << endl;
    }

    //
    // main function, the tests are done in the for loop
    //
    int main( int argc, char* argv[] ){

       pthread_t threads[5];

       if(argc < 2){
        cout << "Args must be at least 1 " << endl;
        usage();
        return -1;
       }

       for(size_t j = 0; j < 5; j++){   
        unsigned int size = atoi( argv[1] );
        cout << "Size: " << size << endl;
        int* queue = initialize(size);

        for(size_t i = 2; i < argc; i++){
            enqueue( queue, (void*)atoi( argv[i] ) );
        }
            pthread_create( &threads[j], NULL, dequeue, (void*)queue );
        // make sure memory is freed 
        // finally end the thread
        pthread_join( threads[j], NULL );
        remove(queue);
       }
       return 0;
    }

I would like to have unsigned int MAXSIZE = 0; declared as const unsigned int MAXSIZE; So I could initialize that at runtime. I know it can be done in the constructor of class, however I would like to know whether there is a way to initialize MAXSIZE to size which is given by the user. MAXSIZE is used to implement the circular queue implemented in an array was shown, so it’s important to MAXSIZE to be declared as const to avoid being changed and then affect the circular operation of the queue.
Thanks.
*I hope I clarify my question enough to have a more precise answer. I’ve added all my code into the question for completeness and for the sake of the community. *

  • 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-09T02:07:40+00:00Added an answer on June 9, 2026 at 2:07 am

    The only way you can initialize a const variable at run-time is if it’s a member of a class. Then you can use the initialization list of the constructor to set the initial value.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
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 have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.