CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2009
    Posts
    12

    problems appending to array (dynamic array size)

    Hello-

    i'm having problems appending data to the end of a dynamic array, i'm reading in data from a wave file and then getting the number of frames and creating a dynamic array with that memory size however in my loop the data doesn't appear to add to the array, here is a sample, any ideas what might be going wrong? (i should note, i've been doing python for a little while and this is my first jump into the world of c+ and i miss some of the list methods )

    buf = (int *) malloc(num_items*sizeof(int));

    num = sf_read_int(sf,buf,num_items);

    sf_close(sf);

    rawWaveDataArray = new int[num_items];
    int arraySize = sizeof(rawWaveDataArray);
    printf("the array size is: %d \n", arraySize);

    printf("Read %d items\n", num);

    /* Write the data to filedata.out. */
    out = fopen("filedata.out","w");
    for (i = 0; i < num; i += c)
    {
    for (j = 0; j < c; ++j)

    fprintf(out,"%d ",buf[i+j]);
    rawWaveDataArray[j-1] = buf[i+j];

    fprintf(out,"\n");
    }

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: problems appending to array (dynamic array size)

    Why not use the vector or list class instead ?

    Example usage for vector:

    Code:
    #include <vector>
    #include <iostream>
    
    int main()
    {
       std::vector<int> v;
    
       v.push_back(1);
       v.push_back(5);
    
       std::cout << v.size() << "\n";      // prints 2
    
       return 0;
    }

    Also note:

    Code:
    int arraySize = sizeof(rawWaveDataArray);
    printf("the array size is: %d \n", arraySize);
    arraySize will just be the size of an int pointer (probably 4).

  3. #3
    Join Date
    Aug 2009
    Posts
    12

    Re: problems appending to array (dynamic array size)

    I kinda of need it to be an array because I will be performing some FFT on it afterwards.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: problems appending to array (dynamic array size)

    A vector is an array internally. And even if you have a routine that takes an array,
    you can still use vector. Example:

    Code:
    void fft(int * array , int n /* other params */)
    {
    
    }
    
    
    // call using a std::vector<int> v
    
    fft(&v[0],v.size() /* other params */);

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: problems appending to array (dynamic array size)

    Quote Originally Posted by dommmm View Post
    any ideas what might be going wrong?
    The thing you're doing "wrong" is that you are using 'C' memory allocation routines instead of the various ready-made standard container classes that are part of the C++ library.
    Code:
       #include <vector>
      //...
       std::vector<int> rawWaveDataArray;
       std::vector<int> buf(num_items);	
       num = sf_read_int(sf,&buf[0],num_items);
        sf_close(sf);
        rawWaveDataArray.resize(num_items);
        printf("the array size is: &#37;d \n", rawWaveDataArray.size());
       //...
    }
    That entire code that had malloc(), new[], etc. was replaced by just using vector.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Aug 2009
    Posts
    12

    Re: problems appending to array (dynamic array size)

    thanks! that worked kinda. i'm getting some kind of out of bounds error at runtime now though:

    /Developer/SDKs/MacOSX10.5.sdk/usr/include/c++/4.0.0/debug/vector:192:
    error: attempt to subscript container with out-of-bounds index 1757448,
    but container only holds 1757448 elements.

    I integrated the code you put and it looks like this:


    vector <int> rawWaveDataArray;

    out = fopen("filedata.out","w");

    for (i = 0; i < num; i += c)
    {
    for (j = 0; j < c; ++j)

    fprintf(out,"%d ",buffer[i+j]);
    rawWaveDataArray.push_back(buffer[i+j]);

    fprintf(out,"\n");

    }

    I'm guessing by the error message that this is because the vector is being accessed by one container too many, but I was under the impression that vectors scale automatically unlike arrays.

    thanks again

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: problems appending to array (dynamic array size)

    Quote Originally Posted by dommmm View Post
    but I was under the impression that vectors scale automatically unlike arrays.
    They do not resize themselves -- you have to resize the vector using the various methods, namely:

    1) push_back()
    2) resize()
    3) insert()
    4) erase()
    5) clear()
    6) on construction of the vector, you size it there.

    You cannot resize a vector by using operator []. If you go beyond the boundaries of your vector, then the behaviour is undefined.

    As an alternative, if you use vector::at() instead of [], the at() will throw an exception if you attempt to use the vector beyond its boundaries. Then you will know exactly where you are going over the edge.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: problems appending to array (dynamic array size)

    1) Are you missing brackets around the "j-loop" ?

    2) Also, make sure that you are not going out of bounds
    with the "num and c" combination for buffer

    a) I assume that buffer has a size of num

    b) if num = 11 and c = 3 , you will end up accessing
    buffer[9+2] when i = 9 and j = 2 (which is out of bounds).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured