471,122 Members | 1,148 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

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

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[i].AliasName); } }

Mar 7 '06 #1
1 1914
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.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 7 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by barbaros | last post: by
6 posts views Thread by MikeY | last post: by
1 post views Thread by RSB | last post: by
2 posts views Thread by louise raisbeck | last post: by
1 post views Thread by benoit | last post: by
7 posts views Thread by MarkoH | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.