-1

I would like to know why when calling the function uint_x64_read doesnt work when called using the statement (*func_list_x32_x64[2]) (ptr_64); (Im not getting any warnings nor errors my program just exits) but runs correctly when using (*func_list_x32_x64[2]) (&var_64);

func_ptr.h:

typedef enum {
    FUNC_PTR_SUCCESS = 0,
    FUNC_PTR_FAIL
} FUNC_PTR_STATUS;

typedef FUNC_PTR_STATUS (*func_ptr)(void *);

FUNC_PTR_STATUS uint_x32_read(void *);

FUNC_PTR_STATUS uint_x32_write(void *);

FUNC_PTR_STATUS uint_x64_read(void *);

FUNC_PTR_STATUS uint_x64_write(void *);

main.c file:

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

#include "func_ptr.h"

static func_ptr func_list_x32_x64[] = {
    (func_ptr) uint_x32_read,
    (func_ptr) uint_x32_write,
    (func_ptr) uint_x64_read,
    (func_ptr) uint_x64_write,
    NULL
};



void main() {
    uint8_t usr_option = 0;
    uint32_t var_32 = 0;
    uint32_t *ptr_32;
    uint64_t var_64 = 0;
    uint64_t *ptr_64;

    ptr_32 = &var_32;
    ptr_64 = &var_64;

    while (1) {
        printf("Enter option: ");
        scanf("%d", &usr_option);

        if(usr_option == 0)
            (*func_list_x32_x64[0]) ((void *)ptr_32);

        else if(usr_option == 1)
            (*func_list_x32_x64[1]) ((void *)ptr_32);

        else if(usr_option == 2) {
            (*func_list_x32_x64[2]) (ptr_64);
        }

        else if(usr_option == 3) {
            (*func_list_x32_x64[3]) (ptr_64);
        }

        else
            break;
    }
}

func_ptr.c:

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


#include "func_ptr.h"


/**
 * @def Defines  #UINT_X32_ADDR_VAL as 2000
 */
#define         UINT_X32_ADDR_VAL       (2000u)

/**
 * @def Defines  #UINT_X64_ADDR_VAL as 4000
 */
#define         UINT_X64_ADDR_VAL       (4000u)



FUNC_PTR_STATUS uint_x32_read(void *data_32) {

    *(uint32_t *) data_32 = UINT_X32_ADDR_VAL;
    return FUNC_PTR_SUCCESS;

}



FUNC_PTR_STATUS uint_x64_read(void *data_64) {

    *(uint64_t *) data_64 = 45;
    return FUNC_PTR_SUCCESS;

}


FUNC_PTR_STATUS uint_x32_write(void *data_32) {

    printf("Val:        %d\n", *(uint32_t *) data_32);
    return FUNC_PTR_SUCCESS;

}


FUNC_PTR_STATUS uint_x64_write(void *data_64) {

    printf("Val:        %d\n", *(uint64_t *)data_64);
    return FUNC_PTR_SUCCESS;

}

Edit: Added minimal reproducible code

tonyjosi
  • 717
  • 1
  • 9
  • 17
  • 2
    Post an [MCVE] please. Your excerpt is probably not what was in your program, there is no `var_64` defined variable here... – Jean-Baptiste Yunès Jan 31 '20 at 13:59
  • @VladfromMoscow that was mistake while I wrote the question actual code contains var_64 defined – tonyjosi Jan 31 '20 at 14:05
  • how are you determining your program doesn't work when it has no output? – Chris Turner Jan 31 '20 at 14:10
  • @ChrisTurner its part a of a long src code, the program exits when entering the `uint_x64_read` function – tonyjosi Jan 31 '20 at 14:11
  • 1
    @tonyjosi the code you've provided in the question works as expected - whatever problem within your actual code is not included above – Chris Turner Jan 31 '20 at 14:18
  • Does this answer your question? [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – pdoherty926 Jan 31 '20 at 14:25
  • @pdoherty926, No. – ikegami Jan 31 '20 at 14:26
  • Please give us [MCVE] that code is actually correct. – Jean-Baptiste Yunès Jan 31 '20 at 14:32
  • @Jean-BaptisteYunès I have modified the question with reproducible code, thanks – tonyjosi Jan 31 '20 at 14:36
  • 1
    ALWAYS enable and heed your compiler's warning (e.g. `-Wall -Wextra -pendatic` using `gcc`). It will find [problems](https://pastebin.com/n3qsnysu) that result in *Undefined Behaviour* in your code. – ikegami Jan 31 '20 at 14:44
  • @ikegami the problem is `uint_x32_read`and `uint_x32_write` works perfectly fine, the issue is only with `uint_x64_read` and `uint_x64_write` – tonyjosi Jan 31 '20 at 14:46
  • 1
    The problem is that your code invokes UB. Your comment isn't relevant. – ikegami Jan 31 '20 at 14:47
  • @ikegami I dont know what that means. – tonyjosi Jan 31 '20 at 14:49
  • 1
    Certain code constructs you aren't allowed to use result in *Undefined Behaviour*, which means anything can happen when that code is run. Again, you need to enable your compiler's warnings and heed them. `gcc` identifies two situations that result in UB, and I'd be surprised if other compilers don't identify they too. – ikegami Jan 31 '20 at 14:53
  • 1
    [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/q/2397984/589924) – ikegami Jan 31 '20 at 15:04

1 Answers1

1

Your code suffers from errors that result in Undefined Behaviour.

scanf("%d", &usr_option);

printf("Val:        %d\n", *(uint32_t *)data_32);

printf("Val:        %d\n", *(uint64_t *)data_64);

void main()

should be

#include <inttypes.h>

scanf("%" SCNu8, &usr_option);

printf("Val:        %" PRIu32 "\n", *(uint32_t *)data_32);

printf("Val:        %" PRIu64 "\n", *(uint64_t *)data_64);

int main(void)

ALWAYS enable and heed your compiler's warnings. (I use -Wall -Wextra -pendatic for gcc.)

ikegami
  • 367,544
  • 15
  • 269
  • 518