简体   繁体   中英

Strange errors in Visual C++ :: 'malloc' : function does not take 1 arguments

Error 38 error C2660: 'malloc' : function does not take 1 arguments C:\VolumeRenderer\render.cpp 296 1 VolumeRenderer
Error 39 error C2660: 'malloc' : function does not take 1 arguments C:\VolumeRenderer\render.cpp 412 1 VolumeRenderer
Error 40 error C2660: 'malloc' : function does not take 1 arguments C:\VolumeRenderer\render.cpp 414 1 VolumeRenderer
Error 41 error C2660: 'read_den' : function does not take 4 arguments C:\VolumeRenderer\render.cpp 506 1 VolumeRenderer

My all malloc sections are like this:

/* allocate space for the raw data */
density_size = BRAIN_XLEN * BRAIN_YLEN * BRAIN_ZLEN;
density = (unsigned char*)malloc(density_size);
if (density == NULL) {
  fprintf(stderr, "out of memory\n");
  exit(1);
}

regarding read_den (last error) . read_den does take 4 parameters. You can see the function prototype & its corresponding call here:

  unsigned char *read_den(char *filename,int *xptr,int *yptr,int *zptr)// function prototype
  src_volume = read_den(src_file, &src_xlen, &src_ylen, &src_zlen);// function call

Is it my code or the errors that are absurd. How to rectify them?

EDIT: can some one comment on the last error because. I'm unable to justify it.

EDIT2: When I changed the file extension from *.cpp to *.c then all errors are gone. So, I think it has something to do with C & C++.

Wild guess: you used malloc incorrectly somewhere else, passing two arguments instead of one. This would result in an implicit declaration.

Try compiling with all warnings enabled and see if anything comes up.


Update: You could also put #include <stlib.h> as the very first line in your source file, so that any potential implicit declaration would be flagged as an error.

Maybe you shadow the real malloc function somewhere in your code. In gcc you can use the -Wshadow flag to test. I am sure there is something similiar in Visual Studio.

Edit: I read the second part you added and the errors seem to come indeed from incompatibilities between C and C++. Depending on the size of your project, this might be tedious work to sort out. I suggest, that you use the "extern" keyword to link your new C++ code to your working C code.

Example:

#include <stdio.h>
#include <stdlib.h>

int cplusplus_function(int a); 

main(){
    printf("c code\n");

    int* a = malloc(sizeof(int)); //just proving that this is indeed C code.
                                  //this would not compile with a C++ compiler

    cplusplus_function(5);
    return 0;
}

and a C++ function with the "extern" keyword:

#include <iostream>

extern "C" void cplusplus_function(int);

void cplusplus_function(int a){
    std::cout << "c++ code" << std::endl;
}

Now you can compile the files separately and link them together.

  1. Do you have #include <stdlib.h> ?
  2. Try generating the preprocessed output (with MSVC, this is done with the /E option). Look through the resulting output for malloc and read_den declarations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM