程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#中事務處置和非事務處置辦法實例剖析

C#中事務處置和非事務處置辦法實例剖析

編輯:C#入門知識

C#中事務處置和非事務處置辦法實例剖析。本站提示廣大學習愛好者:(C#中事務處置和非事務處置辦法實例剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中事務處置和非事務處置辦法實例剖析正文


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

頭文件:#include <malloc.h> 或 #include <alloc.h> (留意:alloc.h 與 malloc.h 的內容是完整分歧的。)

功效:分派長度為num_bytes字節的內存塊

解釋:假如分派勝利則前往指向被分派內存的指針,不然前往空指針NULL。

當內存不再應用時,應應用free()函數將內存塊釋放。

舉例:

#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;
}


2、函數聲明(函數原型):

  void *malloc(int size);

  解釋:malloc 向體系請求分派指定size個字節的內存空間。前往類型是 void* 類型。void* 表現未肯定類型的指針。C,C++劃定,void* 類型可以強迫轉換為任何其它類型的指針。這個在MSDN上可以找到相干的說明,詳細內容以下:   

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.

3、malloc與new的分歧點

  從函數聲明上可以看出。malloc 和 new 至多有兩個分歧: new 前往指定類型的指針,而且可以主動盤算所須要年夜小。好比:

   

  int *p;

  p = new int; //前往類型為int* 類型(整數型指針),分派年夜小為 sizeof(int);

  或:

  int* parr;

  parr = new int [100]; //前往類型為 int* 類型(整數型指針),分派年夜小為 sizeof(int) * 100;

 

    而 malloc 則必需由我們盤算要字節數,而且在前往後強行轉換為現實類型的指針。

   int* p;

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

 

  第1、malloc 函數前往的是 void * 類型,假如你寫成:p = malloc (sizeof(int)); 則法式沒法經由過程編譯,報錯:“不克不及將 void* 賦值給 int * 類型變量”。所以必需經由過程 (int *) 來將強迫轉換。

  第2、函數的實參為 sizeof(int) ,用於指明一個整型數據須要的年夜小。假如你寫成:

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

  代碼也能經由過程編譯,但現實上只分派了1個字節年夜小的內存空間,當你往外頭存入一個整數,就會有3個字節無家可歸,而直接“住進鄰人家”!形成的成果是前面的內存華夏稀有據內容全體被清空。

  malloc 也能夠到達 new [] 的後果,請求出一段持續的內存,辦法不過是指定你所須要內存年夜小。

  好比想分派100個int類型的空間:

  int* p = (int *) malloc ( sizeof(int) * 100 ); //分派可以放得下100個整數的內存空間。

  別的有一點不克不及直接看出的差別是,malloc 盡管分派內存,其實不能對所得的內存停止初始化,所以獲得的一片新內存中,其值將是隨機的。

  除分派及最初釋放的辦法紛歧樣之外,經由過程malloc或new獲得指針,在其它操作上堅持分歧。


4、靜態請求數組

請求一維數組
一維數組的數組名可以算作數組肇端元素的首地址,是以我界說一個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; 
  } 


請求二維數組
二維數組的數組名是其一切一維數組的首地址,由於二維數組的數組名是指針的指針,由於我界說一個row行column列的二維數組,寫法以下:

 

  #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; 
  } 


總結:

malloc()函數其實就在內存中找一片指定年夜小的空間,然後將這個空間的首地址規模給一個指針變量,這裡的指針變量可所以一個零丁的指針,也能夠是一個數組的首地址,這要看malloc()函數中參數size的詳細內容。我們這裡malloc分派的內存空間在邏輯上持續的,而在物理上可以持續也能夠不持續。關於我們法式員來講,我們存眷的是邏輯上的持續,由於操作體系會幫我們支配內存分派,所以我們應用起來便可以當作是持續的。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved