Create an array of pointers?

W

Wendell Wilkie

I am working with a 3rd party unmanaged dll, and I need to pass an array of
char* as an argument.

I can used fixed to get a single char* as follows:

char[] buf = new char[64];
fixed (char* p = buf) { ... }

But I do not know how many of these to create until runtime. I cannot think
of a way to use fixed in a loop to fix multiple pointers. Is there a way?

Alternately, I could create and fix a single long char[] and treat it as
several shorter char[] as follows:

int count = 3;
char[] buf = new char[64 * count];
fixed (char* b = buf)
{
for (int i = 0; i < count; i++)
{
char* tmp = b + (64 * i); // this is the start of each shorter
char[]
}
}

This would meet my first requirement.

But how do I create an array of char* to hold the values produced by the
above loop? I tried the following, but p is always null. Using the
debugger, it seems that char*[] results in an array of char. Why?

int count = 3;
char[] buf = new char[64 * count];
char*[] ptrs = new char*[count];
fixed (char* b = buf)
{
fixed (char** p = ptrs) // this always leaves p = null
{
for (int i = 0; i < count; i++)
{
p = b + (64 * i);
}
}
}

Thanks in advance,
Wendell
 
N

Niki Estner

Wendell Wilkie said:
I am working with a 3rd party unmanaged dll, and I need to pass an array of
char* as an argument.

Wouldn't marshalling a char[][] parameter do that job?
I can used fixed to get a single char* as follows:

char[] buf = new char[64];
fixed (char* p = buf) { ... }

But I do not know how many of these to create until runtime. I cannot
think
of a way to use fixed in a loop to fix multiple pointers. Is there a way?

I guess you could do it using recursion: Every function call fixes one
string, calls the next and the final one calls your unmanaged function. But
I don't know if it's a good idea to fix so many pointers at the same time.
Alternately, I could create and fix a single long char[] and treat it as
several shorter char[] as follows:

int count = 3;
char[] buf = new char[64 * count];
fixed (char* b = buf)
{
for (int i = 0; i < count; i++)
{
char* tmp = b + (64 * i); // this is the start of each shorter
char[]
}
}

This would meet my first requirement.

But how do I create an array of char* to hold the values produced by the
above loop? I tried the following, but p is always null. Using the
debugger, it seems that char*[] results in an array of char. Why?

int count = 3;
char[] buf = new char[64 * count];
char*[] ptrs = new char*[count];
fixed (char* b = buf)
{
fixed (char** p = ptrs) // this always leaves p = null
{
for (int i = 0; i < count; i++)
{
p = b + (64 * i);
}
}
}


Really don't know why this doesn't work; This seems to:

int count = 3;
char[] buf = new char[64 * count];
IntPtr[] ptrs = new IntPtr[count];
fixed (char* b = buf)
{
fixed (IntPtr* p = ptrs) // this always leaves p = null
{
for (int i = 0; i < count; i++)
{
p = (IntPtr)(b + (64 * i));
}
}
}

Niki
 
N

Niki Estner

Niki Estner said:
...
int count = 3;
char[] buf = new char[64 * count];
char*[] ptrs = new char*[count];
fixed (char* b = buf)
{
fixed (char** p = ptrs) // this always leaves p = null
{
for (int i = 0; i < count; i++)
{
p = b + (64 * i);
}
}
}


Really don't know why this doesn't work; This seems to:


I''ve looked at this code again, and notices it *does* actually work fine:

int count = 3;
char[] buf = new char[64 * count];
char*[] ptrs = new char*[count];
fixed (char* b = buf)
{
fixed (char** p = ptrs) // this always leaves p = null
{
for (int i = 0; i < count; i++)
{
p = b + (64 * i);
}
}
}
foreach (char* p in ptrs)
Console.WriteLine((IntPtr)p);

Prints out "good" pointer values: if "p" was really null in that fixed
block, the code would crash: I'd say this is a debugger bug; Maybe the CLR
debugger hasn't been tested with pointers too much.

Niki
 

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