Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DLL Talk / How do I create a dynamic array in C++?

Author
Message
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 27th Jan 2011 15:37
I'm trying to port some of my DBPro functions over to a C++ plugin (I'm using Visual C++ Express 2008), to try and speed up my compile times, but I can't figure out how to create dynamic arrays that are global within the plugin.

In DBPro I can write two functions like this:


But how would I write this in C++? I can't figure out how to create a global variable within a C++ function, or how to extend an array the way I can in DBPro (though I have seen refrrence to vector classes, whatever they are). Any ideas?

We spend our lives chasing dreams. Dark Basic lets us catch some of them.
Brendy boy
18
Years of Service
User Offline
Joined: 17th Jul 2005
Location: Croatia
Posted: 27th Jan 2011 23:06
int *TestArray=Null;

void One(int size){
TestArray=new int[size];
}

void Two(int old_size, int new_size){
int *temp=TestArray;
TestArray=new int[new_size];
memcpy(TestArray,temp,sizeof(int)*old_size);
delete []temp;
}

Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 28th Jan 2011 15:46
Wow, this is brilliant! Thank you!

We spend our lives chasing dreams. Dark Basic lets us catch some of them.
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 28th Jan 2011 18:03
Sorry, but it's not that brilliant - ou are using C++, not C, and dynamic array is definitely spelt 'std::vector' in C++.



Omen
17
Years of Service
User Offline
Joined: 7th Nov 2006
Location: Maple Grove, MN US
Posted: 4th Feb 2011 07:02
@Ian, that made me laugh - it made me think of how right$() is spelt 'fast right$()'

MikeRK
13
Years of Service
User Offline
Joined: 18th Aug 2010
Location: United Kingdom
Posted: 31st Mar 2011 09:37
You can get the functionality of a 'Dynamic Array' using an STL Container (i.e. Vector as IanM pointed out)

Other variations are;

std::map, std::list std::deque... etc.


Look here for a reference:

http://www.cplusplus.com/reference/stl/

All have their benefits depending on what you need.

Mikey

::Hybrid Two::Binary Zoo::RGT Veteran::

Login to post a reply

Server time is: 2024-04-19 23:05:24
Your offset time is: 2024-04-19 23:05:24