Variable Size Array

K

Kevin Frey

Hello,

I want to do the equivalent of this in Managed C++:

void MyFunc( int n )
{
Byte[ ] mybuffer = new Byte[ n ];

socket.Receive( mybuffer ... );
}

The key point here is that I want to allocate an array of Bytes according to
a variable, not a constant-sized array.

The compiler complains that n is not constant. For our TCP communications
(porting from unmanaged C++) all of our packets are length prefixed, so we
read the length, then allocate the right amount of space for the buffer.

Can someone tell me how to achieve this?

Thanks

Kevin.
 
L

lallous

Hello,

void MyFunc( int n )
{
char *mybuffer = new char[ n ];
socket.Receive( mybuffer ... );
}
 
B

Bo Persson

lallous said:
Hello,

void MyFunc( int n )
{
char *mybuffer = new char[ n ];
socket.Receive( mybuffer ... );
}

--
Elias
Kevin Frey said:
Hello,

I want to do the equivalent of this in Managed C++:

void MyFunc( int n )
{
Byte[ ] mybuffer = new Byte[ n ];

socket.Receive( mybuffer ... );
}

The key point here is that I want to allocate an array of Bytes
according
to
a variable, not a constant-sized array.

The compiler complains that n is not constant. For our TCP communications
(porting from unmanaged C++) all of our packets are length prefixed, so we
read the length, then allocate the right amount of space for the buffer.

Totally OT...

How do you handle the case of a faked packet with a false length (too
small)? :)


Bo Persson
 

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