Signal code: Address not mapped (1) 我应该如何动态定义一个变量来避免它?

Signal code: Address not mapped (1) how should I define a variable dynamically to avoid it?

我有以下代码 - 它是一个图像过滤器,但在这种情况下我没有使用卷积部分 - 当使用散点时,它因标题错误而失败。我认为错误在接收缓冲区中,但无法弄清楚原因:

#include "utils.h"
#include <string.h>
#include <assert.h>
#include <math.h>
#include <stdint.h>
#include <mpi.h>

typedef char pixel_t[3];

typedef struct image_s {
    pixel_t *data_in, 
            *data_out;
    size_t   width, height;
} image_t;

#define DIE_MPI(msg) do {                       \
fprintf(stderr, "FAILURE: %s\n", msg);  \
exit(EXIT_FAILURE);                     \
} while(0)

char *g_progname;
static inline void usage(void) {
    fprintf(stderr, "USAGE: %s [filename [x y]]\n", g_progname);
    exit(0);
}

void img_load(const char* filename, const size_t width, const size_t height, image_t* img)
{
    FILE* fsrc = fopen(filename, "r");
    if (!fsrc) 
        FATAL("fopen");

    fseek(fsrc, 0, SEEK_END);
    size_t sz = ftell(fsrc);
    fseek(fsrc, 0, SEEK_SET);

    img->data_in  = XMALLOC(sz);
    img->data_out = XMALLOC(sz);
    img->width    = width;
    img->height   = height;

    memset(img->data_in,  0UL, sz);
    memset(img->data_out, 0UL, sz);

    size_t n_bytes = fread(img->data_in, 1, sz, fsrc);
    printf("Read %u. Expected %u bytes\n", n_bytes, sz);
    if (n_bytes < sz) {
        fprintf(stderr, "Read fewer bytes than expected: %u/%u\n", n_bytes, sz);
    }

    if (feof(fsrc)) {
        printf("Finished reading %s\n", filename);
        clearerr(fsrc);
    }

    fclose(fsrc);
}

int main(int argc, char* argv[])
{
    int err = MPI_Init(&argc, &argv);
    if (err) DIE_MPI("MPI_Init");

    int size = 0;
    err = MPI_Comm_size(MPI_COMM_WORLD, &size);
    if (err) DIE_MPI("MPI_Comm_size");

    int rank = 0;
    err = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    if (err) DIE_MPI("MPI_Comm_rank");

    uint32_t X        = 512, 
             Y        = 512;
    char*    filename = NULL;
    g_progname        = argv[0]; 
    switch(argc) {
        case 4:
                X        = strtoul(argv[3], NULL, 0);
                Y        = strtoul(argv[2], NULL, 0);
        case 2:
                filename = argv[1];
                break;
        case 1:
                filename = "lena_color.data";
                X        = 512;
                Y        = 512;
                break;
        default:
                usage();
    }
    
    pixel_t *bitmap_in;
    bitmap_in = malloc(X * Y * sizeof(pixel_t));
    image_t img;
    if(rank==0)
    {
        printf("Processing %s...\n", filename);
        img_load(filename, X, Y, &img);
    }
    
    MPI_Scatter(img, X*Y*sizeof(pixel_t), MPI_CHAR, bitmap_in, X*Y*sizeof(pixel_t), MPI_CHAR, 0, MPI_COMM_WORLD);

    err = MPI_Finalize();
    if (err) DIE("MPI_Finalize");
    
    return err;
    }

综上所述,我的发送缓冲区或接收缓冲区存在映射问题。我已经尝试在接收缓冲区上分配的内存中添加更多 space 但它没有解决它。另一个原因可能是我发送的内存比结构 image_t 中分配的内存多,但我不这么认为,因为它应该是我尝试发送的大小的两倍。

也许我的结论遗漏了一些东西,因为我还在学习 c 和 MPI。

错误不在变量本身的定义上,而是在MPI_Scatter()中对变量的调用,MPI_Scatter中不需要加上"&var" (), 你应该只输入 "var"