一道关于内存操作和strcpy的笔试题
2013-04-21 23:42
148 查看
/** char* strcpy(char* _Dest, const char* _Source); 不管strlen(_Dest)和strlen(_Source)的大小关系如何,strcpy总能成功执行,并且执行完copy之后,会在内容的后面补上'\0'。 */ #include <iostream> using namespace std; void GetMomery1(char** str, int a) { *str = (char*)malloc(sizeof(a)); } char* GetMomery2() { char* str = new char[8]; strcpy(str, "HelloWorld2!"); return str; } char* GetMomery3() { char str[16] = "HelloWorld3!"; return str; } int main(int argc, char* argv[]) { char* str1; GetMomery1(&str1, 100); strcpy(str1, "HelloWorld1!"); printf("%s\n", str1); char* str2 = GetMomery2(); printf("%s\n", str2); char* str3 = GetMomery3(); printf("%s\n", str3); char* str4 = (char*)malloc(128); strcpy(str4, "HelloWorld4!"); //free(str4); if(str4 != NULL) { strcpy(str4, "HelloWorld4!"); //如果上面去掉注释,执行了“free(str4);”,则这里会有runtime error printf("%s\n", str4); } system("pause"); return 0; } /* Output: HelloWorld1! HelloWorld2! ? HelloWorld4! */
相关文章推荐
- 一道关于内存分配的笔试题
- 一道关于字符串逆序的笔试题
- 一道关于删除数据表中重复数据的笔试题
- 一道关于字符串操作的面试题
- 一道关于java 类初始化 成员初始化的笔试题的解析
- 一道关于联合(union)的笔试题
- [C/C++]一道指针,内存布局的笔试题[摘自CSDN]
- 一道关于回文的笔试题
- 关于Go语言共享内存操作的小实例
- Jquery学习,一道笔试题:关于表格
- 关于Linux命令ls的一道笔试题
- 一道 google曾出过的笔试题:编程实现对数学一元多项式的相加和相乘操作
- 关于一道C++笔试题的纠结,学计算机的伤不起啊
- 一道关于fork的笔试题
- 关于Go语言共享内存操作的小实例
- 一道关于static的笔试题。
- 一道关于java线程中断的题目,求大神指导,昨晚的阿里巴巴笔试题
- 关于NVDIA(英伟达)一道笔试编程题——蛇形阵列——的再思考
- 关于创新工场的一道笔试题