关于MALLOC 和FREE

C语言 码拜 9年前 (2015-05-11) 950次浏览 0个评论
 

为社么这样不可以:

#include “stdio.h”
#include “malloc.h”

void main()
{

   char *string;

   /* Allocate space for a path name */
   string = malloc( sizeof(“memory”) );
   if( string == NULL )
      printf( “Insufficient memory available\n” );
   else
   {
      string = “memory”;
      printf( “Memory space allocated for path name\n” );
      free( string );
      printf( “Memory freed\n” );
   }

}

30分
作了一些改动。

  #include  “stdio.h”  //———->#include <stdio.h>
  #include  “malloc.h” //———->#include <malloc.h>
                       //#include <string.h> 
  void  main()
  {
    
        char  *string;
  
        /*  Allocate  space  for  a  path  name  */
        string  =  malloc(  sizeof(“memory”)  );
      //string  =  (char*)malloc(   sizeof(“memory”));
        if(  string  ==  NULL  )
              printf(  “Insufficient  memory  available\n”  );
        else
        {
              string  =  “memory”;   //这里指针指向了一个常量
                                     //如果改用
                                     //strcpy(string,”memory);就行了
              printf(  “Memory  space  allocated  for  path  name\n”  );
              free(  string  );      //试图释放常量的内存,导致失败。
              printf(  “Memory  freed\n”  );
        }
  
  
  }

上面注释中的程序是我的改动。
因为malloc被定义为空指针void * , 因此在具体分配内存时应将malloc转换成相应的数据类型。
string = (char*)malloc(sizeof(“memory”)) ; 
另外free(string)后应将string==NULL;当然,这是个好习惯,但不影响编译。
点评你的程序,一起学习
#include  “malloc.h”
void  main()
{
    char  *string;
  
    /*  Allocate  space  for  a  path  name  */
    string  =  malloc(  sizeof(“memory”)  );
/*”memory”是一个const  char* 类型,取长度任何指针都是4,32位的操作系统
malloc返回void*类型,应强制转换
这句应该改为string=(void*)malloc(strlen(sizeof(“memory”)+1);//加1留给字符串的””/0″”字符
*/
        if(  string  ==  NULL  )
              printf(  “Insufficient  memory  available\n”  );
        else
        {
              string  =  “memory”;
/*
将const char*指针赋给string,不应释放。这种常量字符串内存不是动态申请的
你的意思是想,将string指向的内容修改
memcpy(string,”memory”,strlen(“memory”));
*/
              printf(  “Memory  space  allocated  for  path  name\n”  );
/*帮你加一句
if(string!=NULL)
*/              free(  string  );
/*

*/
              printf(  “Memory  freed\n”  );
        }
  
  
  }

在标准C里面
char * s = malloc(20);
是完全正确的,也算是个好习惯,但是C++不行,为了兼容C++,所以最好
进行类型转换

你那句string = “memory”;
是错误的,不应该这样直接赋值,可以看_goolooloo的程序作为参考。

你的malloc返回的是void*,而string是char*,所以不行了::#include  “stdio.h”
  #include  “malloc.h”
  
  void  main()
  {
    
        char  *string;
  
        /*  Allocate  space  for  a  path  name  */
        string  =(char*)  malloc(  sizeof(“memory”)  );
        if(  string  ==  NULL  )
              printf(  “Insufficient  memory  available\n”  );
        else
        {
              string  =  “memory”;
              printf(  “Memory  space  allocated  for  path  name\n”  );
              free(  string  );
              printf(  “Memory  freed\n”  );
        }
  
  
  } 
 
 
谢谢大家,我知道错误在那里了,不应该直接用string  =  “memory”;赋值,
至于string = malloc(sizeof(“memory”));我不认为有错.
这段程序是MSDN上的例子,只是我加了一句string  =  “memory”;
再次谢谢大家

sizeof没有错么?
sizeof(“memory”)我跟踪了
等于4
sizeof(“memory”)我跟踪了
等于4

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于MALLOC 和FREE
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!