Thread: ”Converting” an array…

  1. #16
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    89
    Quote Originally Posted by AndiPersti View Post
    Code:
    int main(int argc, char *argv[])
    {
        double a[INDEXMAX][2];
    The array is stored on the stack, thus the command to check how much stack memory you are allowed to use is
    Code:
    ulimit -s
    Code:
    ~$ ulimit -s
    8192
    ~$
    8192 bytes? That's not much…
    Quote Originally Posted by AndiPersti View Post
    Create your array on the heap (using malloc())

    Bye, Andreas
    That's good news, because that's what I intended to do in my ”real” project anyway. But I realise that I have a lot to learn before starting it up for real, so until then there will be a lot of experimenting with small meaningless things that just test things…

    Thanks! I'll do some further tests on the subject later.

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by guraknugen View Post
    Code:
    ~$ ulimit -s
    8192
    ~$
    8192 bytes? That's not much…
    That's 8192 kB = 8 MB.

    Bye, Andreas

  3. #18
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    89
    Quote Originally Posted by AndiPersti View Post
    That's 8192 kB = 8 MB.

    Bye, Andreas
    Ah, yes. That makes sense. 8 MB (or rather 8 MiB) seems to be the resulting maximal size that my test file could produce without segmentation fault (”a little more than” 5×10⁵×2 doubles, one double=8 bytes ⇨ ”a little more than” 8 MB, which 8 MiB is…).

    Thanks for the information!

    I'll try the malloc thing, although I think it's a little bit tricky to do that with multi dimensional arrays. As far as I understand, I need at least one loop for a 2D array, is that right?

    So I guess Array[Channel][Sample] will be a lot faster to allocate than Array[Sample][Channel], since there are millions of samples but maximum 8 channels in a FLAC file.

    Maybe it's a bit overkill to use a 2D array anyway in this case. The library I'm going to use will give me a 1D array of all the audio, maybe I should just stick with it. Well, I can decide that after some more experimenting. I like clean and easy-to-follow code, and I think Array[Channel[Sample] looks cleaner than Array[Sample*Channels+Channel]
    Maybe I should just write a simple function that does the ”dirty job” for me:
    Code:
    double getSample(double Array, size_t Row, size_t Column, size_t Columns) {
        return Array[Row*Columns+Column];
    }
    or something like that.
    Or a macro…?
    There is a lot to think about here, I guess. For example, if using a 1D array, I need to calculate the position (Sample*Channels+Channel) millions of times in my loops (once for each sample in every loop). With a 2D array, there's no need for that, so it should be faster, right?
    Decisions, decision, decisions…

  4. #19
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by guraknugen View Post
    So I guess Array[Channel][Sample] will be a lot faster to allocate than Array[Sample][Channel], since there are millions of samples but maximum 8 channels in a FLAC file.

    Maybe it's a bit overkill to use a 2D array anyway in this case. The library I'm going to use will give me a 1D array of all the audio, maybe I should just stick with it.
    A 2d array is just an array of arrays, so yes typically you would allocate memory for each subarray. If the number of subarrays is large, then it's conceivable that it might take longer than allocating a 1d array of the same size. But consider something for a moment - you only need to allocate this thing one time. And your program will probably re-use it for more than just one time. So ultimately the "set up time" doesn't matter too much. The choice of data structure should be based on how you use it and how efficiently you can use it. Set up and tear down time are basically "constant time operations"

    If your library uses a 1D array then I think it would be best to use that simply for lack of a reason not to. If you want an accessor function to get at elements in a Row,Column fashion then use that. This conversion step shouldn't impact performance if you compile with optimization.

  5. #20
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    89
    Quote Originally Posted by guraknugen View Post
    For example, if using a 1D array, I need to calculate the position (Sample*Channels+Channel) millions of times in my loops (once for each sample in every loop). With a 2D array, there's no need for that, so it should be faster, right?
    Unless I design the loops so that I don't have to calculate that every time, like:
    Code:
    for(size_t Ch=0; Ch<Channels; Ch++) {
        for(size_t Sample=Ch; Sample<Samples; Sample+=Channels) {
            Do stuff with current sample=AudioData[Sample];
        }
        Maybe more stuff here;
    }
    I'm not sure why I didn't think of that before… Maybe I did, but forgot about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting char 1d array to 2d array
    By wasi.cjr in forum C Programming
    Replies: 8
    Last Post: 03-19-2011, 04:21 PM
  2. Converting tchar array to string array
    By GeekInTraining in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2011, 01:34 AM
  3. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  4. Replies: 8
    Last Post: 11-12-2008, 11:25 AM
  5. Converting character array to integer array
    By quiet_forever in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2007, 05:48 AM