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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:49:19+00:00 2026-06-10T11:49:19+00:00

I have a queue of set length implemented as a dynamic c array implemented

  • 0

I have a queue of set length implemented as a dynamic c array implemented like this:

typedef struct {
    float* queue;
    int size;
    int pointer;
} QueueStruct;

void createQueue(QueueStruct* queueInstance, int size){
    queueInstance->queue = malloc(sizeof(float)*size);
    queueInstance->size = size;
    queueInstance->pointer = 0;
}

void addElementToQueue(QueueStruct* queueInstance,float element){
    queueInstance->queue[pointer] = element;
    if (queueInstance->pointer == queueInstance.size - 1){
        queueInstance->pointer = 0;
    } else {
        ++queueInstance->pointer;
    }
}

void freeQueue(QueueStruct* queueInstance){
    free(queueInstance->queue);
}

And I want to implement this function:

float* returnQueue(QueueStruct queueInstance){
    //I want this function to malloc a new float* and then put the queue in it in the
    // correct order, from start to finish, as pointed too by the pointer.  
    //Im not sure how to do this.
}

Any help would be appreciated.

Edit: Corrected a silly programming mistake – this is a simplified version of what is actually in my program.

  • 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-10T11:49:20+00:00Added an answer on June 10, 2026 at 11:49 am

    Let’s see if I got that right.

    float* returnQueue(QueueStruct *queueInstance){
        int j = 0;
        float *ret = malloc(sizeof(float)*queueInstance->size);  //Allocates the memory you want.
        //Copies the elements from pointer to End into the new buffer (assumes, that the array has been filled at least once, add a marker to make sure)
        if(queueInstance->FilledOnce) { //Marker variable, explanation as above.
            for(int i = queueInstance->pointer; i < queueInstance->size; ++i, ++j)
                ret[j] = queueInstance->queue[i];
        }
        //Copies the newest elements (from beginning to pointer) into the buffer.
        for(int i = 0; i < queueInstance->pointer; ++i, ++j)
            ret[j] = queueInstance->queue[i];
        return ret; //Returns the code in question.
    }
    

    To make this code work, you’d have to add ‘FilledOnce’ to your struct, and amend your ‘Add’ Code as follows:

    void addElementToQueue(QueueStruct* queueInstance, float element){
        queueInstance->queue[queueInstance->pointer] = element;
        if (queueInstance->pointer == queueInstance.size - 1){
            queueInstance->pointer = 0;
            queueInstance->FilledOnce = 1;
        } else {
            ++queueInstance->pointer;
        }
    }
    

    I also advise you, to reset your variables, once you’re done with it.

    void freeQueue(QueueStruct* queueInstance){
        free(queueInstance->queue);  //Frees the queue
        queueInstance->queue = NULL; //Nulls the reference
        queueInstance->FilledOnce = 0;
        queueInstance->pointer = 0;
        queueInstance->size = 0;
    }
    

    This way, if you reuse the struct, you won’t run into the problem of trying to access non-allocated memory. Just be sure to check for those variables.

    I hope this helps.

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

Sidebar

Related Questions

I have queue from struct type struct test { int numbers; }; queue<test> q;
I have partial C# code for a blocking queue that looks like this: private
I have declared: queue<int, list<int> > Q After a series of calls: Q.push(37); Q.pop();
I have a request queue and a response queue. I would like to take
We have a set of actions or Jobs which we'd like to happen one
I have a set of commands like: .kick .unban .ban .unvouch .vouch .add .del
I have a queue manager QM_TEST created using the following MQSC command: SET AUTHREC
I have some difficulties to set up the correct configuration relative to sendAsynchronousRequest:queue:completionHandler: method
I have a Queue that contains a collection of objects, one of these objects
I have a queue structure that is being used by several pthreads. The threads

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.