malloc的编译错误:类型&QUOT值; INT"不能用于初始化int类型的实体(*)[30] [英] Malloc compile error: a value of type "int" cannot be used to initialize an entity of type int (*)[30]

查看:1068
本文介绍了malloc的编译错误:类型&QUOT值; INT"不能用于初始化int类型的实体(*)[30]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须尝试通过现在这样做的18种方法。我真的需要帮助,无论我做什么,我得到类似这样的一个错误。

I must have tried 20 ways of doing this by now. I really need help, no matter what I do i get a error similar to this one.

a value of type "int" cannot be used to initialize an entity of type "int (*)[30]"

即。这将让我这样的错误

i.e. this will get me such an error

int(*array)[160] = malloc((sizeof *array) * 10);

和做这样的事情。

int** Make2DintArray(int arraySizeX, int arraySizeY) {
    int** theArray;
    theArray = (int**) malloc(arraySizeX*sizeof(int*));
    int i;
    for (i = 0; i < arraySizeX; i++)
    {
        theArray[i] = (int*) malloc(arraySizeY*sizeof(int));
    }
    return theArray;
}

将得到我这个

"void *(size_t)" in "memory.c" at line 239 and: "int()" 

没有任何人有如何成功分配int的2dArray的解决方案[160] [10]

does anyone have a solution for how to successful allocate a 2dArray of int[160][10]

推荐答案

这两种编译对我罚款。当你忘了第一个错误是常见的 的#include&LT;文件stdlib.h&GT; 之前使用内声明的函数表示,相同(如的malloc(为size_t)),这我没有的的忘记做。

Both of these compile fine for me. The first error is common when you forget to #include <stdlib.h> prior to using functions declared within said-same (such as malloc(size_t)), which I did not forget to do.

C有一些有趣的编译时的行为,其中援引从未有过(无论原型定义的的实施)看到了功能的能力。当遇到这样的电话,C假定该函数是:

C has some interesting compile-time behaviors, among them the ability to invoke a function that has never been seen before (neither prototype definition nor implementation). Upon encountering such a call, C assumes the function is:


  • 东西返回 INT

  • 取一个未知数量的参数,所以调用者可以通过为所欲为(包括错误的东西)。

例如,函数被隐含假定为以下形式:

Eg., the function is implicitly assumed to be of the form:

int func();

通常你甚至不会注意到,节省您的编译器报告东西的效果警告:

Often you won't even notice, save for warnings from your compiler that report something to the effect of:

Warning: implicit declaration of `func` assumed to return `int`

如果你是上的球,你有你的警告级别启用警告-AS-错误出现,并会抓住这一点。

and if you're on-the-ball, you have your warning levels turned up with warnings-as-errors enabled and will catch this.

但如果你不这样做?如果事从函数返回不能什么样的内容,再由数据大小在执行psented $ P $ INT ?如果,例如, INT 为32位,但数据指针是64位?例如,让我们假设的char * get_str()在你的的,包括和.c文件实现了一些头文件中声明你编译和程序链接,看起来像这样:

But what if you don't? And what if the "thing" returned from the function cannot be content-represented by the data size in an implementation int ? What if, for example, int were 32-bit, but data pointers were 64-bit? For example, lets assume char *get_str() is declared in some header file you're not including, and implemented in a .c file you compile and link with your program, that looks like this:

#include <stdio.h>

// Note: NO prototype for get_str
int main()
{
    char *s = get_str();
    printf("String: %s\n", s);
    return 0;
}

那么,编译器应该吐,告诉你, INT 的char * 不兼容(后不久向您发出警告 get_str 假定返回 INT )。但是,如果你强迫编译器的手由什么的告诉的它做一个的char * 这样或那样:

Well, the compiler should puke, telling you that int and char* are not compatible (shortly after it warns you get_str is assumed to return int). But what if you force the compiler's hand by telling it to make a char* one way or another:

#include <stdio.h>

// Note: NO prototype for get_str
int main()
{
    char *s = (char*)get_str(); // NOTE: added cast
    printf("String: %s\n", s);
    return 0;
}    

现在,没有警告-AS-错误启用后,你会得到一个隐含的声明警告,多数民众赞成它。在code编译。但它的运行?如果的sizeof(INT)!= 的sizeof(字符*)(32位和64位)可能不。从 get_str 返回的值是一个64位的指针,但是的主叫的是假设只有32位被返回,那么它迫使到64比特指针。总之,剧组隐藏错误,并打开未定义行为潘多拉的盒子。

Now, without warnings-as-errors enabled, you'll get a implicit declaration warning, and thats it. The code will compile. But will it run ? If sizeof(int) != sizeof(char*), (32-bit vs 64-bit) likely not. The value returned from get_str is a 64-bit pointer, but the caller is assuming only 32-bits is returned, then forcing it to a 64-bit pointer. In short, the cast has hidden the error and opened pandora's box of undefined behavior.

那么,如何这一切与你code?如果不包括&LT;&stdlib.h中GT; 编译器不知道是什么的malloc 是。因此,假定它的格式为:

So how does all of this relate to your code? By not including <stdlib.h> the compiler doesn't know what malloc is. So it assumes it is of the form:

int malloc();

然后,结果铸造(INT **)你告诉编译器无论这样来的,使它成为 INT ** 。在链接时, _malloc 被找到(通过名称重整C ++一样没有参数签名),连接好,你的计划是准备党。但是,你的平台上 INT 和数据指针的的大小相同,因此你结束了几个不良后果:

Then, by casting the result to (int**) you're telling the compiler "whatever comes out of this, make it a int**". At link time, _malloc is found (no parameter signature via name mangling like C++), wired up, and your program is ready to party. But on your platform int and data pointers are not the same size, thus you end up with several undesirable consequences:


  • 演员隐藏真正的错误。

  • 一个假的指针从的真正的返回的指针的一半位制造的。

  • 盐在伤口上的一个残酷的剂量,分配的内存泄漏,因为没有有效的指针的任何地方引用它(你刚才毁于只保留它的一半是唯一的一个)。

  • 大概的的不可取的,的如果一个实现编译code将表现出正常的行为,其中的sizeof(int)的== sizeof的(INT **)

  • The cast hides the real error.
  • A bogus pointer is manufactured from half the bits of the real returned pointer.
  • As a cruel dose of salt to the wound, the allocated memory is leaked, as there are no valid pointers anywhere that reference it (you just destroyed the only one by only keeping half of it).
  • Probably the most undesirable, the code will exhibit normal behavior if compiled on an implementation where sizeof(int) == sizeof(int**).

所以,你建立这对32位的Debian中,一切看起来很好。你把你的功课谁建立它自己的64位Mac上的教授,它崩溃,你失败的任务,失败的阶级,辍学的大学生,花未来十年抚摸猫一边看宋飞重播在你妈妈的地下室不知道什么地方出了错。哎哟。

So you build this on your 32-bit Debian box, all looks well. You turn in your homework to the professor who builds it on his 64bit Mac and it crashes, you fail the assignment, fail the class, drop out of college, and spend the next ten years petting the cat while watching Seinfeld reruns in your mom's basement wonder what went wrong. Ouch.

不要把铸像一些银弹。事实并非如此。在C,它是需要的的少往往比人们使用它,如果在​​错误的地方使用,可以隐藏灾难性的错误。如果你发现在你的code地步的东西会不会没有硬铸铁编译的再看看的。除非你是绝对,积极确保投是做正确的事,赔率是它的错误

Don't treat casting like some silver bullet. It isn't. In C, it is needed far less often than people use it, and if used in the wrong place, can hide catastrophic errors. If you find a point in your code where something won't compile without a hard cast, look again. Unless you're absolutely, positively sure the cast is the right thing to do, odds are its wrong.

在这种情况下,躲到了真正的错误,你忘了给足够的信息来编译器知道的malloc 确实

In this case it hid the real error, that you neglected to give enough info to your compiler to know what malloc really does.

这篇关于malloc的编译错误:类型&QUOT值; INT&QUOT;不能用于初始化int类型的实体(*)[30]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆