为什么g ++不在乎初始化列表分配给std :: string&和std :: string?和其他怪异

如何解决为什么g ++不在乎初始化列表分配给std :: string&和std :: string?和其他怪异

我在做作业时遇到了这个问题。当您有一个继承了类A的类B,并且B通过在B的构造函数中调用A的构造函数来初始化A中的变量时,应该在A的构造函数中初始化的变量仍未初始化。如果我们创建类A的对象,似乎不会发生这种情况,即使看起来唯一的区别是继承和构造函数链。

这是一个最小的示例:

#include <iostream>
#include <string>

class A {
public:
    A(std::string s)
    : s_(s) {}

    ~A(){}

    void Print() const {
        std::cout << s_ + "123";
    }
private:
    const std::string& s_;
};

class B : public A {
public:
    B(std::string s)
    : A(s) {}
};

int main()
{
  //A a = A("123");
  //a.Print();
  B b = B("123");
  b.Print();
}

在Print()中,您可以删除多余的字符串文字。这样,问题就集中在未初始化的变量上。但是根据valgrind的说法,有了额外的文字,仍然可以达到一百万个字节。这看起来很奇怪。

在Print()中没有文字的Valgrind:

g++ -c -g -std=c++17 -Wall -Wextra -pedantic main.cpp -o main.o
g++ main.o   -o main
valgrind ./main
==22551== Memcheck,a memory error detector
==22551== Copyright (C) 2002-2017,and GNU GPL'd,by Julian Seward et al.
==22551== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==22551== Command: ./main
==22551==
==22551== error calling PR_SET_PTRACER,vgdb might block
==22551== Conditional jump or move depends on uninitialised value(s)
==22551==    at 0x4F4FA9A: std::basic_ostream<char,std::char_traits<char> >& std::__ostream_insert<char,std::char_traits<char> >(std::basic_ostream<char,std::char_traits<char> >&,char const*,long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551==    by 0x109BAF: A::Print() const (main.cpp:15)
==22551==    by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551==    at 0x545C928: fwrite (iofwrite.c:35)
==22551==    by 0x4F4FB83: std::basic_ostream<char,long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551==    by 0x109BAF: A::Print() const (main.cpp:15)
==22551==    by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551==    at 0x54689B4: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1226)
==22551==    by 0x545C9E6: fwrite (iofwrite.c:39)
==22551==    by 0x4F4FB83: std::basic_ostream<char,long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551==    by 0x109BAF: A::Print() const (main.cpp:15)
==22551==    by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551==    at 0x5468A85: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1275)
==22551==    by 0x545C9E6: fwrite (iofwrite.c:39)
==22551==    by 0x4F4FB83: std::basic_ostream<char,long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551==    by 0x109BAF: A::Print() const (main.cpp:15)
==22551==    by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551==    at 0x5468210: _IO_file_write@@GLIBC_2.2.5 (fileops.c:1198)
==22551==    by 0x5468B9E: new_do_write (fileops.c:457)
==22551==    by 0x5468B9E: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1277)
==22551==    by 0x545C9E6: fwrite (iofwrite.c:39)
==22551==    by 0x4F4FB83: std::basic_ostream<char,long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551==    by 0x109BAF: A::Print() const (main.cpp:15)
==22551==    by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Syscall param write(buf) contains uninitialised byte(s)
==22551==    at 0x54ED264: write (write.c:27)
==22551==    by 0x546822C: _IO_file_write@@GLIBC_2.2.5 (fileops.c:1203)
==22551==    by 0x5468B9E: new_do_write (fileops.c:457)
==22551==    by 0x5468B9E: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1277)
==22551==    by 0x545C9E6: fwrite (iofwrite.c:39)
==22551==    by 0x4F4FB83: std::basic_ostream<char,long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551==    by 0x109BAF: A::Print() const (main.cpp:15)
==22551==    by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Syscall param write(count) contains uninitialised byte(s)
==22551==    at 0x54ED264: write (write.c:27)
==22551==    by 0x546822C: _IO_file_write@@GLIBC_2.2.5 (fileops.c:1203)
==22551==    by 0x5468B9E: new_do_write (fileops.c:457)
==22551==    by 0x5468B9E: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1277)
==22551==    by 0x545C9E6: fwrite (iofwrite.c:39)
==22551==    by 0x4F4FB83: std::basic_ostream<char,long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551==    by 0x109BAF: A::Print() const (main.cpp:15)
==22551==    by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Syscall param write(buf) points to uninitialised byte(s)
==22551==    at 0x54ED264: write (write.c:27)
==22551==    by 0x546822C: _IO_file_write@@GLIBC_2.2.5 (fileops.c:1203)
==22551==    by 0x5468B9E: new_do_write (fileops.c:457)
==22551==    by 0x5468B9E: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1277)
==22551==    by 0x545C9E6: fwrite (iofwrite.c:39)
==22551==    by 0x4F4FB83: std::basic_ostream<char,long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551==    by 0x109BAF: A::Print() const (main.cpp:15)
==22551==    by 0x1099EB: main (main.cpp:32)
==22551==  Address 0x1ffefff910 is on thread 1's stack
==22551==  in frame #5,created by A::Print() const (main.cpp:14)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551==    at 0x5468BB0: new_do_write (fileops.c:458)
==22551==    by 0x5468BB0: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1277)
==22551==    by 0x545C9E6: fwrite (iofwrite.c:39)
==22551==    by 0x4F4FB83: std::basic_ostream<char,long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551==    by 0x109BAF: A::Print() const (main.cpp:15)
==22551==    by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551==    at 0x5468BEB: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1279)
==22551==    by 0x545C9E6: fwrite (iofwrite.c:39)
==22551==    by 0x4F4FB83: std::basic_ostream<char,long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551==    by 0x109BAF: A::Print() const (main.cpp:15)
==22551==    by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551==    at 0x545CA65: fwrite (iofwrite.c:45)
==22551==    by 0x4F4FB83: std::basic_ostream<char,long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551==    by 0x109BAF: A::Print() const (main.cpp:15)
==22551==    by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551==    at 0x545CA6A: fwrite (iofwrite.c:45)
==22551==    by 0x4F4FB83: std::basic_ostream<char,long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551==    by 0x109BAF: A::Print() const (main.cpp:15)
==22551==    by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551==    at 0x4F4FB91: std::basic_ostream<char,long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551==    by 0x109BAF: A::Print() const (main.cpp:15)
==22551==    by 0x1099EB: main (main.cpp:32)
==22551==
==22551==
==22551== HEAP SUMMARY:
==22551==     in use at exit: 0 bytes in 0 blocks
==22551==   total heap usage: 2 allocs,2 frees,73,216 bytes allocated
==22551==
==22551== All heap blocks were freed -- no leaks are possible
==22551==
==22551== For counts of detected and suppressed errors,rerun with: -v
==22551== Use --track-origins=yes to see where uninitialised values come from
==22551== ERROR SUMMARY: 13 errors from 13 contexts (suppressed: 0 from 0)

在Print()中带有文字的Valgrind:

(为了适应字符数限制,我不得不将中间部分切掉,整个内容在这里:https://pastebin.com/UQmB0mXj

g++ -c -g -std=c++17 -Wall -Wextra -pedantic main.cpp -o main.o
g++ main.o   -o main
valgrind ./main
==22561== Memcheck,a memory error detector
==22561== Copyright (C) 2002-2017,by Julian Seward et al.
==22561== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==22561== Command: ./main
==22561==
==22561== error calling PR_SET_PTRACER,vgdb might block
==22561== Conditional jump or move depends on uninitialised value(s)
==22561==    at 0x10AC50: void std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char> >::_M_construct<char*>(char*,char*,std::forward_iterator_tag) (basic_string.tcc:217)
==22561==    by 0x10A7DD: void std::__cxx11::basic_string<char,std::allocator<char> >::_M_construct_aux<char*>(char*,std::__false_type) (basic_string.h:236)
==22561==    by 0x10A472: void std::__cxx11::basic_string<char,char*) (basic_string.h:255)
==22561==    by 0x10A15B: std::__cxx11::basic_string<char,std::allocator<char> >::basic_string(std::__cxx11::basic_string<char,std::allocator<char> > const&) (basic_string.h:440)
==22561==    by 0x10A05E: std::__cxx11::basic_string<char,std::allocator<char> > std::operator+<char,std::allocator<char> >(std::__cxx11::basic_string<char,std::allocator<char> > const&,char const*) (basic_string.h:5928)
==22561==    by 0x109E66: A::Print() const (main.cpp:15)
==22561==    by 0x109C8B: main (main.cpp:32)
==22561==
==22561== Conditional jump or move depends on uninitialised value(s)
==22561==    at 0x10AD84: std::__cxx11::basic_string<char,std::allocator<char> >::_M_create(unsigned long&,unsigned long) (basic_string.tcc:137)
==22561==    by 0x10AC69: void std::__cxx11::basic_string<char,std::forward_iterator_tag) (basic_string.tcc:219)
==22561==    by 0x10A7DD: void std::__cxx11::basic_string<char,char const*) (basic_string.h:5928)
==22561==    by 0x109E66: A::Print() const (main.cpp:15)
==22561==    by 0x109C8B: main (main.cpp:32)
==22561==
...
had to cut here to fit into character limit
...
==22561==
==22561== Conditional jump or move depends on uninitialised value(s)
==22561==    at 0x10A9F0: std::__cxx11::basic_string<char,std::allocator<char> >::_S_copy(char*,unsigned long) (basic_string.h:337)
==22561==    by 0x10AED7: std::__cxx11::basic_string<char,std::allocator<char> >::_S_copy_chars(char*,char*) (basic_string.h:382)
==22561==    by 0x10ACB0: void std::__cxx11::basic_string<char,std::forward_iterator_tag) (basic_string.tcc:225)
==22561==    by 0x10A7DD: void std::__cxx11::basic_string<char,char const*) (basic_string.h:5928)
==22561==    by 0x109E66: A::Print() const (main.cpp:15)
==22561==    by 0x109C8B: main (main.cpp:32)
==22561==
==22561== Conditional jump or move depends on uninitialised value(s)
==22561==    at 0x109DE1: std::char_traits<char>::copy(char*,unsigned long) (char_traits.h:348)
==22561==    by 0x10AA1D: std::__cxx11::basic_string<char,unsigned long) (basic_string.h:340)
==22561==    by 0x10AED7: std::__cxx11::basic_string<char,char const*) (basic_string.h:5928)
==22561==    by 0x109E66: A::Print() const (main.cpp:15)
==22561==    by 0x109C8B: main (main.cpp:32)
==22561==
==22561== Conditional jump or move depends on uninitialised value(s)
==22561==    at 0x4C366E6: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22561==    by 0x109E05: std::char_traits<char>::copy(char*,unsigned long) (char_traits.h:350)
==22561==    by 0x10AA1D: std::__cxx11::basic_string<char,char const*) (basic_string.h:5928)
==22561==    by 0x109E66: A::Print() const (main.cpp:15)
==22561==    by 0x109C8B: main (main.cpp:32)
==22561==
==22561== Conditional jump or move depends on uninitialised value(s)
==22561==    at 0x4C36711: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22561==    by 0x109E05: std::char_traits<char>::copy(char*,char const*) (basic_string.h:5928)
==22561==    by 0x109E66: A::Print() const (main.cpp:15)
==22561==    by 0x109C8B: main (main.cpp:32)
==22561==
==22561== Conditional jump or move depends on uninitialised value(s)
==22561==    at 0x4C367EE: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22561==    by 0x109E05: std::char_traits<char>::copy(char*,char const*) (basic_string.h:5928)
==22561==    by 0x109E66: A::Print() const (main.cpp:15)
==22561==    by 0x109C8B: main (main.cpp:32)
==22561==
==22561== Invalid read of size 8
==22561==    at 0x4C367EE: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22561==    by 0x109E05: std::char_traits<char>::copy(char*,char const*) (basic_string.h:5928)
==22561==    by 0x109E66: A::Print() const (main.cpp:15)
==22561==    by 0x109C8B: main (main.cpp:32)
==22561==  Address 0x1fff001000 is not stack'd,malloc'd or (recently) free'd
==22561==
==22561==
==22561== Process terminating with default action of signal 11 (SIGSEGV)
==22561==  Access not within mapped region at address 0x1FFF001000
==22561==    at 0x4C367EE: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22561==    by 0x109E05: std::char_traits<char>::copy(char*,char const*) (basic_string.h:5928)
==22561==    by 0x109E66: A::Print() const (main.cpp:15)
==22561==    by 0x109C8B: main (main.cpp:32)
==22561==  If you believe this happened as a result of a stack
==22561==  overflow in your program's main thread (unlikely but
==22561==  possible),you can try to increase the size of the
==22561==  main thread stack using the --main-stacksize= flag.
==22561==  The main thread stack size used in this run was 8388608.
==22561==
==22561== HEAP SUMMARY:
==22561==     in use at exit: 1,089,515 bytes in 1 blocks
==22561==   total heap usage: 2 allocs,1 frees,1,162,219 bytes allocated
==22561==
==22561== LEAK SUMMARY:
==22561==    definitely lost: 0 bytes in 0 blocks
==22561==    indirectly lost: 0 bytes in 0 blocks
==22561==      possibly lost: 0 bytes in 0 blocks
==22561==    still reachable: 1,515 bytes in 1 blocks
==22561==         suppressed: 0 bytes in 0 blocks
==22561== Rerun with --leak-check=full to see details of leaked memory
==22561==
==22561== For counts of detected and suppressed errors,rerun with: -v
==22561== Use --track-origins=yes to see where uninitialised values come from
==22561== ERROR SUMMARY: 197 errors from 12 contexts (suppressed: 0 from 0)
src.make:32: recipe for target 'valgrind-run' failed
make: *** [valgrind-run] Segmentation fault (core dumped)

我的问题是:

  1. 为什么由于A的构造函数,编译器没有给出错误?如何设置带有值的引用?

  2. 为什么仅在涉及继承时才给出错误?

  3. 为什么在Print()中添加文字会产生如此大的影响?以及为什么要为此分配一百万个字节?

解决方法

  1. 为什么编译器由于A的构造函数而不给出错误?

大概是因为A的构造函数格式正确,因此编译器必须接受它才能符合标准。

允许您将引用绑定到局部变量。构造函数返回后,引用将无效,但是,如果程序从构造函数返回后再也没有通过引用间接进行引用,则从技术上讲这没有问题。证明编译器能够做到这一点并非难事(这个问题通常类似于停止问题)。

编译器会通过以下方式发出警告:

警告:将引用成员's_'绑定到堆栈分配的参数's'[-Wdangling-field]


如何设置带有值的引用?

您已将引用绑定到局部变量。目前尚不清楚您要做什么,但可能不应该使用引用成员来实现。


  1. 为什么...
  1. 为什么...

因为该程序的行为是不确定的。任何更改都可能以任何方式影响程序的行为。行为甚至可以更改,而无需更改程序。否则它可能不会改变。不能保证程序的行为。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-