Recieving C socket data to allocated 2D array

dynamic 2d string array in c
c dynamic memory allocation 2d array
dynamic multidimensional arrays
deallocate 2d array c
2d array in c dynamically
dynamically allocated array c
free 2d array c
creating 2d array in c

I am trying to populate an array I allocated with data recieved from a socket, but am unable to make it work properly. When i define and get data through:

uint16_t data[2048];
recv(network_socket, &data, sizeof(data), 0);

the data is recieved properly, but when I try to do it through an allocated array:

int i;
int N = 3;
int M = 2048;
int **matrix; 

matrix = (int **)calloc(N, sizeof(int *));

for (i=0; i<N; i++) {
    matrix[i] = (int *)calloc(M, sizeof(uint16_t));

for (i=0; i<N; i++) {
    recv(network_socket, matrix[i], sizeof(data), 0);

I am sensing things go wrong at the final part. As far as I understand matrix[i] at the end gets the pointer to the beginning of row i and should work for recv() but that might be where I'm going wrong here.

EDIT: When I try to printf("Number: %" PRIu16 "\n", matrix[a][b]); I get some whacko large number in the second part, wheras printf("Number: %" PRIu16 "\n", data[a]); works fine in the first.

You tell in your first code you want an array of 16bit unsigned integers and receive them. All is good.

In your second code you want an array of ints which probably aren’t 16bit on your platform. Then you allocate an amount of memory to them based on them being 16bit values and read an undisclosed amount of data in.

Then you try to access them as ints which obviously will cause two uint16_t values being combined into one int if your system is 32bit, or even four on a 64bit system.

Fix your code to use uint16_t as in the first one and it’ll actually know how to handle the 16bit data. Your current code will even have undefined behavior since you try to access memory outside the allocated range.

C Sockets -- Write/Read a 2D Array, Recieving C socket data to allocated 2D array You tell in your first code you want an array of 16bit unsigned integers and receive them. 2-Dimensional Array 1. Using Single Pointer. In this approach, we simply allocate one large block of memory of size M*N dynamically and assign it to pointer. Then we can use pointer arithmetic to index 2D array.

How to send the dynamically allocated two dimensional character , I'm trying to send a 2D int array across a network with C sockets. what data is the reciver getting? Cancel Save I allocate memory for the array dynamically server-side, if that matters. You're sending pointers, not data. The last call would be returning an amount of data that is smaller than the chunk size (unless the whole response size is a multiple of the CHUNK_SIZE). Now in the last call to recv, recv would block till it gets CHUNK_SIZE amount of data or a default timeout occurs which can be from 30 seconds to 1 minute.

int **matrix; 

matrix = (int **)calloc(N, sizeof(int *));

The code doesn't work since the above is not a 2D array, but rather a segmented, pointer-based look-up table. As such, it is not in the slightest compatible with arrays.

There is no reason why you need to have such a look-up table here. The problem likely originates in confusion about how to Correctly allocating multi-dimensional arrays.

How to dynamically allocate a 2D array in C, allocated two dimensional character array through C++ UDP socket. to send data to server , due to the dynamic memory allocation, how to  So my next question is how big do you expect your 2D array to be? since the UDP packet has a maximum size of 65535 bytes of payload (data) per packet. if your 2d array is always going to be smaller than that number, then using the send method, all you can do is copy all the data into a single array.

Sort an array using socket programming in C, In the following examples, we have considered 'r' as number of rows, 'c' as number of columns and we created a 2D array with r = 3, c = 4 and following values. There are two ways to pass dynamic 2D array to a function: 1) Passing array as pointer to pointer( int **arr) Using new operator we can dynamically allocate memory at runtime for the array. New operator returns the address of the space allocated .This method Passes array reference as double pointer to the function along with rows and columns.

Two dimensional (2D) arrays in C programming with example, Sort an array using socket programming in C. Given an array of unsorted positive integer, sort the given array using the Socket Receive a reply from the server. i use a UDP-socket for sending/receiving objects. I serialize the objects into a byte array send it and receive it with a client. But before i can receive i have to allocate a byte array. I must do it before and then handle the Socket.Receive()-methode the array. But my objects have a variable length. How can i discover the array-size? Thank you

Malloc Function, The two dimensional (2D) array in C programming is also known as matrix. A matrix can be For now don't worry how to initialize a two dimensional array, we will discuss that part later. This program How to store user input data into 2D array. We can How do I assign a 2-D array to a pointer ? I am getting error. Reply. The recv () function receives data on a socket with descriptor socket and stores it in a buffer. The recv () call applies only to connected sockets. The socket descriptor. The pointer to the buffer that receives the data. The length in bytes of the buffer pointed to by the buf parameter. If the MSG_CONNTERM flag is set, the length of the buffer

  • How things go wrong? What did you expect to happen? Please read How to Ask and minimal reproducible example.
  • I think that sizeof(data) should that not be M * sizeof(uint16_t)? Have you tried what you wrote?
  • Why is matrix defined as int** and then you allocate based on uint16_t?
  • is recv expecting the size of the memory (i.e. 2048 * sizeof(uint16_t)) or is it expecting the number of elements in the array (i.e. only 2048)? If it is the later then it could be trying to write past the end of the memory that you allocated for it.
  • Does the real code also completely ignore recv()'s return value?
  • Thanks for your help. It turns out it was my poor understading of pointers that caused issues!
  • Thanks for replying, I will try to structure my code into functions like these. However the tricky part I find is the use of the ´recv( )´ function here
  • Why would they need to use this look-up table for in the first place? Encapsulating it just hides the crap underneath the carpet. Do it proper instead, as explained here.
  • if you are after unsigned int use unsigned int. Printing a signed int via %u invokes undefined behaviour.
  • Even if matrix[x][y] would be an unsigned short passing it to the (variadic) printf() function it would get promoted to int.
  • @Lunden - I acknowledge your concern. But when having to perform such pieces of code multiple times in an application, I find it much more readable to encapsulate once, rather than duplicate the messy code necessary many times. In any case, no arguments with what I think is your sentiment, and have cited your linked answer in mine for its very detailed explanation. Thank you
  • The problem that lead me to this solution (which should be with uint16_ts as pointed out above) was that I have an application that sends data in buckets of 2048, and I need to store this data in some handy way with previously sent data so that I can fprintf( ) all contents to a .dat file for gnu-plot. I will read through your link to see if I can find a better approach! Thanks for answering