
void* realloc (void* ptr, size_t size)
例如,你第一次用 malloc 函数 动态分配了空间,随着一步步运算,觉得空间不够,需要加大空间,与此同时,原先空间里的数据需保留并还要继续使用,这时需要用 realloc,它能满足此需要。
下面是完整的程序例子。告诉使用 realloc 的窍门。
#include <stdio.h> /* printf, scanf, puts */
#include <stdlib.h>/* realloc, free, exit, NULL */
int main ()
{
int input,n
int count = 0
int* numbers = NULL
int* more_numbers = NULL
do {
printf ("Enter an integer value (0 to end): ")
scanf ("%d", &input)
count++
more_numbers = (int*) realloc (numbers, count * sizeof(int))
if (more_numbers!=NULL) {
numbers=more_numbers
numbers[count-1]=input
}
else {
free (numbers)
puts ("Error (re)allocating memory")
exit (1)
}
} while (input!=0)
printf ("Numbers entered: ")
for (n=0n<countn++) printf ("%d ",numbers[n])
free (numbers)
return 0
}
你可以在窗体中添加一个容器控件,然后通过Add方法,把你添加的控件添加进这个容器控件里,每次你需要添加新控件前,运行一次容器控件的Clear(清除)方法,再添加,这样就能实现新添加的覆盖后添加的效果了。动态添加控件,自己要 *** 作的就是new一个控件出来,然后this.controls.add(控件)方法添加进去,利用Location属性确定其起始位置,用size属性确定其大小,附代码双击鼠标添加testboxprivate void from1_DoubleClick(object sender, EventArgs e)
{
TextBox mytestbox = new TextBox()
mytestbox.Location = PointToClient(MousePosition)
mytestbox.Size = new Size(100,50)//自己调整
this.Controls.Add(mytestbox)//添加,添到哪里自己调整
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)