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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:41:46+00:00 2026-06-08T07:41:46+00:00

How does one dynamically allocate either a single dimensional array or a two dimensional

  • 0

How does one dynamically allocate either a single dimensional array or a two dimensional array, depending on either the value 1 or 2?

This array needs to hold a frameCount number of floats but split between 1 or 2 channels. The first dimension being the channel count and the second being the number of frames. If there are 2 channels, the frameCount is split between the 2 dimensions, such as [channelNumber] [frameNumber]

I have the following code but I don’t think it is working.

float ** arrayToFill = (float **)malloc((frameCount*channelCount)*sizeof(float));

My array looks like this in the debugger

enter image description here

I need to pass a float ** to a function so it can fill the array with as follows

- (OSStatus) readFloatsConsecutive:(SInt64)numFrames intoArray:(float**)audio withOffset:(long)offset
{
OSStatus err = noErr;

if (!mExtAFRef) return -1;

int kSegmentSize = (int)(numFrames * mExtAFNumChannels * mExtAFRateRatio + .5);
if (mExtAFRateRatio < 1.) kSegmentSize = (int)(numFrames * mExtAFNumChannels / mExtAFRateRatio + .5);

AudioBufferList bufList;
UInt32 numPackets = numFrames; // Frames to read
UInt32 samples = numPackets * mExtAFNumChannels;
UInt32 loadedPackets = numPackets;


short *data = (short*)malloc(kSegmentSize*sizeof(short));
if (!data) {
    NSLog(@"data is nil");
    goto error;
}


bufList.mNumberBuffers = 1;
bufList.mBuffers[0].mNumberChannels = mExtAFNumChannels;
bufList.mBuffers[0].mData = data; // data is a pointer (short*) to our sample buffer
bufList.mBuffers[0].mDataByteSize = samples * sizeof(short);

@synchronized(self) {
    err = ExtAudioFileRead(mExtAFRef, &loadedPackets, &bufList);
}
if (err) goto error;

if (audio) {
    for (long c = 0; c < mExtAFNumChannels; c++) {
        if (!audio[c]) continue;
        for (long v = 0; v < numFrames; v++) {
            if (v < loadedPackets) audio[c][v+offset] = (float)data[v*mExtAFNumChannels+c] / 32768.f;
            else audio[c][v+offset] = 0.f;
        }
    }
}

error:
free(data);
if (err != noErr) return err;
if (loadedPackets < numFrames) mExtAFReachedEOF = YES;
mRpos += loadedPackets;
return loadedPackets;
 }
  • 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-08T07:41:47+00:00Added an answer on June 8, 2026 at 7:41 am

    Your malloc statement is correct, but your type is wrong. Malloc returns an address, (ie a pointer) and the thing at that address is the first of a series of floats. That is, it returns a pointer to a float.

    try

    float * arrayToFill = malloc ( (frameCount * channelCount) * sizeof( float ));
    

    If you really need a pointer pointer, then you probably need to malloc something like this:

    float ** arrayToFill = malloc(frameCount * sizeof(float *));
    for(i=0;i<frameCount;i++)
      arrayToFill[i] = malloc(channelCount * sizeof(float));
    

    But this is just a guess. The function you are calling should be clear on what to pass it.

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

Sidebar

Related Questions

Why does this not work? $w = 'one,two'; $a = array($w); for($i = 0;
This question is in C++. I am trying to dynamically allocate an array of
How does one implement a multithreaded single process model in linux fedora under c
How does one combine two GCC compiled .o object files into a third .o
How does one dynamically load a new report from an embedded resource? I have
How does one dynamically generate a TextBox control at run-time as a result of
With the MVVM pattern, how does one go about dynamically binding an ICommand to
How does one dynamically create table cells based on a dataset? Is Javascript/ajax required
does one perform better over the other in terms of indexing/quering etc ? e.g.
How does one wait until all of the Javascript is loaded before curling a

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.