Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

moronmaster

simple variable length array

This topic is 5659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, how do you create variable length array? For example: int mysize = 100 char buffer [mysize]; -This doesn''t work cause an array needs to be created with a constant value. The idea is, instead of having a fixed 100 element array , i''d like the program to determine the size it needs then create the array accordingly. How would you go about doing this? I read somewhere of something like this: int i = 100 vector buffer(i); is supposed to work. But unfortunately i keep getting something along the lines of vector not declared/defined. Can someone help point out what to do?

Share this post


Link to post
Share on other sites
Advertisement
What you're looking to do is called Dynamic Allocation of an array. In other words, you're defining the size at runtime as oppose to compiling time. So what you can do instead is use the keyword 'new' to allocate your array.

For example,


int main()
{
int SIZE=100;
char *foo=new char[SIZE];

//more code goes here

//do more stuff etc.


delete[]foo;//release memory when done

return 0;
}


You mentioned vectors which is just an array that can be resized. It does this through dynamic allocation. I believe this is available as part of the standard template library so you should probably look it up somewhere in the book. In short though you can just resize your array at anytime you see fit to fit your needs.

You're getting that undefined vector error because you probably forgot to include the header file for it. Try adding this to the top of your source.


#include <vector.h>


hope that helps

[edited by - Greatwolf on June 18, 2003 3:02:04 AM]

Share this post


Link to post
Share on other sites

#include<vector>
using namespace std:

vector<type> buffer(i);

// OR


#include<vector>

std::vector<type> buffer(i);


where type is the data type the vector is to contain, such as: int, char, double, float, etc.

Share this post


Link to post
Share on other sites
Just to make sure your not confused

std::vector is a dynamically resizing array.

you should look for some tutorials on it... and remember with no .h

but i wouldnt start using that until you really understand just a plain Dynamically Allocated array.


#include <vector>

// i like to typedef the different vectors i use

typedef std::vector< char > CharVec;

/* e.g ... */
int buffer_size = 20;
CharVec buff( buffer_size );

Share this post


Link to post
Share on other sites
Cool!
Thanks! These are exactly what i needed.

By the way Yoshin
SHould the code be
#include
using namespace std; //<----'';'' instead of '':''?
vector buffer(i);

Cause i get compile error swhen i use '':''.

Share this post


Link to post
Share on other sites
quote:
Original post by moronmaster
Cool!
Thanks! These are exactly what i needed.

By the way Yoshin
SHould the code be
#include
using namespace std; //<----';' instead of ':'?
vector buffer(i);

Cause i get compile error swhen i use ':'.


your question's confusing, what are you asking?


using namespace std;


is one statement so of course when that statement ends we use semicolon ';' to show that. Why would you use a colon ':'?

Edit: wait nm, it's a typo of Yoshin yes that should be a semicolon lol

[edited by - Greatwolf on June 18, 2003 5:32:55 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by moronmaster
how do you create variable length array?
For example:
int mysize = 100
char buffer [mysize];



In C99, this is legal and works as expected.

EDIT: That is, if you put a semi-colon after 100

[edited by - Way Walker on June 18, 2003 5:22:45 PM]

Share this post


Link to post
Share on other sites
I''ve found that vectors and other container classes of the stl are extremely convenient (and pretty simple) to use. I recomment picking up The C++ Standard Library: A Tutorial and Reference by Nicolai M. Josuttis. It pretty much explains everything you need to know about the stl. Although I''m only beginning to learn all the book has to offer, I can see already that once I become used to all the features it will be invaluable.

unkn.Enigma1625

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!