Solved

Malloc in VS 2005 C++

Posted on 2008-10-21
12
2,531 Views
Last Modified: 2012-08-14
When the follow code is compiled, the errors:
Error      1      error C3861: 'malloc': identifier not found      c:\temp\firtemp\firtemp\firtemp.cpp      11      
Error      2      error C3861: 'free': identifier not found      c:\temp\firtemp\firtemp\firtemp.cpp      21      

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" );

   }

}

Open in new window

0
Comment
Question by:dcrackel
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 22769697
You're using a C++ compiler with strong type casting.  :)  recasting the value returned from malloc should work.

  string = (char *)malloc( 260 );


Good Luck,
Kent


0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22769726
What is <malloc.h> ?
0
 

Author Comment

by:dcrackel
ID: 22769758
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      

0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 22769832
I tried the very same sample fom http://msdn.microsoft.com/en-us/library/6ewkz86d(VS.80).aspx - works like a charm:
C:\tmp\cc>cl malloc.c

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
 

Copyright (C) Microsoft Corporation.  All rights reserved.
 

malloc.c

Microsoft (R) Incremental Linker Version 8.00.50727.762

Copyright (C) Microsoft Corporation.  All rights reserved.
 

/out:malloc.exe

malloc.obj
 

C:\tmp\cc>

Open in new window

0
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 150 total points
ID: 22769854
Hi dcrackel,

I suspect that your compiler settings are set so that the reporting of errors and warnings errs on the side of caution.

malloc () returns a void*, not a char*. You're using a C++ compiler, not a C compiler. I suspect that it is attempting a strong typecast of the return value malloc, seeing that a char* is required to meet the program requirement, and not finding an appropriate prototype.

Try recasting the return value and/or compiling the program in C mode.


kent
0
 

Author Comment

by:dcrackel
ID: 22769970
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



0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 86

Expert Comment

by:jkr
ID: 22770017
That's odd. Try adding

#include <stdlib.h>

and then (if that is not enough)

   string = (char *) ::malloc( 260 );
0
 
LVL 45

Expert Comment

by:Kdo
ID: 22770020

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
0
 

Accepted Solution

by:
dcrackel earned 0 total points
ID: 22770044
another update.

as jkr: did, it works when compiled from the command line, not from inside Visual Studio?

/shurg

0
 
LVL 86

Expert Comment

by:jkr
ID: 22770071
Should work in both cases - the command line is just a quicker way to do that.
0
 

Author Comment

by:dcrackel
ID: 22771179
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?
0
 

Author Comment

by:dcrackel
ID: 22771646
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.  



0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now