Thread: Using new with an array of constant length arrays

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    Using new with an array of constant length arrays

    I'm having trouble getting to grips with the syntax of new in c++. I come from a C background.

    The thing is I have a an array

    Code:
    float (*arr)[3]
    so a pointer to an array of 3 floats. I now wish to allocate memory to the pointer arr so that it can hold an array of float-triples. I know I could do it by making arr of type float ** instead and then allocate in a loop, but since I know the length of each element there must be an easier way that clearly shows my intention.

    I tried with

    Code:
    arr=new float[3] [N]
    but this refuses to compile.
    Last edited by cppfreak; 06-21-2011 at 05:16 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    With your C background, you should have known that arr is an array of 3 pointers to float, not a pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Create a struct instead. Or even a class with overloaded [] operator. Or just use std::array.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you just want a pointer to an array of 3 floats then you don't specify the size in the declaration. You just declare a pointer and specify the size where you do allocation.
    In C you'd do:
    Code:
    float *arr = malloc(sizeof(float) * 3);
    and in C++:
    Code:
    float *arr = new float[3];
    It is unusual to allocate such a small fixed-size buffer in real-world code however, you'd typically declare a local array, or just use a std::vector.
    Welcome to the happy land of C++!
    Last edited by iMalc; 06-21-2011 at 03:48 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by iMalc View Post
    If you just want a pointer to an array of 3 floats then you don't specify the size in the declaration.
    What he actually wants to achieve is:
    an array of float-triples
    Like:
    Code:
    std::vector< std::array<float, 3> >

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    As Laserlight pointed out there was indeed a mistake in the first post, I have corrected it now (happens when you are in a hurry).

    Thank you for your answers. I fond that using

    Code:
    arr=new float[N] [3]
    seems to do what I want. The suggestion to use std::vector and std::array seems very interesting as well, but I have to interface with some C libraries so I need to be able access the data as an C array of float-triples. Is it possible to access the underlying consecutive memory block as a normal C array by making a vector of std::arrays (or a std::array of std::arrays)?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You got the sizes in the wrong order when you called new.
    Code:
        float   (*arr)[3];
        arr = new float[10][3];
        delete [ ] arr;
    Edit:
    Heh, looks like you figured it out
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Indeed The syntax seems a bit backwards to me however. I figured the syntax was "new type amount", and since the type I want is float[3] and the amount is N I seemed to get it wrong.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by cppfreak View Post
    Is it possible to access the underlying consecutive memory block as a normal C array by making a vector of std::arrays (or a std::array of std::arrays)?
    Vector and Array are contiguous in memory and you can obtain address of the first element with: &vec[0] or &arr[0], although I am not sure if this is guaranted for 2 dimensions (I do not know whether std::array is allowed to hold additional information), like:
    Code:
    std::vector< std::array<float, 3> > vec;
    
    &vec[0][0] + 3 == &vec[1][0] // satisfied?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable length arrays
    By nik2 in forum C Programming
    Replies: 2
    Last Post: 03-18-2010, 09:48 AM
  2. How to get length of array of char arrays
    By nicoeschpiko in forum C Programming
    Replies: 3
    Last Post: 03-13-2010, 12:53 PM
  3. Declaring and initialising constant nested arrays
    By officedog in forum C Programming
    Replies: 6
    Last Post: 10-28-2008, 09:55 AM
  4. length (arrays)
    By 182 in forum C++ Programming
    Replies: 10
    Last Post: 02-27-2006, 02:57 PM