3 回答

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超21個(gè)贊
你有其中一些是正確的,但是寫這些問(wèn)題的人至少會(huì)欺騙你一個(gè)問(wèn)題:
全局變量------->數(shù)據(jù)(正確)
靜態(tài)變量------->數(shù)據(jù)(正確)
常量數(shù)據(jù)類型----->代碼和/或數(shù)據(jù)。當(dāng)一個(gè)常量本身存儲(chǔ)在數(shù)據(jù)段中時(shí),考慮字符串文字,并且對(duì)它的引用將嵌入到代碼中
局部變量(在函數(shù)中聲明和定義)--------> stack(正確)
main
函數(shù)中聲明和定義的變量-----> 堆也堆棧(老師試圖欺騙你)指針(例如:
char *arr
,int *arr
)-------> 堆數(shù)據(jù)或堆棧,具體取決于上下文。C允許您聲明全局或static
指針,在這種情況下,指針本身將在數(shù)據(jù)段中結(jié)束。動(dòng)態(tài)分配的空間(使用
malloc
,calloc
,realloc
)--------> 堆的堆
值得一提的是,“堆?!北徽椒Q為“自動(dòng)存儲(chǔ)類”。

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊
糾正了錯(cuò)誤的句子
constant data types -----> code //wrong
局部常量變量----->棧
初始化全局常量變量----->數(shù)據(jù)段
未初始化的全局常量變量-----> bss
variables declared and defined in main function -----> heap //wrong
在main函數(shù)-----> stack中聲明和定義的變量
pointers(ex:char *arr,int *arr) -------> heap //wrong
dynamically allocated space(using malloc,calloc) --------> stack //wrong
指針(例如:char * arr,int * arr)------->指針變量的大小將在堆棧中。
考慮你動(dòng)態(tài)分配n個(gè)字節(jié)的內(nèi)存(使用malloc或calloc),然后使指針變量指向它?,F(xiàn)在,n內(nèi)存的字節(jié)數(shù)在堆中,指針變量需要4個(gè)字節(jié)(如果是64位機(jī)器8個(gè)字節(jié)),它將在堆棧中存儲(chǔ)n內(nèi)存塊字節(jié)的起始指針。
注意:指針變量可以指向任何段的內(nèi)存。
int x = 10;
void func()
{
int a = 0;
int *p = &a: //Now its pointing the memory of stack
int *p2 = &x; //Now its pointing the memory of data segment
chat *name = "ashok" //Now its pointing the constant string literal
//which is actually present in text segment.
char *name2 = malloc(10); //Now its pointing memory in heap
...
}
動(dòng)態(tài)分配空間(使用malloc,calloc)-------->堆
- 3 回答
- 0 關(guān)注
- 479 瀏覽
添加回答
舉報(bào)