user1964417
user1964417

Reputation: 1705

pass array as parameters in function

I have a function need to pass an array and the array value will be modified in that function. I implement it as follows, but the values in the array does not change after the function is called. Could any one help me with this? Thanks!

This is where I call the function, socket.Receive(sender,buffer,sizeof(buffer)). The variable buffer does not get the right value.

while ( true )
{
    Address sender;
    unsigned char buffer[256];
    int bytes_read = socket.Receive( sender, buffer, sizeof( buffer) );
    if ( !bytes_read )
        break;
    printf("%d\n",buffer[92]);

}

This is the code of function socket.Receive()

int Socket::Receive( Address & sender, 
                    void*  data, 
                    int size )
{
        unsigned char packet_data[256];

        unsigned int max_packet_size = 
            sizeof( packet_data );

#if PLATFORM == PLATFORM_WINDOWS
        typedef int socklen_t;
#endif

        sockaddr_in from;    
        socklen_t fromLength = sizeof( from );

        size = recvfrom( handle, 
            (char*)packet_data, 
            max_packet_size,
            0, 
            (sockaddr*)&from, 
            &fromLength );

        data = packet_data;
        unsigned int from_address = ntohl( from.sin_addr.s_addr );
        unsigned int from_port = ntohs( from.sin_port );

    return size;
}

Upvotes: 0

Views: 113

Answers (3)

David G
David G

Reputation: 96790

data = packet_data;

On this line you're assigning to data but the parameter that Receive takes is a local copy of a pointer. It needs to take a reference if you are trying to mutate the argument being passed:

int Socket::Receive( Address & sender, 
                    void*&  data, // <-- reference to a pointer
                    int size )

And like @DrewDormann said, the assignment doesn't copy the data from packet_data to data, but rather changes the address the data points to to the address of packet_data. The consequence is that when you attempt to derefrerence buffer later on, you will get Undefined Behavior for accessing an already destroyed object.

Instead, should use memcpy to copy the data:

#include <cstring>
std::memcpy(data, packet_data, size);

Upvotes: 2

tsragravorogh
tsragravorogh

Reputation: 3153

I think the problem is because you declare "unsigned char packet_data[256];" on the stack of your function and you do not copy from it the bytes to your "data" parameter. Use memcpy to copy contents of "packet_data" into "data". What you are doing instead you are assigning "data" to "packet_data", when you do that "data" will point to the same memory as "packet_data" does. But as soon as the function exits, "packet_data" is being freed, so it's contents become garbage. Hence you do not see the data in your "data" pointer. Hope this was helpful.

Upvotes: 1

Drew Dormann
Drew Dormann

Reputation: 63704

You have several bugs, but the problem you mention stems from this line.

        data = packet_data;

This line does not copy the entire packet_data array. It only overwrites the pointer data with a new address.

Upvotes: 2

Related Questions