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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:42:22+00:00 2026-05-23T03:42:22+00:00

I would like some advice on safe ways to deal with struct’s when the

  • 0

I would like some advice on safe ways to deal with struct’s when the size of certain members are not known at code time.

For example I have a Struct Named “Channel”. This struct has a member name “AudioSourceOBJ” which is a pointer to an an array of other struct type named “AudioSource”. I wont know how many AudioSources I will have per channel until the program is run. I deal with that like this.

channel object

struct channelobj {

        AudioUnitSampleType *leftoutput;
    AudioUnitSampleType *rightoutput;
    AudioSourceOBJ *audioSource;



};

audiosource

struct audiosourceobj {


    AudioUnitSampleType   *leftoutput;
    AudioUnitSampleType   *rightoutput;


};

creation of variable sized structs

void createInputs(ChannelOBJ channel,int numAudioInputs)
{
    channel->audioSource=(AudioSourceOBJ *)malloc(numAudioInputs * sizeof(AudioSourceOBJ));

    for (int i=0;i<numAudioInputs;i++)
    {
        AudioSourceOBJ obj;
        obj=newAudioSourceOBJ();
        channel->audioSource[i]=obj;

    }
}

I think this is o.k?

The problem I am now facing is that even though I can assign memory for the correct number of audio objects in my channel struct, the leftoutput and rightoutput arrays in the audiosource struct will not be set until later in the program. They will be filled with an undermined amount of data, and are likely to change in size and content throughout the lifetime of the application.

Will I have to completely re malloc the channel containing the audiosource every time I want to make changes to a single audio object?

What is a safe way to do this or is there a better approach?

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

    “Will I have to completely re malloc the channel containing the audiosource every time I want to make changes to a single audio object?”

    No. You could for example replace the left output of the ith audio source like this:

    free(channel->audioSource[i].leftoutput);
    channel->audioSource[i].leftoutput = malloc(newSize * sizeof(AudioUnitSampleType));
    

    Or even:

    AudioUnitSampleType *tmp = realloc(channel->audioSource[i].leftoutput,
        newSize * sizeof(*tmp));
    if (tmp == 0) { /* handle the error */ }
    channel->audioSource[i].leftoutput = tmp;
    

    By the way, if you don’t post real code, it’s possible that answers will contain errors due to errors in your examples.

    There seems to be some confusion in your code between pointers and objects, for example the channel parameter is of type ChannelOBJ, then you use it as if it’s a pointer. Is this an error, or is ChannelOBJ a typedef for struct channelobj*? It’s generally better not to conceal that something is a pointer using a typedef.

    If AudioUnitSampleType is likewise a pointer type, then my first code snippet above is incomplete, since it would then also be necessary to free the old objects pointed to by the elements of the array, and allocate new ones. The second one needs to free old ones or allocate new ones according to whether the size is being increased or decreased.

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

Sidebar

Related Questions

I'm not much of a database guru so I would like some advice. Background
I would like some advice on the best approach to use in the following
I am designing RESTful Api's and would like some advice on designing an API
I am considering starting an OSS project and would like some advice. I would
I'm looking into building a distributed application and would like some advice on the
I would like some advice on how to best layout my JPA entity classes.
I would like some advice from anyone experienced with implementing something like pessimistic locking
I'm starting a new project and would like some advice. The purpose is to
I am new to the MVVM model, so I would like some advice, the
I'm pretty sure this is causing a leak and I would like some advice.

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.