C中的malloc:C中的動態內存分配

什么是C中的malloc()? (What is malloc() in C?)

malloc() is a library function that allows C to allocate memory dynamically from the heap. The heap is an area of memory where something is stored.

malloc()是一個庫函數,它允許C從堆動態分配內存。 堆是存儲內容的內存區域。

malloc() is part of stdlib.h and to be able to use it you need to use #include <stdlib.h>.

malloc()是stdlib.h的一部分,要使用它,您需要使用#include <stdlib.h>

如何使用Malloc (How to Use Malloc)

malloc() allocates memory of a requested size and returns a pointer to the beginning of the allocated block. To hold this returned pointer, we must create a variable. The pointer should be of same type used in the malloc statement.Here we’ll make a pointer to a soon-to-be array of ints

malloc()分配請求大小的內存,并返回指向已分配塊開頭的指針。 要保留此返回的指針,我們必須創建一個變量。 該指針應與malloc語句中使用的類型相同。在這里,我們將創建一個指向即將成為整數的數組的指針。

int* arrayPtr;

Unlike other languages, C does not know the data type it is allocating memory for; it needs to be told. Luckily, C has a function called sizeof() that we can use.

與其他語言不同,C不知道它為其分配內存的數據類型。 它需要被告知。 幸運的是,C有一個我們可以使用的名為sizeof()的函數。

arrayPtr = (int *)malloc(10 * sizeof(int));

This statement used malloc to set aside memory for an array of 10 integers. As sizes can change between computers, it’s important to use the sizeof() function to calculate the size on the current computer.

該語句使用malloc為10個整數的數組留出內存。 由于大小可以在計算機之間改變,因此使用sizeof()函數計算當前計算機上的大小非常重要。

Any memory allocated during the program’s execution will need to be freed before the program closes. To free memory, we can use the free() function

在程序關閉之前,必須先釋放程序執行期間分配的所有內存。 要free內存,我們可以使用free()函數

free( arrayPtr );

This statement will deallocate the memory previously allocated. C does not come with a garbage collector like some other languages, such as Java. As a result, memory not properly freed will continue to be allocated after the program is closed.

該語句將取消分配先前分配的內存。 C沒有像Java之類的其他語言那樣帶有garbage collector 。 因此,關閉程序后,將繼續分配未正確釋放的內存。

在繼續之前... (Before you go on…)

回顧 (A Review)

  • Malloc is used for dynamic memory allocation and is useful when you don’t know the amount of memory needed during compile time.

    Malloc用于動態內存分配,當您在編譯時不知道所需的內存量時很有用。
  • Allocating memory allows objects to exist beyond the scope of the current block.

    分配內存允許對象存在于當前塊的范圍之外。
  • C passes by value instead of reference. Using malloc to assign memory, and then pass the pointer to another function, is more efficient than having the function recreate the structure.

    C通過值而不是引用傳遞。 使用malloc分配內存,然后將指針傳遞給另一個函數,比讓函數重新創建結構更有效。

有關C編程的更多信息: (More info on C Programming:)

  • The beginner's handbook for C programming

    C程序設計初學者手冊

  • If...else statement in C explained

    如果...在C中的其他語句解釋了

  • Ternary operator in C explained

    C中的三元運算符說明

翻譯自: https://www.freecodecamp.org/news/malloc-in-c-dynamic-memory-allocation-in-c-explained/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390722.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390722.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390722.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux文本編輯器

Linux文本編輯器 Linux系統下有很多文本編輯器。 按編輯區域&#xff1a; 行編輯器 ed 全屏編輯器 vi 按運行環境&#xff1a; 命令行控制臺編輯器 vi X Window圖形界面編輯器 gedit ed 它是一個很古老的行編輯器&#xff0c;vi這些編輯器都是ed演化而來。 每次只能對一…

Alpha第十天

Alpha第十天 聽說 031502543 周龍榮&#xff08;隊長&#xff09; 031502615 李家鵬 031502632 伍晨薇 031502637 張檉 031502639 鄭秦 1.前言 任務分配是VV、ZQ、ZC負責前端開發&#xff0c;由JP和LL負責建庫和服務器。界面開發的教輔材料是《第一行代碼》&#xff0c;利用And…

Streamlit —使用數據應用程序更好地測試模型

介紹 (Introduction) We use all kinds of techniques from creating a very reliable validation set to using k-fold cross-validation or coming up with all sorts of fancy metrics to determine how good our model performs. However, nothing beats looking at the ra…

Spring MVC Boot Cloud 技術教程匯總(長期更新)

昨天我們發布了Java成神之路上的知識匯總&#xff0c;今天繼續。 Java成神之路技術整理&#xff08;長期更新&#xff09; 以下是Java技術棧微信公眾號發布的關于 Spring/ Spring MVC/ Spring Boot/ Spring Cloud 的技術干貨&#xff0c;本文長期更新。 Spring 系列 Java 必看的…

X Window系統

X Window系統 一種以位圖方式顯示的軟件窗口系統。誕生于1984&#xff0c;比Microsoft Windows要早。是一套獨立于內核的軟件 Linux上的X Window系統 X Window系統由三個基本元素組成&#xff1a;X Server、X Client和二者通信的通道。 X Server&#xff1a;是控制輸出及輸入…

冒名頂替上大學羅彩霞_什么是冒名頂替綜合癥,您如何克服?

冒名頂替上大學羅彩霞冒名頂替綜合癥 (Imposter Syndrome) Imposter Syndrome is a feeling of being a fraud or not being good enough to get the job done. Its common among software engineers, developers and designers working in tech companies, especially those n…

Linux命令----用戶管理

修改用戶密碼&#xff1a; sudo passwd &#xff08;當前&#xff09;用戶名  【sudo是super user do的簡寫&#xff0c;passwd是password的簡寫】 顯示當前正在操作系統的用戶&#xff1a;whoami   顯示當前登錄系統的用戶信息&#xff1a;who am i 注意&#xff1a; 普通…

lasso回歸和嶺回歸_如何計劃新產品和服務機會的回歸

lasso回歸和嶺回歸Marketers sometimes have to be creative to offer customers something new without the luxury of that new item being a brand-new product or built-from-scratch service. In fact, incrementally introducing features is familiar to marketers of c…

python代碼

原始字符串&#xff0c;不做任何特殊的處理 print("Newlines are indicated by \n")#Newlines are indicated by print(r"Newlines are indicated by \n")#Newlines are indicated by \n 格式輸出&#xff0c;轉化為字符串由format自動完成 ag…

Linux 設備管理和進程管理

設備管理 Linux系統中設備是用文件來表示的&#xff0c;每種設備都被抽象為設備文件的形式&#xff0c;這樣&#xff0c;就給應用程序一個一致的文件界面&#xff0c;方便應用程序和操作系統之間的通信。 設備文件集中放置在/dev目錄下&#xff0c;一般有幾千個&#xff0c;不…

樂高ev3涉及到的一些賽事_使您成為英雄的前五名開發者技能(提示:涉及LEGO)

樂高ev3涉及到的一些賽事Programming is like building something with LEGOs. Any developer can pick up a brand new LEGO set and build it following the instructions. This is very easy. Think of it as coding school assignments or entry level tutorials.編程就像用…

貝葉斯 定理_貝葉斯定理實際上是一個直觀的分數

貝葉斯 定理Bayes’ Theorem is one of the most known to the field of probability, and it is used often as a baseline model in machine learning. It is, however, too often memorized and chanted by people who don’t really know what P(B|E) P(E|B) * P(B) / P(E…

winfrom 點擊按鈕button彈框顯示顏色集

1.窗體托一個按鈕button&#xff1b; 2.單擊事件&#xff1a; 1 private void btnForeColor_Click(object sender, EventArgs e)2 {3 using (ColorDialog cdialog new ColorDialog())4 {5 cdialog.AnyColor true;6 …

JavaScript時間事件:setTimeout和setInterval

Programmers use timing events to delay the execution of certain code, or to repeat code at a specific interval.程序員使用時序事件來延遲某些代碼的執行&#xff0c;或以特定的時間間隔重復代碼。 There are two native functions in the JavaScript library used to …

webservice 基本要點

webservice的特點 webservices是自我包含的 webservices是自我描述的 webservices是跨平臺和語言的 webservices是基于開放和標準的 webservices是可以組合的 webservices是松散耦合的 webservices提供編程訪問的能力 webservices通過網絡進行發布&#xff0c;查找和使用 發布w…

文本數據可視化_如何使用TextHero快速預處理和可視化文本數據

文本數據可視化自然語言處理 (Natural Language Processing) When we are working on any NLP project or competition, we spend most of our time on preprocessing the text such as removing digits, punctuations, stopwords, whitespaces, etc and sometimes visualizati…

Less變量

Less變量 定義變量 Less 中的變量和其他編程語言一樣&#xff0c;可以實現值的復用&#xff0c;同樣它也有作用域&#xff08;scope&#xff09;。簡單的講&#xff0c;變量作用域就是局部變量和全局變量的概念。 Less 中&#xff0c;變量作用域采用的是就近原則&#xff0c;換…

漸進式web應用程序_如何在漸進式Web應用程序中添加到主屏幕

漸進式web應用程序添加到主屏幕 (Add To Homescreen) Here the web app install banner is focused on web app, with the feature of add to homescreen.在此&#xff0c;Web應用程序安裝標語專注于Web應用程序&#xff0c;具有添加到主屏幕的功能。 瀏覽器對“添加到主屏幕”…

linux shell 編程

shell的作用 shell是用戶和系統內核之間的接口程序shell是命令解釋器 shell程序 Shell程序的特點及用途&#xff1a; shell程序可以認為是將shell命令按照控制結構組織到一個文本文件中&#xff0c;批量的交給shell去執行 不同的shell解釋器使用不同的shell命令語法 shell…

Leetcode之javascript解題(No33-34)

附上我的github倉庫&#xff0c;會不斷更新leetcode解題答案&#xff0c;提供一個思路&#xff0c;大家共勉 在我的主頁和github上可以看到更多的關于leetcode的解題報告&#xff01;&#xff08;因為不知道為什么掘金沒有將其發布出來&#xff0c;目前已經聯系掘金客服&#x…