| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 449 人关注过本帖
标题:求教:输入结果后报错,求教!
取消只看楼主 加入收藏
lxfeng1988
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2013-8-4
结帖率:100%
收藏
已结贴  问题点数:7 回复次数:0 
求教:输入结果后报错,求教!
程序清单:
//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
}
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: function address include storage memory 
2014-01-19 19:44
快速回复:求教:输入结果后报错,求教!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044135 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved