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.

Geek Culture / C/C++ globals arrays

Author
Message
Darktib
17
Years of Service
User Offline
Joined: 18th Jun 2007
Location: France
Posted: 13th Oct 2007 17:53
Hello,

I am developing a dll (Astar Pathfinding Dll ) and I have problems with arrays : I want to allocate the whole arrays of the dll in one function (i.e. InitAstarPlugin) and because I want to use the arrays in other functions I need to create globals arrays.

But, I have searched, tried 'static' operator, and other ideas, and I have no result ...

I use GCC compiler with Code::Blocks.

Can you help me?

Thanks in advance.

Her is the code (not the dll code, but a test code)



RTS game project : 3.2% finished
|||%
Jess T
Retired Moderator
21
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 13th Oct 2007 18:12 Edited at: 13th Oct 2007 18:15
Why're you doing memory allocation like that?
Let the compiler sort all that out...



If you want a global variable, then you must declare it outside of the program's entry point (usually, for convention, just above, or in a standard includes header).

To be honest, a global array is a bad idea, especially if you're using C++ when you can create things like objects and the like (much cleaner IMO).

On top of that, you'll more than likely want to be able to make dynamically sized arrays since noone's map-sizes will be the same for each time they call the DLL. This means invoking the C++ STL (or creating your own memory manager) to use Vectors/Lists/whatever.

Personally, I think my native DBP AStar code is fast enough

Nintendo DS & Dominos :: DS Dominos
http://jt0.org
Darktib
17
Years of Service
User Offline
Joined: 18th Jun 2007
Location: France
Posted: 13th Oct 2007 19:31
Thank you for your reponse

I have declare nbL and nbC as variables, because I want change it: the user i=of the dll changes this values as he like.

I want globals arrays because if I declare globals arrays using normal syntax the arrays are fixe-sized. And I know a few how to code in C, but I don't know how I should use std::vector...

Can I use pointers? (I allocate the array, and the function tell to the other functions the arrays pointers?)

And for your plugin, the link is dead; and mine is not actually very optimized... but it can be more faster!

And excuse me if my english is bad- I'm french

RTS game project : 3.2% finished
|||%
ionstream
20
Years of Service
User Offline
Joined: 4th Jul 2004
Location: Overweb
Posted: 13th Oct 2007 20:20
The link works for me. If you're really adamant about the global variables in C++, you might want to look into singletons. They are a fantastic way to make sure only one instance of an object exists throughout the program.

That's not as bad as you think you said.
Darktib
17
Years of Service
User Offline
Joined: 18th Jun 2007
Location: France
Posted: 13th Oct 2007 23:09
The link doesn't works apparently on IE7...

Can you tell me an internet adress to learn singletons?

And if anyone knows if the solution is to use pointers, help is welcome (is that sentence correct english??)

And I really want globals arrays in C++, because I use a 'pathmap' array (which describe which state is a cell - obstacle or not) and the rest of arrays depends of the pathmap (openlist, closedlist,...)

Otherwise I can reallocate openlists/closedlists/... every time I call the 'searchpath' function, but I don't know how exactly should I do in this case - I have try a realloc function but that doesn't works (the program stop after 1 min of computing, and surely because of a memory acces violation...)

RTS game project : 3.2% finished
|||%
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 14th Oct 2007 01:10
Here's a template class for a vector of vectors that gives you a 2D array:


Now you have a dynamic array class that knows its own size. It might be marginally faster to use a single vector and calculate the position for each x/y coord, but you want that, you can code it yourself.

Place an instance of this class at global scope, and when you want to set a new size, simply do this:


If you don't want to lose the contents during resize, then you'll need to change the class.

Utility plugins collection and
http://www.matrix1.demon.co.uk for older plug-ins and example code
Jess T
Retired Moderator
21
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 14th Oct 2007 04:45 Edited at: 14th Oct 2007 04:46
Templates are great, aren't they?

For the singleton, here's a short example;

myClass.h


myClass.cpp


How to use



You can see that the value is changed in a seperate function than where it's printed, yet both have the same value. That is because when you use getInstance() on the singleton, you are getting a pointer to the same object in memory.

Nintendo DS & Dominos :: DS Dominos
http://jt0.org
ionstream
20
Years of Service
User Offline
Joined: 4th Jul 2004
Location: Overweb
Posted: 14th Oct 2007 05:07
Or, if you want to combine the world of templates and singletons, make a singleton base template class!




And then use it like so:




Templates rock my socks!

That's not as bad as you think you said.
Darktib
17
Years of Service
User Offline
Joined: 18th Jun 2007
Location: France
Posted: 14th Oct 2007 14:52
Thank you for your answers,

I will try singletons and template, but for template I need learn... (I have a very good tutorial for C/C++ in "Site du zéro", C++ tutorial)

I think I have found a solution (but I'm not sure it will works): I allocate memory at the start of the dll, then the init function will reallocate the arrays, but I don't know how to reallocate properly...

But althought the vector method is more complex to set, it will be more easier to use...


I'm not sure of the method I will use...

If someone knows how to realloc an array using realloc... because my compiler doesn't compile C++ dlls (actually, but I hope I will find the problem rapidly)

RTS game project : 3.2% finished
|||%
Darktib
17
Years of Service
User Offline
Joined: 18th Jun 2007
Location: France
Posted: 14th Oct 2007 16:13 Edited at: 14th Oct 2007 17:49
Yeah, I have found !

I use malloc and realloc functions and this works good. I have try the templates and singletons, but I have compilations bugs...

So here is the code, in C, which can be adapted for all you want...

French version (comments and console text):




And English version (comments and console text):



It is a console application.

And I have found the problem with gcc: the options were not adapted to a C++ dll

Thank you very much for your help

@+

RTS game project : 3.2% finished
|||%
Jess T
Retired Moderator
21
Years of Service
User Offline
Joined: 20th Sep 2003
Location: Over There... Kablam!
Posted: 15th Oct 2007 04:06
Good to see that you found the solution

Don't forget about vectors and singletons as both are very good alternatives, maybe for your next project.

Nintendo DS & Dominos :: DS Dominos
http://jt0.org

Login to post a reply

Server time is: 2024-11-19 09:21:52
Your offset time is: 2024-11-19 09:21:52