根据
最好不要在C中强制转换malloc,因为如果对malloc的返回进行强制转换,那么将被标记的错误将被隐藏,从而导致很难找到错误。另外,在维护过程中,如果指针的类型发生了变化,但是转换没有改变,那么仍然很难找到错误。大多数有经验的程序员选择的方法是:
p = malloc ( n * sizeof *p );
malloc不需要强制转换,因为不需要强制转换,而不是使用 size of ( type )来确定块的大小,而是使用size of *ptr。通过取消引用指针并获取指针的大小,可以给出适当的值,而不必担心如果指针的类型发生变化就会修改分配请求。
但是它不会在C++中编译。为什么?
如
这是我第一次使用Eclipse编写C代码,所以答案可能非常简单。我还在计算Eclipse已经为编译、构建和链接做了适当的设置。这是运行在Windows 7 64位计算机上。
在我的C代码中,似乎每当我使用malloc()甚至printf()函数时,它都会将红色的行放在下面。只是函数加下划线,而不是浇铸,甚至参数。
下面是代码行:
#include <stdlib.h>
#include <stdio.h>
int* list; //This is a global variable
int main(){
//...inside a function
我对malloc不太了解,我知道也有类似的问题,但我还是想问。
当我编译代码时,我遇到了以下错误
scheduler.c: In function 'char* addprocess(char*, FILE*)':
scheduler.c:127:60: error: invalid conversion from 'void*' to 'process*'
scheduler.c:135:38: error: invalid conversion from 'void*' to 'char*'
scheduler
我正在编写一个程序,该程序使用malloc来创建全局数组。我尝试了一下(当然是在main()之外): int *arr=malloc(5*sizeof(int)); 在GCC编译器中,闪现了以下错误: main.c:4:10: error: initializer element is not constant
int *arr=(int *)malloc(5*sizeof(int));
^ 我在Dev-C++上尝试过,但没有这样的错误中断编译。这个错误是什么意思,为什么是针对GCC的?
typedef struct node node;
// LinkedList data structure to hold characters.
struct node {
char character;
node *link;
};
后来我试着:
node *tmp = malloc(sizeof (node));
在我的文本编辑器中,我发现了一个错误:
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
er
所以我正在学习C的教程,并且我被困在了结构体上,因为它们使用了C++函数,而这个函数在我的编译器(Visual Studio my 10.0)中似乎不太好用。因此,我完全按照说明操作,我可以编译C语言,除了在这段特定的代码中,它给了我一个错误(代码直接取自教程网站):
#include <stdio.h>
#include <stdlib.h>
struct node {
int x;
struct node *next;
};
int main()
{
/* This won't change, or we would lose the l
C函数malloc()在stdlib.h下定义。
如果我们不包含这个文件,它应该会给出一个错误,但是这个代码工作得很好,但有一点警告。
我的问题是,如果malloc()在没有这个头文件的情况下工作,那么为什么我们需要包含它呢?请帮助澄清我的概念。
# include <stdio.h>
int main()
{
int a, b, *p;
p = (int*)malloc(sizeof(int)*5);
for(a=0;a<5;a++)p[a]=a*9;
for(b=0;b<5;b++)printf("%d ",p[
可能重复:
我试图使用指针在memmory上分配一个矩阵,但是我一直收到错误消息:
从“ *”到“int**”的转换无效
{124}错误:从'void*‘到’int*‘的转换无效
这是我的代码,我看不出我做错了什么.这种“空虚”的转换对我来说毫无意义..。
int i,j;
int **a;
int c = 2;
/* Form the matrix */
a = malloc((nxy+1)*sizeof(int *));
for (i=0;i<=nxy;i++)
a[i] = malloc((nxy+1)*size
我试图计算NxN矩阵的行列式。这段代码在我尝试动态分配内存的行中出现了一个错误。
错误:不能将"int“类型的值分配给"float *”类型的实体
错误:不能将"int“类型的值分配给"float **”类型的实体。
double CalcDeterminant( float **mat, int order)
{
float **minor;
unsigned short i;
float det = 0;
// order must be >= 0
// stop the recursion when matr
在头文件中声明静态函数时,我遇到了这个问题。假设我有这个文件"test.cpp":
#include <cstdlib>
#include "funcs.hpp"
...do some operations...
void *ptr = my_malloc(size);
...do some operations...
"funcs.hpp“文件包含:
#include <ctime>
#include "../my_allocator.hpp" // my_malloc is defined here as
我正在尝试在ipython notebook中使用JPype,以便使用JIDT (我不知道Java)。我按照这里的说明操作:。
我导入jpype并使用以下命令启动java虚拟机:
from jpype import *
startJVM(getDefaultJVMPath())
这会导致ipython notebook内核由于以下错误而崩溃:
python(490,0x7fff7a99e310) malloc: *** error for object 0x104d90720: pointer being freed was not allocated
*** set a breakpoint
最简单的例子是下面的程序,链接到OS上的libmx (-lmx) (支持MATLAB ),用g++4.9.1从macports编译:
#include <sstream>
int main()
{
std::ostringstream ostr;
// ostr << " "; // if I un-comment this line no more malloc error
}
我意识到在ostr销毁时(就在退出程序之前),我得到了一个malloc运行时错误的形式:
testcpp(5362,0x7fff7d2af300) mallo
我有以下简单的代码:
第一次使用这个结构时,f运行得很好,但是我不能为n使用malloc --我得到一个错误,它是空的*不能被赋值给myValue*。我知道我不应该使用malloc,那么我该怎么做呢?怎么了?
确切的错误是:
a value of type "void *" cannot be assigned to an entity of time "myValues *"
#include <stdio.h>
#include <stdlib.h>
typedef struct values
{
int a;
char c;
vo