Home > Blockchain >  Pass 2D array row address to a 1D array for future use
Pass 2D array row address to a 1D array for future use

Time:09-17

I'm trying to store the row address of an array. It seems as though the addresses are passed correctly but they don't seem to refer to the values stored in the array.

I want to save the addresses so I can then use them in a Python application.

#include <stdio.h>
#include "func.h"

int main(int argc, const char * argv[]) {
    
    uint64_t pkt[3];
    func(pkt);
    
    printf ("The address in main is %p\n", pkt[0]);
    printf ("The address in main is %p\n", pkt[1]);
    printf ("The address in main is %p\n", pkt[2]);
    
    uint8_t *ptr0 = (uint8_t*)pkt[0];
    printf ("The address in main is %p\n", ptr0);
    printf("%d\n", ptr0[0]);
    printf("%d\n", ptr0[1]);
    printf("%d\n", ptr0[2]);
    
    uint8_t *ptr1 = (uint8_t*)pkt[1];
    printf ("The address in main is %p\n", ptr1);
    printf("%d\n", ptr1[0]);
    printf("%d\n", ptr1[1]);
    printf("%d\n", ptr1[2]);
    
    uint8_t *ptr2 = (uint8_t*)pkt[2];
    printf ("The address in main is %p\n", ptr2);
    printf("%d\n", ptr2[0]);
    printf("%d\n", ptr2[1]);
    printf("%d\n", ptr2[2]);
    
    return 0;
}
#include "func.h"


void func(uint64_t* pkt)
{
    uint8_t matrix[3][3] = { {3 , 7 , 4} ,
        {1, 2 , 8} ,
        {8 , 3, 2} } ;
    int i = 0;
    
    for (i = 0; i < 3; i  )
    {
        pkt[i] = (uintptr_t)&matrix[i];
        printf("The address is %p\n", &matrix[i]);
        printf("The address is %p\n", pkt[i]);
    }
    
}

CodePudding user response:

I don't agree with you. It works as expected. However, there is no guarantee, even kinda bad programming, the address of matrix is valid after returning from the function. So you shouldn't use the addresses of things defined in local functions if you don't allocate the addresses in heap.

The address is 0x7ffcfeae7f6f
The address is 0x7ffcfeae7f6f
The address is 0x7ffcfeae7f72
The address is 0x7ffcfeae7f72
The address is 0x7ffcfeae7f75
The address is 0x7ffcfeae7f75
The address in main is 0x7ffcfeae7f6f
The address in main is 0x7ffcfeae7f72
The address in main is 0x7ffcfeae7f75
The address in main is 0x7ffcfeae7f6f
3
7
4
The address in main is 0x7ffcfeae7f72
1
2
8
The address in main is 0x7ffcfeae7f75
8
3
2

CodePudding user response:

The array matrix declared in the function func has automatic storage duration. It means that after exiting the function it will not be alive. So an attempt to access the array after exiting the function invokes undefined behavior.

You could declare the array with static storage duration like

static uint8_t matrix[3][3] = { {3 , 7 , 4} ,
    {1, 2 , 8} ,
    {8 , 3, 2} } ;

In this case it will be alive after exiting the function and you will may access its elements indirectly through a pointer.

Pay attention to that calls of printf like this

printf ("The address in main is %p\n", pkt[0]);

use incorrect conversion specifier %p that again can invoke undefined behavior.

Your program can look the following way.

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

void func( uintptr_t *pkt )
{
    static uint8_t matrix[3][3] = 
    { 
        { 3, 7, 4 },
        { 1, 2, 8 },
        { 8, 3, 2 } 
    };

    for (int i = 0; i < 3; i  )
    {
        pkt[i] = ( uintptr_t )matrix[i];
        printf( "The address is %p\n", ( void * )matrix[i] );
        printf( "The address is %" PRIuPTR "\n", pkt[i] );
    }
    
    putchar( '\n' );
}

int main(void) 
{
    uintptr_t pkt[3];
    func( pkt );
    
    printf( "The address in main is %" PRIuPTR "\n", pkt[0] );
    printf( "The address in main is %" PRIuPTR "\n", pkt[1] );
    printf( "The address in main is %" PRIuPTR "\n", pkt[2] );
    
    uint8_t *ptr0 = ( uint8_t * )pkt[0];
    printf( "The address in main is %p\n", ( void * )ptr0 );
    printf( "%u\n", ptr0[0] );
    printf( "%u\n", ptr0[1] );
    printf( "%u\n", ptr0[2] );
    
    uint8_t *ptr1 = ( uint8_t * )pkt[1];
    printf( "The address in main is %p\n", ( void * )ptr1 );
    printf( "%u\n", ptr1[0] );
    printf( "%u\n", ptr1[1] );
    printf( "%u\n", ptr1[2] );
    
    uint8_t *ptr2 = ( uint8_t * )pkt[2];
    printf( "The address in main is %p\n", ( void * )ptr2 );
    printf( "%u\n", ptr2[0] );
    printf( "%u\n", ptr2[1] );
    printf( "%u\n", ptr2[2] );

    return 0;
}

The program output night be

The address is 0x5578e37f7010
The address is 93977701216272
The address is 0x5578e37f7013
The address is 93977701216275
The address is 0x5578e37f7016
The address is 93977701216278

The address in main is 93977701216272
The address in main is 93977701216275
The address in main is 93977701216278
The address in main is 0x5578e37f7010
3
7
4
The address in main is 0x5578e37f7013
1
2
8
The address in main is 0x5578e37f7016
8
3
2
  • Related