我一直在阅读的C++书中指出,当使用delete
操作符删除指针时,指向它的位置的内存被"释放"并且可以被覆盖.它还指出指针将继续指向同一位置,直到重新分配或设置为NULL
.
但是在Visual Studio 2012中; 这似乎不是这样的!
例:
#include <iostream>
using namespace std;
int main()
{
int* ptr = new int;
cout << "ptr = " << ptr << endl;
delete ptr;
cout << "ptr = " << ptr << endl;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译并运行该程序时,我得到以下输出:
ptr = 0050BC10
ptr = 00008123
Press any key to continue....
Run Code Online (Sandbox Code Playgroud)
显然,当调用delete时,指针指向的地址会发生变化!
为什么会这样?这是否与Visual Studio有关?
如果删除可以改变它指向的地址,为什么不删除自动设置指针NULL
而不是一些随机地址?
c++ pointers memory-management delete-operator visual-studio-2012
假设我有一个数组,我知道我会做很多"数组是否包含X?" 检查.执行此操作的有效方法是将该数组转换为哈希,其中键是数组的元素,然后您可以说
if($hash{X}) { ... }
是否有一种简单的方法来执行此数组到哈希转换?理想情况下,它应该足够通用以获取匿名数组并返回匿名哈希.
我有一个采用多维的函数,std::vector
并需要将深度(或维数)作为模板参数传入。我不想对这个值进行硬编码,我想编写一个constexpr
函数,它将std::vector
深度作为unsigned integer
值返回。
例如:
std::vector<std::vector<std::vector<int>>> v =
{
{ { 0, 1}, { 2, 3 } },
{ { 4, 5}, { 6, 7 } },
};
// Returns 3
size_t depth = GetDepth(v);
Run Code Online (Sandbox Code Playgroud)
这需要在编译时完成,因为这个深度将作为模板参数传递给模板函数:
// Same as calling foo<3>(v);
foo<GetDepth(v)>(v);
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
我理解如何使用Perl sort()
函数得到我想要的结果,这更像是关于内部工作的问题sort()
.
"$ a"和"$ b"变量来自哪里?我阅读了文档中的排序,似乎不清楚.什么是"$ a"和"$ b"以及它们的特殊之处是什么?
例如:
my @sorted_list = sort {$a cmp $b} @unsorted_list;
Run Code Online (Sandbox Code Playgroud)
如何排序知道如何处理"$ a"和"$ b"以及为什么不为"$ a"或"$ b"获得"全局符号需要显式包名"错误?
我需要能够将数组切片绑定到原始数组,以便对原始数组(包括删除元素)所做的任何更改也将对数组切片进行.有没有办法做到这一点?
以下示例并不适合我想要的方式,但它只是为了证明我想要做的一点.
例:
my @array = 1 .. 10;
my @slice = @array[3 .. 8];
splice @array, 5, 2;
print "ARRAY: ";
print join ', ', @array;
print "\n";
print "SLICE: ";
print join ', ', @slice;
Run Code Online (Sandbox Code Playgroud)
输出:
ARRAY: 1, 2, 3, 4, 5, 8, 9, 10
SLICE: 4, 5, 6, 7, 8, 9
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是一种将切片绑定到原始数组的方法,因此输出看起来像这样:
ARRAY: 1, 2, 3, 4, 5, 8, 9, 10
SLICE: 4, 5, 8, 9
Run Code Online (Sandbox Code Playgroud)
从原始数组中删除6和7也会将其从数组切片中删除.
我怎样才能实现这样的目标?
我最近问了一个关于在Perl中覆盖对象和内存管理的问题.我收到的一个答案告诉我,我最近写的脚本可能有问题.
我有一个脚本,有一些非常复杂的数据结构,有许多parent->child / child->parent
关系.这也意味着有许多对象具有循环引用.根据这个答案,循环引用可以"欺骗"Perl的引用计数机制,如果没有正确处理它们会导致内存泄漏.
循环引用的示例:
+-----------------------------------------------------+
| |
+-->+============+ +==========+ |
[ Reference ----->[ Blessed ] |
$parent -->+============+ [ Hash ] |
[ ] +==========+ |
[ children --->[ Array ] |
[ ] [ ] |
+==========+ [ 0: ---------+ |
[ ] | |
+==========+ | |
| |
+--------------------------------------------------+ |
| |
+-->+============+ +==========+ |
[ Reference ----->[ Blessed ] |
$child --->+============+ [ Hash ] |
[ ] | …
Run Code Online (Sandbox Code Playgroud) 我已经读过,$VERSION
在所有Perl脚本中始终包含一个数字是一种良好的做法,但我从未完全理解其目的.
通过在脚本中包含版本号,您真正获得了什么?
通过混合静态和动态多态(模板和继承),我遇到了一种奇怪的技术,其功能类似于C++中的常规静态多态,除了在创建新对象后子类的成员仍然可见.
请考虑以下示例:
Base.h:
#include <iostream>
class Base {
public:
virtual ~Base() {}
virtual void say_hello() {
std::cout << "Hello from Base!" << std::endl;
}
};
Run Code Online (Sandbox Code Playgroud)
Class1.h:
#include "Base.h"
#include <iostream>
class Class1 : public Base {
public:
virtual void say_hello() {
std::cout << "Hello from Class1!" << std::endl;
}
int x = 1;
};
Run Code Online (Sandbox Code Playgroud)
Class2.h:
#include "Base.h"
#include <iostream>
class Class2 : public Base {
public:
virtual void say_hello() {
std::cout << "Hello from Class2!" << std::endl;
}
int y …
Run Code Online (Sandbox Code Playgroud) 我正在研究在Perl中读取文件的有效方法,并遇到了这个非常有趣的内容:
my $text = do { local (@ARGV, $/) = $file; <> };
Run Code Online (Sandbox Code Playgroud)
我的问题是:这究竟是如何工作的?通常在啜饮你设置的文件时$/ = undef
,但我不知道这是怎么回事.事实证明,这一小段代码非常难以理解.
什么是简化的细分和解释?
现在我知道它是如何工作的,让我们真正想象!
并不是说这段代码有任何实际用途; 想出来并冷静看待它真的很有趣.这是一个同时啜饮多个文件的单线程!
my @texts = map { local (@ARGV, $/) = $_; <> } @files;
Run Code Online (Sandbox Code Playgroud) 我遇到一个奇怪的问题,尝试inline
我的"Person"类的访问器导致代码无法编译.
以下代码将成功编译并运行(使用Visual Studio 2012):
Person.h
#pragma once
#include <string>
using namespace std;
class Person
{
public:
Person(string name, int age = 0);
~Person(void);
// Accessors
string name(void) const;
int age (void) const;
private:
string m_name;
int m_age;
};
Run Code Online (Sandbox Code Playgroud)
Person.cpp
#include "stdafx.h"
#include "Person.h"
Person::Person(string name, int age) :
m_name(name),
m_age (age )
{}
Person::~Person(void) {}
string Person::name(void) const
{
return m_name;
}
int Person::age(void) const
{
return m_age;
}
Run Code Online (Sandbox Code Playgroud)
header_test.cpp
#include "stdafx.h"
#include <iostream>
#include "Person.h"
using namespace …
Run Code Online (Sandbox Code Playgroud) perl ×6
c++ ×4
arrays ×2
scope ×2
templates ×2
constexpr ×1
file-io ×1
hash ×1
header-files ×1
inheritance ×1
inline ×1
memory-leaks ×1
oop ×1
pointers ×1
polymorphism ×1
reference ×1
slice ×1
sorting ×1
stdvector ×1
version ×1