我想确定以下程序(查找最大子数组的实现)是否泄漏内存。有没有通用的方法来确定这一点?例如使用调试器的某些功能?什么是一般策略?
struct Interval {
int max_left;
int max_right;
int sum;
};
struct Interval * max_crossing_subarray(int A[], int low, int mid, int high) {
struct Interval * crossing = malloc(sizeof(struct Interval));
int left_sum = INT_MIN;
int sum = 0;
for(int i = mid; i >= low; --i) {
sum = sum + A[i];
if(sum > left_sum) {
left_sum = sum;
crossing->max_left = i;
}
}
int right_sum = INT_MIN;
sum = 0;
for(int j = mid+1; j <= high; ++j) { …Run Code Online (Sandbox Code Playgroud) 到目前为止,我已经设法将词法分析器和堆栈放在一起,以期实现 LL1 解析器。我这样做纯粹是为了了解解析的工作原理,并且可能在未来的项目中使用这些想法。我知道有很多更好的框架,比如json-cpp和rapid-json,但我想自己理解这一点。
头文件如下。
#pragma once
#include <string>
#include <vector>
#include <map>
#include <variant>
#include <fstream>
#include <stack>
#include "Helper.h"
// Debugging
#include <iostream>
// Types to store JSON ouput
struct jlist;
struct jobject;
using json_value = std::variant<int, float, bool, std::string, jlist, jobject>;
enum tag { int_value, float_value, string_value, list, object };
struct jlist {
tag type;
std::vector<json_value *> vector_value;
};
struct jobject {
tag type;
std::map<std::string, json_value *> map_value;
};
class JSONParser
{
public:
JSONParser(); …Run Code Online (Sandbox Code Playgroud) 我不确定如何从我编写的可变参数模板函数中获得特定效果。下面是我编写的函数。
template<typename ... T>
bool multiComparision(const char scope, T ... args) {
return (scope == (args || ...));
}
Run Code Online (Sandbox Code Playgroud)
有人向我指出,尽管没有在较大的代码范围内创建任何错误,但这样做实际上与我想要的有所不同。
multiComparision('a', '1', '2', '3');
=>
return ('a' == ('1' || '2' || '3'));
Run Code Online (Sandbox Code Playgroud)
我实际上打算让该函数返回以下内容
multiComparision('a', '1', '2', '3');
=>
return ('a' == '1' || 'a' == '2' || 'a' == '3');
Run Code Online (Sandbox Code Playgroud)
如何达到预期的效果?