欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C語言中的malloc使用詳解

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

一、原型:extern void *malloc(unsigned int num_bytes);

頭文件:#include <malloc.h> 或 #include <alloc.h> (注意:alloc.h 與 malloc.h 的內(nèi)容是完全一致的。)

功能:分配長度為num_bytes字節(jié)的內(nèi)存塊

說明:如果分配成功則返回指向被分配內(nèi)存的指針,否則返回空指針NULL。

當(dāng)內(nèi)存不再使用時,應(yīng)使用free()函數(shù)將內(nèi)存塊釋放。

舉例:

#include<stdio.h>
#include<malloc.h>
int main()
{
  char *p;
 
  p=(char *)malloc(100);
  if(p)
    printf("Memory Allocated at: %x/n",p);
  else
    printf("Not Enough Memory!/n");
  free(p);
  return 0;
}


二、函數(shù)聲明(函數(shù)原型):

  void *malloc(int size);

  說明:malloc 向系統(tǒng)申請分配指定size個字節(jié)的內(nèi)存空間。返回類型是 void* 類型。void* 表示未確定類型的指針。C,C++規(guī)定,void* 類型可以強(qiáng)制轉(zhuǎn)換為任何其它類型的指針。這個在MSDN上可以找到相關(guān)的解釋,具體內(nèi)容如下:   

malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item. Always check the return from malloc, even if the amount of memory requested is small.

三、malloc與new的不同點(diǎn)

  從函數(shù)聲明上可以看出。malloc 和 new 至少有兩個不同: new 返回指定類型的指針,并且可以自動計(jì)算所需要大小。比如:

   

  int *p;

  p = new int; //返回類型為int* 類型(整數(shù)型指針),分配大小為 sizeof(int);

  或:

  int* parr;

  parr = new int [100]; //返回類型為 int* 類型(整數(shù)型指針),分配大小為 sizeof(int) * 100;

 

    而 malloc 則必須由我們計(jì)算要字節(jié)數(shù),并且在返回后強(qiáng)行轉(zhuǎn)換為實(shí)際類型的指針。

   int* p;

  p = (int *) malloc (sizeof(int));

 

  第1、malloc 函數(shù)返回的是 void * 類型,如果你寫成:p = malloc (sizeof(int)); 則程序無法通過編譯,報錯:“不能將 void* 賦值給 int * 類型變量”。所以必須通過 (int *) 來將強(qiáng)制轉(zhuǎn)換。

  第2、函數(shù)的實(shí)參為 sizeof(int) ,用于指明一個整型數(shù)據(jù)需要的大小。如果你寫成:

  int* p = (int *) malloc (1);

  代碼也能通過編譯,但事實(shí)上只分配了1個字節(jié)大小的內(nèi)存空間,當(dāng)你往里頭存入一個整數(shù),就會有3個字節(jié)無家可歸,而直接“住進(jìn)鄰居家”!造成的結(jié)果是后面的內(nèi)存中原有數(shù)據(jù)內(nèi)容全部被清空。

  malloc 也可以達(dá)到 new [] 的效果,申請出一段連續(xù)的內(nèi)存,方法無非是指定你所需要內(nèi)存大小。

  比如想分配100個int類型的空間:

  int* p = (int *) malloc ( sizeof(int) * 100 ); //分配可以放得下100個整數(shù)的內(nèi)存空間。

  另外有一點(diǎn)不能直接看出的區(qū)別是,malloc 只管分配內(nèi)存,并不能對所得的內(nèi)存進(jìn)行初始化,所以得到的一片新內(nèi)存中,其值將是隨機(jī)的。

  除了分配及最后釋放的方法不一樣以外,通過malloc或new得到指針,在其它操作上保持一致。


四、動態(tài)申請數(shù)組

申請一維數(shù)組
一維數(shù)組的數(shù)組名可以看成數(shù)組起始元素的首地址,因此我定義一個int *arr的指針,分配n個大小的int型空間,寫法如下:

   

#include <stdio.h> 
  #include <stdlib.h> 
   
  int main(void) 
  { 
    int n, *arr; 
   
    while (scanf("%d", &n) != EOF) { 
      arr = (int *)malloc(sizeof(int) * n); 
    } 
   
    return 0; 
  } 


申請二維數(shù)組
二維數(shù)組的數(shù)組名是其所有一維數(shù)組的首地址,因?yàn)槎S數(shù)組的數(shù)組名是指針的指針,因?yàn)槲叶x一個row行column列的二維數(shù)組,寫法如下:

 

  #include <stdio.h> 
  #include <stdlib.h> 
   
  int main(void) 
  { 
    int i, row, column, **arr; 
   
    while (scanf("%d %d", &row, &column) != EOF) { 
      arr = (int **)malloc(sizeof(int *) * row); // 分配所有行的首地址 
      for (i = 0; i < row; i ++) { // 按行分配每一列 
        arr[i] = (int *)malloc(sizeof(int) * column);   
      } 
   
      free(arr); 
    } 
   
    return 0; 
  } 


總結(jié):

malloc()函數(shù)其實(shí)就在內(nèi)存中找一片指定大小的空間,然后將這個空間的首地址范圍給一個指針變量,這里的指針變量可以是一個單獨(dú)的指針,也可以是一個數(shù)組的首地址,這要看malloc()函數(shù)中參數(shù)size的具體內(nèi)容。我們這里malloc分配的內(nèi)存空間在邏輯上連續(xù)的,而在物理上可以連續(xù)也可以不連續(xù)。對于我們程序員來說,我們關(guān)注的是邏輯上的連續(xù),因?yàn)椴僮飨到y(tǒng)會幫我們安排內(nèi)存分配,所以我們使用起來就可以當(dāng)做是連續(xù)的。

上一篇:北郵計(jì)算機(jī)考研復(fù)試題的C語言解答精選

欄    目:C語言

下一篇:使用C語言求二叉樹結(jié)點(diǎn)的最低公共祖先的方法

本文標(biāo)題:C語言中的malloc使用詳解

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2877.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有