C 如何为x64编译此文件

C 如何为x64编译此文件,c,pointers,struct,malloc,64-bit,C,Pointers,Struct,Malloc,64 Bit,这在x86中可以很好地编译,但是当我在x64配置中使用它时,当我尝试访问x和y变量时,它们没有地址?是否需要某种填充以与较大的地址对齐?使用MSVC #define ARR_SIZE 25 typedef struct { unsigned int x; unsigned int y; }Stuff; void allocateArray(Stuff *stuffArr) { Stuff *stuff = malloc(sizeof (Stuff) * ARR_SI

这在x86中可以很好地编译,但是当我在x64配置中使用它时,当我尝试访问x和y变量时,它们没有地址?是否需要某种填充以与较大的地址对齐?使用MSVC

#define ARR_SIZE 25

typedef struct {
    unsigned int x;
    unsigned int y;
}Stuff;

void allocateArray(Stuff *stuffArr) {

    Stuff *stuff = malloc(sizeof (Stuff) * ARR_SIZE);

    for (int i = 0; i < ARR_SIZE; i++) {
        (*(stuff + i)) = (Stuff) { i, i + i };
    }

    for (int i = 0; i < ARR_SIZE; i++) {
        printf("%d : %d\n", (stuff + i)->x, (stuff + i)->y);
    }

    stuffArr = stuff;
}

void deallocateArray(Stuff *stuffArr) {
    free(stuffArr);
}

int main(){
    Stuff * stuff = NULL;

    allocateArray(stuff);
    deallocateArray(stuff);

    return 0;
}

当您在AllocaterRay中传递内容时,您创建了一个局部变量,当您将其放置在函数末尾时,main中的变量不会更新

这应该是可行的,您刚刚在AllocaterRay函数中丢失了指针

#define ARR_SIZE 25

typedef struct {
    unsigned int x;
    unsigned int y;
}Stuff;

Stuff *allocateArray() {

   Stuff *stuff = malloc(sizeof (Stuff) * ARR_SIZE);

   for (int i = 0; i < ARR_SIZE; i++) {
       (*(stuff + i)) = (Stuff) { i, i + i };
   }

   for (int i = 0; i < ARR_SIZE; i++) {
       printf("%d : %d\n", (stuff + i)->x, (stuff + i)->y);
   }

   return stuff;
}

void deallocateArray(Stuff *stuffArr) {
  free(stuffArr);
}

int main(){
  Stuff * stuff = NULL;

  stuff = allocateArray();
  deallocateArray(stuff);

  return 0;
}

正如用户3386109所说,代码不正确。可能您希望allocateArray函数在按值传递指针时返回分配的指针,这样main中的变量内容就不会被更新

你可以:

将AllocaterRay签名更改为作废AllocaterRayStuff**stuffArr 将AllocaterRay签名更改为Stuff*AllocaterRay 我想第二个会更地道、更清晰

我会这样写:

Stuff *allocateArray(size_t count) {
    Stuff *stuff = (Stuff *) malloc(sizeof (Stuff) * count);

    if (! stuff)
        return NULL;

    for (int i = 0; i < count; i++) {
        stuff[i].x = i;
        stuff[i].y = 2 * i;

        printf("%d : %d\n", stuff[i].x, stuff[i].y);
    }

    return stuff;
}

void deallocateArray(Stuff *stuffArr) {
    if (stuffArr)
        free(stuffArr);
}

int main(){
    Stuff * stuff = allocateArray(ARR_SIZE);
    deallocateArray(stuff);

    return 0;
}

我将您的代码复制并粘贴到Visual Studio 2015中,并在x86和x64中编译,两次都得到了完全相同的输出,但正如user3386109所说,您实际上并没有更改main中的变量内容

您还可以使用数组索引,而不是像这样向指针添加整数

for (int i = 0; i < ARR_SIZE; i++) {
    stuff[i] = (Stuff) { i, i + i };
}

for (int i = 0; i < ARR_SIZE; i++) {
    printf("%d : %d\n", stuff[i].x, stuff[i].y);
}

好问题是它在x86中是如何工作的。这是未定义的行为。 试试这个:

#define ARR_SIZE 25
#include  "stdlib.h"
#include  "stdio.h"
typedef struct {
    unsigned int x;
    unsigned int y;
}Stuff;

void * allocateArray(Stuff *stuffArr) { //this row

    Stuff *stuff = (Stuff*)malloc(sizeof (Stuff) * ARR_SIZE);

    for (int i = 0; i < ARR_SIZE; i++) {
        (*(stuff + i)) = (Stuff) { i, i + i };
    }

    for (int i = 0; i < ARR_SIZE; i++) {
        printf("%d : %d\n", (stuff + i)->x, (stuff + i)->y);
    }

    stuffArr = stuff;
    return stuff; //this row
}

void deallocateArray(Stuff *stuffArr) {
    free(stuffArr);
}

int main(){
    Stuff * stuff = NULL;
    printf("%d\n",stuff);
    stuff=(Stuff*)allocateArray(stuff); ///this row
    printf("%p\n",stuff);
    deallocateArray(stuff);
    printf("%p\n",stuff);
    return 0;
}

这两种架构都不适用。行stuffArr=stuff;更改stuffArr的本地副本。它对main中的变量stuff没有影响。而且,*stuff+i与stuff[i]相同,stuff+i->x只是stuff[i]。这是一个常见问题解答错误。让我看看我是否能找到一个副本。显然我们没有这个的标准副本。。。我们可能应该创建一个,因为这是一个很常见的bug。我只是不知道这是怎么回事,我修改了它以去掉愚蠢的指针,它不会编译为x64,而您缺少include。我将您的代码从pastebin复制到visual studio中,设置了完全相同的项目设置,并获得了与您相同的结果,但后来我注意到您缺少stdlib.h,在中添加了它,然后它在x64中运行良好。我不知道为什么它在没有stdlib.h的情况下在x86中工作。这已经困扰了我一天半。。非常感谢!!!没问题!如果让我猜一下为什么它在x86中工作,Windows/MSVC可能有一个默认的32位实现内置于malloc和free等函数中,但没有默认的64位实现。一般来说,在进行动态内存分配时,请始终记住包含stdlib.h。我只是不知道该处理是什么,我修改了它以去掉愚蠢的指针,它不会编译为x64,我只是不知道该处理是什么,我修改了它以去掉愚蠢的指针,它不会编译为x64和