How do I create a dynamic collection @ run-time using C (struct data Type) : C++/CLI Interop

R

Russell Mangel

Sorry about the Cross-Post, I posted my question in the wrong group.

Hello,

What is the simplest way to create a dynamic collection (during run-time),
using basic C (Struct data types). Since I am doing C++/CLI interop I wish
to avoid using vector class.

I am using Visual Studio 2005 C++/CLI, and I am writing an Un-Managed class
library. The project type will be a static library (possibly a dll). I have
designed this project type to support both Un-Managed Win32 Console client,
and a Managed (.NET) console client.

I have created a basic (static struct), which only handles 255
messageStores.

Can someone point me in the right direction? I don't need any sorting or
deleting. Just Adding.

Russell Mangel
Las Vegas, NV

// Unmanged C++ Class
class MyStuff
{
public:
MyStuff::MyStuff(void) { }
MyStuff::~MyStuff(void) { }
struct MessageStores {
int cValues;
struct {
LPTSTR AliasName;
LPTSTR ServerName;
int cMessages;
}MessageStore[255]; };

MessageStores MyStuff::GetMessageStores() {
/*
This will be a loop which fills the structure, currently it is static
(255).
I don't know in advance the count of MessageStores.
How would I create this collection at run-time using the basic data
structures of C ?
I am aware of Vector class. I can not use it.
*/
MessageStores messageStores;
messageStores.MessageStore[0].AliasName = "BugsBunny";
messageStores.MessageStore[0].ServerName = "AcmeServer";
messageStores.MessageStore[0].cMessages = 3333;
messageStores.MessageStore[1].AliasName = "Daffy";
messageStores.MessageStore[1].ServerName = "MallardServer";
messageStores.MessageStore[1].cMessages = 4000;
messageStores.MessageStore[2].AliasName = "Elmer";
messageStores.MessageStore[2].ServerName = "ToonServer";
messageStores.MessageStore[2].cMessages = 2000;
messageStores.cValues = 3;
return messageStores; }};

// Un-Managed Client
void main(void){
MyStuff ms;
MyStuff::MessageStores messageStores = ms.GetMessageStores();
for(int i = 0; i < messageStores.cValues; i++) {
printf("%s\n", messageStores.MessageStore.AliasName); } }
 
G

Guest

What is the simplest way to create a dynamic collection (during run-time),
using basic C (Struct data types). Since I am doing C++/CLI interop I wish
to avoid using vector class.

I am using Visual Studio 2005 C++/CLI, and I am writing an Un-Managed class
library. The project type will be a static library (possibly a dll). I have
designed this project type to support both Un-Managed Win32 Console client,
and a Managed (.NET) console client.

I have created a basic (static struct), which only handles 255
messageStores.

Can someone point me in the right direction? I don't need any sorting or
deleting. Just Adding.

A simple linked list implementation would do the trick. there are probably
dozens of free examples floating around. just do a google for C linked list.

Below I changed your struct to support a Next pointer and a prev pointer.
this makes it relatively easy to support adding, inserting and deleting
elements.
Note that I changed MessageStore itself to a pointer to be able to represent
an empty list.

implementing the functions for using the list is pretty straightforward. As
I said, there are probably examples enough on the net.

// Unmanged C++ Class
class MyStuff
{
public:
MyStuff::MyStuff(void) { }
MyStuff::~MyStuff(void) { }
struct MessageStores {
int cValues;
struct Message {
LPTSTR AliasName;
LPTSTR ServerName;
Message * Next;
Message * Prev;
} *MessageStore };


--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top