dcrackel
asked on
Malloc in VS 2005 C++
When the follow code is compiled, the errors:
Error 1 error C3861: 'malloc': identifier not found c:\temp\firtemp\firtemp\fi rtemp.cpp 11
Error 2 error C3861: 'free': identifier not found c:\temp\firtemp\firtemp\fi rtemp.cpp 21
are generated.
What can be done to correct this?
Thank You
Error 1 error C3861: 'malloc': identifier not found c:\temp\firtemp\firtemp\fi
Error 2 error C3861: 'free': identifier not found c:\temp\firtemp\firtemp\fi
are generated.
What can be done to correct this?
Thank You
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include "stdafx.h"
int main( void )
{
char *string;
// Allocate space for a path name
string = malloc( 260 );
// In a C++ file, explicitly cast malloc's return. For example,
// string = (char *)malloc( _MAX_PATH );
if( string == NULL )
printf( "Insufficient memory available\n" );
else
{
printf( "Memory space allocated for path name\n" );
free( string );
printf( "Memory freed\n" );
}
}
What is <malloc.h> ?
ASKER
The compiler, Microsoft Visual C++ 2005
doesn't seem to know what Malloc is? I took that example off MS's web site.
http://msdn.microsoft.com/en-us/library/6ewkz86d.aspx
string = (char *)malloc( 260 );
and
string = malloc( 260 );
yield: Error 1 error C3861: 'malloc': identifier not found
doesn't seem to know what Malloc is? I took that example off MS's web site.
http://msdn.microsoft.com/en-us/library/6ewkz86d.aspx
string = (char *)malloc( 260 );
and
string = malloc( 260 );
yield: Error 1 error C3861: 'malloc': identifier not found
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Program properties, C/C++ , compile as C (instead of C++) /TC - Does work. Thank you for that tip.
Let's say I want to compile this as C++ as this is a simplified example for a larger project.
--No Good --
char *string;
string = malloc( 260 );
--- No Good --
char *string;
string = (char *)malloc( 260 );
Both yield: Error 1 error C3861: 'malloc': identifier not found
Let's say I want to compile this as C++ as this is a simplified example for a larger project.
--No Good --
char *string;
string = malloc( 260 );
--- No Good --
char *string;
string = (char *)malloc( 260 );
Both yield: Error 1 error C3861: 'malloc': identifier not found
That's odd. Try adding
#include <stdlib.h>
and then (if that is not enough)
string = (char *) ::malloc( 260 );
#include <stdlib.h>
and then (if that is not enough)
string = (char *) ::malloc( 260 );
Don't know what to tell you about the error when you recast. That's pretty standard stuff and should be supported by both C and C++ code.
Something else is going on. Recasting the return value from malloc() should work fine.
Kent
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Should work in both cases - the command line is just a quicker way to do that.
ASKER
also to note, it must be a envirment setting or some related issue.
VS 6 has the same issues on my machine as 2005.
Also note: string = malloc( _MAX_PATH );
generates: Error 1 error C2065: '_MAX_PATH' : undeclared identifier
which seems that #include <stdio.h> isn't being linked.
is it possible that all libs are being referanced for some reason or another?
VS 6 has the same issues on my machine as 2005.
Also note: string = malloc( _MAX_PATH );
generates: Error 1 error C2065: '_MAX_PATH' : undeclared identifier
which seems that #include <stdio.h> isn't being linked.
is it possible that all libs are being referanced for some reason or another?
ASKER
Such an amazing n00b here, hopefully somewhere someone can learn from this also.
It seems if you have precompiled headers set, that MS compiler looks to stdafx.h for includes. For reasons I don't understand, if I move my includes into the stdafx.h the libs are included.
I don't, it doesn't included from the .c or .cpp files.
I'm sure this can altered with the project settings, but I'm not pro on those either.
It seems if you have precompiled headers set, that MS compiler looks to stdafx.h for includes. For reasons I don't understand, if I move my includes into the stdafx.h the libs are included.
I don't, it doesn't included from the .c or .cpp files.
I'm sure this can altered with the project settings, but I'm not pro on those either.
string = (char *)malloc( 260 );
Good Luck,
Kent