Declare a two-dimension array

Discussion in 'C++' started by ottawajn, Dec 13, 2006.

  1. ottawajn

    ottawajn Guest

    Hi, There,

    I want to declare a two-dimension array by
    "float coeftemp1 [1024][512];" in Dev c++. It doesn't work.

    But, if I change it to "float coeftemp1 [1024][500];".
    It works. Could any one know the reason?

    Thank you,




    //**************************
    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int main(int argc, char *argv[])
    {
    float coeftemp1 [1024][512];
    system("PAUSE");
    return EXIT_SUCCESS;
    }
    //**************************
     
    ottawajn, Dec 13, 2006
    #1
    1. Advertising

  2. ottawajn

    Default User Guest

    ottawajn wrote:

    > Hi, There,
    >
    > I want to declare a two-dimension array by
    > "float coeftemp1 [1024][512];" in Dev c++. It doesn't work.
    >
    > But, if I change it to "float coeftemp1 [1024][500];".
    > It works. Could any one know the reason?
    >
    > Thank you,
    >
    >
    >
    >
    > //**************************
    > #include <cstdlib>
    > #include <iostream>
    >
    > using namespace std;
    >
    > int main(int argc, char *argv[])
    > {
    > float coeftemp1 [1024][512];
    > system("PAUSE");
    > return EXIT_SUCCESS;
    > }
    > //**************************


    You are likely hitting an implementation-specific limitation on
    automatic memory. Something along the lines of (but not necessarily)
    using up all the available stack space.


    A possible solution is some sort of dynamic array. As is often the
    case, the recommended first approach is std::vector.





    Brian
     
    Default User, Dec 13, 2006
    #2
    1. Advertising

  3. ottawajn

    wahaha Guest

    I think Brian is exactly right. Assume your float is 8 bytes and
    1024x512x8 = 4k byte. This might be the maximum automatic space. Try to
    use another method such as:

    float **coeftemp1 ;
    int i;

    coeftemp1 = (float *)malloc(1024 * sizeof(int *));

    for (i = 0; i < 1024; i++){
    coeftemp1 = (float)malloc(512 * sizeof(float));




    "ottawajn дµÀ£º
    "
    > Hi, There,
    >
    > I want to declare a two-dimension array by
    > "float coeftemp1 [1024][512];" in Dev c++. It doesn't work.
    >
    > But, if I change it to "float coeftemp1 [1024][500];".
    > It works. Could any one know the reason?
    >
    > Thank you,
    >
    >
    >
    >
    > //**************************
    > #include <cstdlib>
    > #include <iostream>
    >
    > using namespace std;
    >
    > int main(int argc, char *argv[])
    > {
    > float coeftemp1 [1024][512];
    > system("PAUSE");
    > return EXIT_SUCCESS;
    > }
    > //**************************
     
    wahaha, Dec 13, 2006
    #3
  4. ottawajn

    Micah Cowan Guest

    wahaha wrote:
    > "ottawajn дµÀ£º
    > "
    > > Hi, There,
    > >
    > > I want to declare a two-dimension array by
    > > "float coeftemp1 [1024][512];" in Dev c++. It doesn't work.
    > >
    > > But, if I change it to "float coeftemp1 [1024][500];".
    > > It works. Could any one know the reason?
    > >
    > > Thank you,

    >
    > I think Brian is exactly right. Assume your float is 8 bytes and
    > 1024x512x8 = 4k byte. This might be the maximum automatic space. Try to
    > use another method such as:
    >
    > float **coeftemp1 ;
    > int i;
    >
    > coeftemp1 = (float *)malloc(1024 * sizeof(int *));
    >
    > for (i = 0; i < 1024; i++){
    > coeftemp1 = (float)malloc(512 * sizeof(float));


    Please refrain from top-posting.

    This being comp.lang.c++, the new operator is frequently more useful
    than malloc(). Ignoring that, your usage of malloc() above is very
    dangerously broken (and would probably fail to compile, anyway). The
    types you're using to cast the result of malloc are completely wrong,
    as is your sizeof(int *).

    For this reason and several others, C++ users might prefer to do
    something like:

    float (*coeftemp1)[512] = new float[1024][512];

    or, probably more commonly, with a change in how elements are accessed:

    float *coeftempl = new float[1024 * 512];

    (hopefully, substituting declared constants for the meaningless magic
    numbers above).

    Perhaps better, depending on the situation, would be to use a
    std::vector<float>.

    HTH,
    Micah Cowan
     
    Micah Cowan, Dec 14, 2006
    #4
  5. ottawajn

    Bo Yang Guest

    ottawajn :
    > Hi, There,
    >
    > I want to declare a two-dimension array by
    > "float coeftemp1 [1024][512];" in Dev c++. It doesn't work.
    >
    > But, if I change it to "float coeftemp1 [1024][500];".
    > It works. Could any one know the reason?
    >
    > Thank you,
    >
    >
    >
    >
    > //**************************
    > #include <cstdlib>
    > #include <iostream>
    >
    > using namespace std;
    >
    > int main(int argc, char *argv[])
    > {
    > float coeftemp1 [1024][512];

    There, it is too big too put this in your stack.
    So, the OS may terminate your app.
    > system("PAUSE");
    > return EXIT_SUCCESS;
    > }
    > //**************************
    >
     
    Bo Yang, Dec 14, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Larry Lindsey
    Replies:
    5
    Views:
    542
    Mike Wahler
    Sep 27, 2003
  2. James
    Replies:
    11
    Views:
    94,068
    yousafzai
    Jun 4, 2011
  3. Geetesh
    Replies:
    19
    Views:
    614
    Yakov Lerner
    Mar 3, 2004
  4. James
    Replies:
    3
    Views:
    385
    E. Robert Tisdale
    Nov 2, 2004
  5. Luuk
    Replies:
    15
    Views:
    843
    Nobody
    Feb 11, 2010
Loading...

Share This Page