int * p = NULL;
p = (int*)malloc(sizeof(int));
if (p == NULL)
{
/*...*/
}
/*初始化为0*/
memset(p, 0, sizeof(int));
char *ptr = (char *)malloc(10);
char name[20] ;
memcpy ( name,ptr,20); // Problem begins here
int mark[100];
...
//让用户输入学生编号,设现实中学生编号由1开始:
cout << "请输入学生编号(在1~100之间):"
int i;
cin >> i;
//输出对应学生的考试成绩:
cout << info[i-1];
class Object {
private:
void* data;
const int size;
const char id;
public:
Object(int sz, char c):size(sz), id(c){
data = new char[size];
cout << "Object() " << id << " size = " << size << endl;
}
~Object(){
cout << "~Object() " << id << endl;
delete []data;
}
};
void GetMemory2(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
}
void Test2(void)
{
char *str = NULL;
GetMemory2(&str, 100); // 注意参数是 &str,而不是str
strcpy(str, "hello");
cout<< str << endl;
free(str);
}
void port();
void addr();
int main ()
{
addr();
port();
}
long *p ;
void port()
{
long i, j ;
j = 0;
for ( i = 0 ; i < 10 ; i++ )
{
(*p)--;
j++;
}
}
void addr()
{
long k;
k = 0;
p = &k;
}
char *GetString2(void)
{
char *p = "hello world";
return p;
}
void Test5(void)
{
char *str = NULL;
str = GetString2();
cout<< str << endl;
}
char *p = (char *) malloc(10);
strcpy(p, “hello”);free(p); // p所指的内存被释放,但是p所指的地址仍然不变…//忘记 释放 strcpy(p, “world”); // 出错
char *p = (char *) malloc(10);
strcpy(p, “hello”);
free(p); // p所指的内存被释放,但是p所指的地址仍然不变…
if(p != NULL) // 虽然记得,但没有起到防错作用
{
strcpy(p, “world”); // 出错
}
联系客服