由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - array allocation in c
相关主题
再问一个free()的问题请教boost::any compile错误。
问一个private destructor的问题关于thread的stack
[合集] 谁给个stack-based allocation 的C++的例子?在 windows下的C++开发平台是不是 Dev-C++?
这个怎么allocate memory?怎么得到char *分配空间的大小?
stack/heap corruptionC puzzle 一日一题
菜鸟请教C问题a string define question (c++)
在子函数内开内存,返回主函数指针然后释放空间是不是很糟糕的(转载)VC++ 中的 memory leak problem
什么是OS Memory management and heap structure?奇怪的问题:关于一个简单的malloc()小程序 (转载)
相关话题的讨论汇总
话题: array话题: int话题: scanf话题: compiler话题: allocation
进入Programming版参与讨论
1 (共1页)
m****i
发帖数: 712
1
#include
int main()
{
int n;
scanf("%d",&n);
int a[n];
int i;
for(i=0;i {
a[i] = i;
printf ("%d ",a[i]);
}
return 1;
}
This code work,
But the compiler only know how big the array on run-time....
how the compiler allocate the array on stack during compile time?
I**********s
发帖数: 441
2
I didn't try your code. But you better use malloc, that's safer.
char * a = (char *) malloc(sizeof(char) * n);
if (NULL == a) {
puts("out of memory");
exit(0);
}
c****e
发帖数: 1453
3
the compiler just implicitly calls the new and delete, it's an easy one-to-
one mapping.
p***o
发帖数: 1252
4
new? delete? malloc? free?
If you have to say it's some function call, it's alloca,
which allocates memory on the stack instead of the heap.

【在 c****e 的大作中提到】
: the compiler just implicitly calls the new and delete, it's an easy one-to-
: one mapping.

t*****g
发帖数: 1275
5
Dynamic array on stack is allocated using calculated size instead of 'pre-
calculated' or 'hard-coded' size; The allocation is not different than
moving the stack pointer though. The following assembly code may show you
how it works (solaris on x86)
...
leal -0x8(%ebp),%eax ; pipe scanf output to this address
...
call -0x23d ; call scanf
movl -0x8(%ebp),%eax ; obain variable 'n' and save it to %eax
...
subl %eax,%esp ; grow the stack pointer

【在 m****i 的大作中提到】
: #include
: int main()
: {
: int n;
: scanf("%d",&n);
: int a[n];
: int i;
: for(i=0;i: {
: a[i] = i;

y*****a
发帖数: 171
6
give n a value of something like 8192 or greater, you will see how it crashs
.
1 (共1页)
进入Programming版参与讨论
相关主题
奇怪的问题:关于一个简单的malloc()小程序 (转载)stack/heap corruption
a=(char **)malloc(12*sizeof(char *)) 是什么意思?菜鸟请教C问题
purify和valgrind的比较在子函数内开内存,返回主函数指针然后释放空间是不是很糟糕的(转载)
a c++ question.什么是OS Memory management and heap structure?
再问一个free()的问题请教boost::any compile错误。
问一个private destructor的问题关于thread的stack
[合集] 谁给个stack-based allocation 的C++的例子?在 windows下的C++开发平台是不是 Dev-C++?
这个怎么allocate memory?怎么得到char *分配空间的大小?
相关话题的讨论汇总
话题: array话题: int话题: scanf话题: compiler话题: allocation