小编Str*_*ode的帖子

当我对指针变量使用不同的赋值运算符时,程序的行为有所不同

我在玩的时候遇到了这种奇怪的行为:

package main

import "fmt"

var appConfig *map[string]interface{}

func main() {
    // Works fine
    //appConfig = &map[string]interface{}{
    //  "name": "marcus",
    //}
    // Causes panic: runtime error: invalid memory address or nil pointer dereference
    appConfig := &map[string]interface{}{
        "name": "marcus",
    }
    fmt.Println("Config:", *appConfig)
    getName()
}

func getName() {
    fmt.Println("Name is ", (*appConfig)["name"])
}
Run Code Online (Sandbox Code Playgroud)

:=我不明白如果我使用短变量声明而不是简单的=赋值运算符,为什么程序会崩溃。有人可以启发我了解行为差异背后的原因吗?提前致谢。

go

2
推荐指数
1
解决办法
61
查看次数

添加#define new时,不是可识别的运算符或类型

我正在尝试解决我的程序的一部分,它有一个#define new.一切都运行良好,直到我尝试创建一个覆盖新运算符的类模板,当我得到错误时:

C:\Define_New_problem\main.cpp:18: error: expected type-specifier before 'dPushMemManFileLine'
C:\Define_New_problem\main.cpp:18: error: expected ';' before 'dPushMemManFileLine'
C:\Define_New_problem\main.cpp:21: error: expected ';' before '}' token
Process terminated with status 1 (0 minutes, 0 seconds)
Run Code Online (Sandbox Code Playgroud)

使用MinGW.(简化)代码如下:

#include <iostream>

#define new dPushMemManFileLine( __FILE__, __LINE__ ) ? 0 : new

using namespace std;

static const int NUM_NEW_STACK_SIZE = 256;
static struct { const char* filename; unsigned int line; } g_NewStackMemDebug[NUM_NEW_STACK_SIZE];
static int g_CurStack = -1;

template <class T>
class mypair {
  private:
    int a, b;
  public:
    mypair …
Run Code Online (Sandbox Code Playgroud)

c++ stl new-operator c-preprocessor

1
推荐指数
1
解决办法
644
查看次数

标签 统计

c++ ×1

c-preprocessor ×1

go ×1

new-operator ×1

stl ×1