const char *在C ++中 [英] const char* 's in C++

查看:126
本文介绍了const char *在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++中的字符串表达式如何工作?

How does string expressions in C++ work?

请考虑:

#include <iostream>
using namespace std;

int main(int argc, char *argv[]){

    const char *tmp="hey";
    delete [] tmp;

    return 0;
}

hey表达式存储在何处以及如何存储,当我试图删除它?

Where and how is the "hey" expression stored and why is there segmentation fault when I attempt to delete it?

推荐答案

它存储的是留给编译器来决定在这个然而,对你来说并不重要 - 如果你不用 new 分配内存,尝试用解除分配它是不太好的,删除。您不能

Where it's stored is left to the compiler to decide in this (somewhat special) case. However, it doesn't really matter to you - if you don't allocate memory with new, it's not very nice to attempt to deallocate it with delete. You cannot delete memory allocated in the way you have allocated it.

<$>

这篇关于const char *在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆