Thread: declaring arrays with visual C++ toolkit 2003

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    declaring arrays with visual C++ toolkit 2003

    I am trying to create an array based off of a argument in one of my functions. Here is my code:

    Code:
    void Engine::PrimitiveEnd(CUSTOMVERTEX* buffer, int vertnumber)
    {
        CUSTOMVERTEX verts[vertnumber];
        
        for (int i = 0; i < tempverts.size(); ++i)
        {
            verts[i].x = tempverts[i].x;
            verts[i].y = tempverts[i].y;
            verts[i].z = tempverts[i].z;
        }
        
        buffer = verts;
    }
    These are the errors I get:

    Engine.cpp(159) : error C2057: expected constant expression
    Engine.cpp(159) : error C2466: cannot allocate an array of constant size 0
    Engine.cpp(159) : error C2133: 'verts' : unknown size


    But I only get this using the visual C++ toolkit 2003, but the GCC compiler does not give this error (but for reaons I can't use GCC).

    How would I declare an array of "unknowen" size than.

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    EDIT: Nevermind, I just glanced at it. I don't know.
    Last edited by durban; 11-02-2005 at 09:19 PM.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Actaully I get no error on that part. I only get the errors on this line:

    CUSTOMVERTEX verts[vertnumber];


    sorry for leaving that part out.

  4. #4
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    What is tempverts?
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    temp verts is declared globaly in the class like this:

    vector<CUSTOMVERTEX> tempverts;

    The user puts data into tempverts with anouther function than the function above (first post) gives the user the final result.

  6. #6
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    The reason I ask is: You're letting the user set the length of verts but you'r looping through tempverts.size() which I don't get. Say I entered 1 for vertnumber and the size of tempverts is 12 it would generate an error. for(int i=0; i<vertnumber; i++) should be your loop.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    oh about that, well if I put tempverts.size() for the size of the array, I get the same errors. I supose using the size of tempverts would be better but I get the same errors.


    EDIT: once I get this to work, I will change that back to my original code wich is what you said.
    Last edited by Rune Hunter; 11-02-2005 at 09:29 PM.

  8. #8
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    yea that's what I was about to suggest, I dunno then. www.bitcomet.com . Download Microsoft Visual Studio 2005
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  9. #9
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Rune Hunter
    Actaully I get no error on that part. I only get the errors on this line:

    CUSTOMVERTEX verts[vertnumber];


    sorry for leaving that part out.
    you cannot (statically) declare an array with a variable that is not a compile-time constant. Visual studio is quite correct to pull you up on this. I believe gcc has an extension which allows this but it is not standard.

    you need to do
    Code:
    CUSTOMVERTEX* verts = new CUSTOMVERTEX[vertnumber];
    but then you need to remember to delete verts.

    I suggest a vector
    Code:
    vector<CUSTOMVERTEX> verts(vertnumber);
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ahh ha. I see the * in there makes all the difference. That worked perfectly.

    That was perfect timing, right before I am going to bed, thanks.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> buffer = verts;

    That line does nothing, since you are passing buffer by value. If it worked in your original code, you'd be saving a pointer to a local variable, which is also bad. If you use new as the solution, and change buffer to be passed by reference, then as long as you delete buffer later in the calling code you are fine.

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ok lets say I return verts, but I can't delete it cause the return exits the function. Would that be fine?

    This is my new code:

    Code:
    CUSTOMVERTEX* Engine::PrimitiveEnd()
    {
        CUSTOMVERTEX* verts = new CUSTOMVERTEX[tempverts.size()];
        
        for (int i = 0; i < tempverts.size(); ++i)
        {
            verts[i].x = tempverts[i].x;
            verts[i].y = tempverts[i].y;
            verts[i].z = tempverts[i].z;
            verts[i].color = tempverts[i].color;
        }
        
        return verts;
    }
    The code works 100% but I want to know is that safe or is there something wrong with the way I did it.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It is a memory leak if you don't delete [] it somewhere else in your code. The function that calls PrimitiveEnd should save the return value and delete [] it when it is done with it, or pass the pointer value to some other code that will eventually delete [] it.

  14. #14
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    So if I use the function like this:

    Code:
    CUSTOMVERTEX* verts1 = eng.PrimitiveEnd();
    and just call:

    Code:
    delete verts1;
    in my clean up section, it should be fine?

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You'll notice I kept putting delete [] in my response (with the brackets). I did that for a reason. Once you figure out what that reason is, then, yes, it should theoretically avoid any memory leaks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  2. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  3. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM
  4. odd errors from msvc std library files
    By blight2c in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2002, 12:06 AM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 04:07 PM