开发者

Array in a memory-constrained system

开发者 https://www.devze.com 2023-02-03 15:01 出处:网络
Consider that my system has memory, but it is scattered in different places (fragmented). There are no four c开发者_开发问答ontiguous memory locations that are free. In that scenario, if I declare a c

Consider that my system has memory, but it is scattered in different places (fragmented). There are no four c开发者_开发问答ontiguous memory locations that are free. In that scenario, if I declare a character array of size 10 in the C language, what will happen ?


If "my system has memory, but it is scattered in different places(fragmented)" means, that heap virtual memory is fragmented, and "declare a character array of size 10" means, that you create character array of size 10 in stack memory:

char str[10];

, then array will be successfully created.

If "declare a character array of size 10" means, that you allocate memory with malloc() (allocate in heap):

char *str2;
str2 = (char*) malloc(10 * sizeof(char));

, then malloc() will return NULL.


If all of your memory including the stack is fragmented like this, you either have a compiler (or runtime system) that supports non-contiguous stacks—in which case it might also be smart enough to support non-contiguous arrays—or basically everything you do (like calling subroutines) will result in a stack overflow and crash your program.

0

精彩评论

暂无评论...
验证码 换一张
取 消