注册 登录
编程论坛 C++教室

求教:输入结果后报错,求教!

lxfeng1988 发布于 2014-01-19 19:44, 449 次点击
程序清单:
//delete.cpp--using the delete operator
#include<iostream>
#include<cstring>  //or string.h
using namespace std;
char * getname(void);  //function prototype
int main()
{
    char * name;  //creat pointer but no storage

    name = getname();  //assign address of string to name
    cout << name << " at " << (int*)name << "\n";
    delete[] name;  //memory freed

    name = getname();  //reuse freed memory
    cout << name << " at " << (int*)name << "\n";
    delete[] name;  //memory freed again
    return 0;
}

char * getname()  //return pointer to new string
{
    char temp[80];  //temporary storage
    cout << "Enter last name: ";
    cin >> temp;
    char * pn = new char[strlen(temp) + 1];
    strcpy_s(pn,5,temp);  //copy string into smaller space

    return pn;  //temp lost when function ends
}
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
1 回复
#2
rjsp2014-01-20 08:24
现在的小孩表达能力真不行
比如说编译错误,却不给出错误信息
比如说输入后报错,却不告诉别人输入的是什么(就你这题而言,输入 abcd 1234 就不报错)

楼主需要的答案,在楼主自己的代码中写着呐,倒数第4行

1