Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,210 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,461 people online right now. Registration is fast and FREE... Join Now!




Dynamic arrays.

 
Reply to this topicStart new topic

Dynamic arrays., How do you properly increment a dynamic arrays element?

trixt.er
28 Sep, 2008 - 04:46 PM
Post #1

D.I.C Head
**

Joined: 28 Sep, 2008
Posts: 115



Thanked: 3 times
My Contributions
QUOTE

Hey I have been trying to add an element to this dynamic array. Not sure how to do it. For a normal array all I would have to do to add another element is this.... index++ .... then.... array[index]. Can I do the same with a dynamic array? First I will show the code for declaring the dynamic array. Then I will show the function that uses the array. This function is placed in main under a while loop. Does it legally add elements to the dynamic array? yes or no?

CODE

setClass::setClass( )
{
       size = 0;
       set = new int[size];//Zero will give me only one element to begin with.
}

void setClass::addElement(int value)
{
        set[size] = value;
        cout << set[size] << " ";
        size++;//Will this allow proper incrementation of the dynamic arrays elements?
}


User is offlineProfile CardPM
+Quote Post


PsychoCoder
RE: Dynamic Arrays.
28 Sep, 2008 - 06:07 PM
Post #2

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,267



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
First of all let me welcome you to Dream.In.Code, I hope you enjoy your stay )

Now, this particular forum is for introductions only, all programming questions should be asked in the appropriate language forum (if you look on the left you can see all the language forums). So I'm going to move this to the C/C++ forum where it belongs smile.gif
User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: Dynamic Arrays.
28 Sep, 2008 - 06:15 PM
Post #3

D.I.C Aficionada
Group Icon

Joined: 11 Jul, 2007
Posts: 3,797



Thanked: 54 times
Dream Kudos: 675
My Contributions
edit...sorry I guess my suggestion wouldn't have helped much.

Welcome anyway. smile.gif

This post has been edited by OliveOyl3471: 28 Sep, 2008 - 06:16 PM
User is offlineProfile CardPM
+Quote Post

trixt.er
RE: Dynamic Arrays.
28 Sep, 2008 - 09:02 PM
Post #4

D.I.C Head
**

Joined: 28 Sep, 2008
Posts: 115



Thanked: 3 times
My Contributions
[quote]Thanks olive and pyschocoder for moving the topic to the proper forum. Sorry about the confusion. Yeah I just my freshmen year at Brigham Young University in Idaho. My major is computer science. This is my first encounter with programming. I absolutely love it. C++ is a very fascinating and powerful language. Under the guidance of my professors and peers I have done more with it than I could have ever imagined possible. I look forward to your added wisdom and support. Thank you again so much.[quote]
User is offlineProfile CardPM
+Quote Post

Psionics
RE: Dynamic Arrays.
28 Sep, 2008 - 09:32 PM
Post #5

D.I.C Head
Group Icon

Joined: 6 Sep, 2008
Posts: 158



Thanked: 11 times
Dream Kudos: 200
My Contributions
here's just a little snippet I put together to help you understand some concepts:
cpp

int main( void )
{
int size;

cout << "How many players are there? __\b\b";
cin >> size;

// Just so you understand, you
// CANNOT allocate it on the stack
//int players[ size ]; *** this is why we use dynamic mem

// Allocate an array of integers on the heap
int* pArray = new int[ size ];

// Add another player to the array
// create a new array that's big enough
int* pBigger = new int[ size +1 ];

// move the old values into the new array
for( int i = 0; i < size; ++i )
pBigger[ i ] = pArray[ i ];

// deallocate the old array
delete[] pArray;

// deallocate the array
delete[] pArray;
pArray = 0;


return 0;
}


In practical use, chances are you're going to want to just change the size to adjust to what you need, so here's a little more 'in-depth' example:
cpp

// Assume you have a structure with
// a char* ptr with a name
// Chances are you'll be doing this in a function
void ChangeArray( Structure* ptr )
{
// *** Dynamic String Input for the structure ***
// Step 1: Create a really big temp array
const int MaxLen = 100;
char temp[ MaxLen ];

// Step 2: User Input
cout << "Enter a name: ";
cin.get( temp.name, 100 );

// Step 3: Allocate shorter array
// add 1 to account for null term.
int len = (int)strlen( temp ) + 1;

// need to allocate the structure's name
ptr->name = new char[ len ];

// Step 4: Copy
strcpy_s( ptr->name, len, temp );
}


Although this is a little more code than just adding 1 to the size, but this little snippet will allow you to change any size array down to exactly what you need. After all, that's the beauty of dynamic memory! smile.gif

Hope this helps and welcome to </dic> smile.gif
User is offlineProfile CardPM
+Quote Post

trixt.er
RE: Dynamic Arrays.
29 Sep, 2008 - 04:40 PM
Post #6

D.I.C Head
**

Joined: 28 Sep, 2008
Posts: 115



Thanked: 3 times
My Contributions
QUOTE
That makes a lot more sense. I can do it that way but that seems a little tedious. I know that dynamic memory uses the new operator to allocate memory from the heap but I am not sure what the procedure is to add another element to a dynamic array. For example say I had a dynamic array that consisted of 5 elements. If I want to add another element to the dynamic array can I just do the following?

[code]
int* dynArray = new int[4];//Taking into account the zero index. Total of five elements.
//Then can I increase the size of the dynamic array to 6 elements and set the last element to a value by doing the following?
dynArray[4 + 1] = 384;
//I could just test it out. But I wish to understand how the compiler will handle this dynamic code. Thank you again for your willingness to assist. These forums are great.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Dynamic Arrays.
29 Sep, 2008 - 04:50 PM
Post #7

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 3,569



Thanked: 267 times
Dream Kudos: 525
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
You seem to be laboring under a misconception. Yes, arrays use "zero based indexes". No, zero isn't one.

CODE

int* dynArray = new int[4];//Taking into account the zero index. Total of five elements.


Not five, four.

CODE

int array[4]; // size is 4, elements are numbered 0..3.  There are four elements.


When you've allocated an array, that's it. It's done. It doesn't grow. Psionics gave one solution. You create a new array the size you want, copy the contents of the prior array to the new one, and drop the old one.

This limitation is a big reason linked lists are used. Hope this makes sense.


CODE

int* dynArray = new int[4];//Taking into account the zero index. Total of five elements.
//Then can I increase the size of the dynamic array to 6 elements and set the last element to a value by doing the following?
dynArray[4 + 1] = 384;
//I could just test it out.  But I wish to understand how the compiler will handle this dynamic code.


It will probably crash, horribly. dynArray is only goo up to dynArray[3]. If you try to address it beyond that point, it will be writing a value somewhere else, possibly over the executing code itself. It won't be pretty. Unfortunately, as stated, arrays are not dynamic. If you want dynamic, look into the vector class.




This post has been edited by baavgai: 29 Sep, 2008 - 04:55 PM
User is online!Profile CardPM
+Quote Post

trixt.er
RE: Dynamic Arrays.
29 Sep, 2008 - 07:20 PM
Post #8

D.I.C Head
**

Joined: 28 Sep, 2008
Posts: 115



Thanked: 3 times
My Contributions
Allright yeah that is what I thought the case would be. Illegal! Well hey thanks for the support. It's great I found this forum. No one in my town programs and I am away from asking my professors at college. Thank you all so much for the experience that was shared. You all gave me some new insights and options to try. smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 07:53AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month