小编And*_*rew的帖子

在哪种情况下删除指针

我的以下问题是关于内存管理.我有一个int变量没有在类中动态分配,比如说invar1.我将此int的内存地址传递给另一个类构造函数.那个班级做到了这个:

class ex1{
    ex1(int* p_intvar1)
    {
       ptoint = p_intvar1;
    }

    int* ptoint;
};
Run Code Online (Sandbox Code Playgroud)

我应该删除ptoint吗?因为它具有动态分配的int的地址,所以我认为我不需要删除它.

我再次向一个带有new运算符的类声明一个对象:

objtoclass = new ex1();
Run Code Online (Sandbox Code Playgroud)

我将它传递给另一个班级:

class ex2{
    ex2(ex1* p_obj)
    {
       obj = p_obj;
    }

    ex1* obj;
};
Run Code Online (Sandbox Code Playgroud)

当我已经删除了objtoclass时,我应该删除obj吗?

谢谢!

c++

14
推荐指数
2
解决办法
894
查看次数

setw() 不影响读取整数字段

我写了这样的代码:

int d{ 0 };
cin >> setw(2) >> d;
Run Code Online (Sandbox Code Playgroud)

但它似乎setw()对读取整数没有影响。如果是这样,我们如何可以实施的行为%2dscanf()istream

c++ stl

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

初始化随机发生器的最佳位置

在我的程序中,我使用了一个随机数生成器.我认为一般规则是你应该将事物定义为接近它们被"调用"的地方,但这对于随机数生成器是否也适用?

例如,在我的代码中,我可以选择:

std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<int> uni(-2147483647, 2147483646);

lots of code

    for (i = 0; i < 10000; i++)
    {
        variable x = uni(rng);
    }
Run Code Online (Sandbox Code Playgroud)

要么

lots of code

    for (i = 0; i < 10000; i++)
    {
        std::random_device rd;
        std::mt19937 rng(rd());
        std::uniform_int_distribution<int> uni(-2147483647, 2147483646); 
        variable x = uni(rng);
    }
Run Code Online (Sandbox Code Playgroud)

我会说第一种方法更快,但由于阅读了很多线程,我觉得它总是将所有内容放在靠近它所调用的地方.

c++ performance

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

分段错误 - C错误中的字符串连接

我是C编程的新手,仍然试图理解C的所有角落和缝隙.我正在编写一个程序来连接两个字符串.但我收到一个我不明白的错误.这是输出.

Asfakul
字符串名称
的长度为7字符串全名的长度为7
L
分段错误(核心转储)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int  main(int argc, char const *argv[])
{
  char *name="Asfakul";
  char *surname="Laskar";
  char *fullname;
  int i=0;
  //Allocate Memory of 100 char 
  fullname=(char*)malloc(100*sizeof(char));
  fullname=name;
  while(*name !='\0')
  {
    i++;
    name++;
  }

  // Allocate Memory for FullName

  //fullname=(char*)malloc(100*sizeof(char));
  //Coppied the spurce String
 // fullname=name; // Here this assignement will not work as Pointer name now point to NULL character of String Name.
  puts(fullname);
  printf("The Length of the String name is %d\n",i );
  printf("The Length of …
Run Code Online (Sandbox Code Playgroud)

c

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

编译器警告字符串转换 - 是因为双引号?

如果我输入5,我希望它显示"May",但他们显示的是始终警告,我怎样才能使它工作?

#include <iostream>

using namespace std;

int main() {
    int month;
    char *m_name[] = {"   ", "Jan", "Feb", "Mar", "Apr", "May",
                      "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    cout << "Enter month: ";
    cin >> month;
    if (month >= 1 && month <= 12)
        cout << m_name[month];
    else cout << "Illegal month";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器警告:

main.cpp:8:70: warning: ISO C++ forbids converting a string constant to 'char*' [-Wpedantic]
                       "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
                                                                      ^
main.cpp:8:70: warning: ISO C++ …
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c++ ×4

c ×1

performance ×1

stl ×1