小编ycs*_*hao的帖子

如果在构造函数中抛出异常,防止内存泄漏的最佳做法是什么?

我知道如果在构造函数中抛出异常,则不会调用析构函数(简单类,没有继承).因此,如果在构造函数中抛出异常,并且有可能没有清理某些堆内存.那么这里最好的做法是什么?让我们假设我必须在构造函数中调用一些函数,它可能会抛出异常.在这种情况下,我总是使用共享指针吗?有什么选择?谢谢!

c++ memory constructor destructor exception-handling

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

mac 上用户“postgres”的密码验证失败

我在创建新的 psql 用户时遇到问题,因为我无法以“postgres”的身份登录 psql,我试过了

1. sudo -u postgres psql

2. sudo -u postgres createuser img_site -P -s -e
Run Code Online (Sandbox Code Playgroud)

他们都要求输入我不知道的“postgres”密码。我试图更改用户“postgres”的 unix 密码(我知道这很危险),但它仍然告诉我:用户“postgres”的密码身份验证失败。我也试过 GUI pgAdmin 但它是同样的错误。

我不知道是否相关:我创建了一个符号链接

sudo ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/
Run Code Online (Sandbox Code Playgroud)

为了摆脱错误

createuser: could not connect to database postgres: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
Run Code Online (Sandbox Code Playgroud)

postgresql macos psql

8
推荐指数
1
解决办法
3万
查看次数

如何将多个问题分配给 github 中的项目

我找不到可以批量选择多个问题并将它们分配给项目的选项。我只能批量分配标签和其他东西。

github github-projects github-issues

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

取消引用NULL指针是否保证在C/C++中崩溃程序?

我遇到了这个有线代码而且没有崩溃.

#include <stdio.h>
struct s
{
    char* c;
    char* c2;
};

int main()
{
    struct s* p = NULL;
    printf("%d\n", &(p->c));
    printf("%d\n", &p->c2);
    printf("%d\n", &(*p).c2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

0
4
4
Run Code Online (Sandbox Code Playgroud)

我有几个问题来找我,我无法回答:

  1. 在c/c ++中NULL指针总是等于0吗?

  2. 如果0恰好是变量的地址会发生什么?

  3. 输出似乎是结构成员的偏移地址.这是如何计算的.我的意思是p-> c是c的地址,它不存在,因为p == NULL.如果c的地址不存在,你怎么能通过&p-> c得到c的地址?

  4. 取消引用NULL指针是否保证在C/C++中使程序崩溃?(让我们假设在系统中)

c c++ null struct pointers

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

为什么选择不是HTML中的输入类型?

为什么例如<select>并且<textarea>是独立标签而不是属性<input>

html tags html5 attributes

6
推荐指数
2
解决办法
1099
查看次数

使用--inspect-brk选项时,WebSockets请求是预期的错误

当我运行nodemon dist/server/app.js它时,它可以在默认端口上运行,3000并且可以访问我的API。但是,如果我运行nodemon --inspect-brk=localhost:3000 dist/server/app.js,则会收到错误消息,提示“期望WebSockets请求”。怎么了?

debugging node.js nodemon

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

如何从Bearer令牌中提取令牌字符串?

例如,我在标头中跟随Bearer JWT,提取令牌本身的一种优雅方法是什么?以后基本上什么都没有Bearer。由于这可以采用其他格式,因此我不希望它始终以开头Bearer。我正在使用node-jsonwebtoken,但没有找到这种方法。

Authorization: Bearer eyJhbGciOiJIUzI1NiIXVCJ9...TJVA95OrM7E20RMHrHDcEfxjoYZgeFONFh7HgQ

node.js jwt

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

用于循环语句警告的冒泡排序算法

我看到了一个冒泡排序代码,最初我认为代码是错误的.但是在编译和运行之后,它让我惊讶它确实有效.我想知道第一个for循环中的第二个语句如何不是条件而是赋值.另外,为什么这段代码不会进入无限循环?

PS:它会产生一个警告:"建议围绕赋值的括号用作真值[-Whatarentheses]"抱怨第一个for循环.令人惊讶的是,这不是一个错误.

#include <iostream>

void bubblesort(int A[], int n)
{
    for (bool sorted = false; sorted = !sorted; n--)
    {
        for (int i = 1; i < n; ++i)
        {
            if (A[i-1] > A[i])
            {
                int tmp = 0;
                tmp = A[i];
                A[i] = A[i-1];
                A[i-1] = tmp;
                sorted = false;
            }
        }
    }
}

int main()
{
    int a[5] = {1,4,5,2,3};

    bubblesort(a, 5);

    for (unsigned int i = 0; i < 5; ++i)
    {
        std::cout << a[i] << std::endl; …
Run Code Online (Sandbox Code Playgroud)

c++ sorting algorithm bubble-sort

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

为什么条件运算符作为参数传入时会被视为bool?

我有两个重载功能

void foo(std::string value);
void foo(bool value);
Run Code Online (Sandbox Code Playgroud)

我打电话的时候

foo(true ? "a" : "b");
Run Code Online (Sandbox Code Playgroud)

为什么函数需要一个布尔值而不是字符串?

c++ overloading conditional-operator function-parameter

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

为什么在传递参数时使用const会给我一个警告?

为什么这段代码会给我一个警告:从不兼容的指针类型传递"test"的参数1?我知道这是关于char之前的const,但为什么呢?

void test(const int ** a)
{
}
int main()
{
    int a=0;
    int *b=&a;
    int **c=&b;
    test(c);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c pointers const function

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

为什么必须导出我在angular appModule导入模块中使用的功能?

我有以下代码

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    RoutingModule,
    SharedModule,
    JwtModule.forRoot({
      config: {
        headerName: 'Authorization',
        tokenGetter: () => localStorage.getItem('token’), // <———— this line has problem
        whitelistedDomains: ['localhost:4200']
        //blacklistedRoutes: ['localhost:3001/auth/', 'foo.com/bar/']
      }
    })
  ],
  ...
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

它可以使用ng serve,但运行时出现以下错误ng build --prod

ERROR in Error during template compile of 'AppModule'
  Function expressions are not supported in decorators in '?0'
    '?0' contains the error at app/app.module.ts(36,22)
      Consider changing the function expression into an exported function.
Run Code Online (Sandbox Code Playgroud)

然后我将代码修改为 …

javascript angular

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

布尔AND和OR运算

为什么以下代码输出1,而不是0a || b应该给我1,1 && 00吗,对吗?我不认为逻辑操作从右到左进行评估.

int main()
{
    printf("%d\n", 1 || 1 && 0);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c binary

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

std :: string :: npos值不是-1

在这个文档http://www.cplusplus.com/reference/string/string/npos/ 中已经说过std :: string :: npos是-1.但是当我打印出价值时,它不是.这个价值架构是否依赖?

我的测试非常简单

std::cout << std::string::npos << std::endl;
Run Code Online (Sandbox Code Playgroud)

哪个输出

4294967295
Run Code Online (Sandbox Code Playgroud)

c++ string

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