468,539 Members | 1,906 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,539 developers. It's quick & easy.

Create an array of pointers?

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[i] = b + (64 * i);
}
}
}

Thanks in advance,
Wendell
Nov 16 '05 #1
2 7606
"Wendell Wilkie" <wj******@hotmail.com> wrote in
news:us*************@TK2MSFTNGP09.phx.gbl...
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[i] = 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[i] = (IntPtr)(b + (64 * i));
}
}
}

Niki
Nov 16 '05 #2
"Niki Estner" <ni*********@cube.net> wrote in
news:OU**************@TK2MSFTNGP15.phx.gbl...
"Wendell Wilkie" <wj******@hotmail.com> wrote in
news:us*************@TK2MSFTNGP09.phx.gbl...
...
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[i] = 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[i] = 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
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bryan Parkoff | last post: by
19 posts views Thread by gaga | last post: by
2 posts views Thread by Simon Morgan | last post: by
14 posts views Thread by Lambda | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.