我不是计算机专业的,我现在学习C/C++,关于内存分配的我不是很懂,好像要补好多底层知识,比如对程序运行在内存中的活动不是很明白,程序在内存中是如何分配的,具体哪些在所谓的栈上,哪些在堆上。比如:gdb调试codedump文件时,我不知如何定位到问题所在,一大堆错误。只知道看到跟我写的代码有关的部分去追踪一定没错了。从bt跟踪出来的那些错误信息,从下往上看,分别代表的是什么东西?
上面只是说一个例子,就是这方面我不懂,也不知如何去学习。希望大家给我推荐一些非科班能看懂的书籍,不要动不动就 计算机体系结构,深入理解计算机系统...等等这类,这些书我尝试去看过了,看不懂~
比如下面这一段,bt后一共有22处提示,但我应该一开始从哪里追踪?应该是第#1处吗?,第#1,#2处就是具体实现业务的方法,为什么它会排在上最面?:
#0 0x000000368c09c0d3 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const ()
from /usr/lib64/libstdc++.so.6
#1 0x000000000059fccb in SongIndexUpdate::BuildSearchIndex (this=0x1b584e0, strlist=std::vector of length 89, capacity 128 = {...},
item=...) at SongIndexUpdate.cpp:1209
#2 0x000000000059d533 in SongIndexUpdate::FormatCommitData (this=0x1b584e0, line=
"Classic Jazz - Black Orpheus|31907DEDB85782ADD036F687AB5E5CAB|0|4015213|128|mp3|251|||127|0|100|1030544|Classic Jazz|Black Orpheus|2|1|0|0|D0A5848270229311DAC2D7BADCD4949D|10037541||0|1|2|0|0|0||0|185"..., item=..., num=5, maxOwnerCount=1882587, M4AFilter=0, type="song")
at SongIndexUpdate.cpp:953
#3 0x000000000059a6b8 in SongIndexUpdate::FormatUpdateData (this=0x1b584e0, line=
"Classic Jazz - Black Orpheus|31907DEDB85782ADD036F687AB5E5CAB|0|4015213|128|mp3|251|||127|0|100|1030544|Classic Jazz|Black Orpheus|2|1|0|0|D0A5848270229311DAC2D7BADCD4949D|10037541||0|1|2|0|0|0||0|185"..., item=..., num=5, index="song", maxOwnerCount=1882587, HitID="",
type="song", needEncode=false) at SongIndexUpdate.cpp:777
#4 0x0000000000597ead in SongIndexUpdate::UpdateToService (this=0x1b584e0,
filename=0x7f2c20ca0538 "/data/ops/Update-1.4.60/bin/../data/IndexUpdate/SongIncrDataTmp.txt", index="song", type="song",
cluster="master", flag=1, needEncode=false) at SongIndexUpdate.cpp:562
#5 0x00000000005931b0 in SongIndexUpdate::OnRecvImportStrings (this=0x1b584e0, req=..., rsp=..., needEncode=false)
at SongIndexUpdate.cpp:183
#6 0x00000000004de823 in CNetCMDImpl::OnHTTPRequest (this=0xb56700, req=..., rsp=...) at CNetCMDEx.cpp:50
#7 0x00000000004d46ee in CNetCMDImpl::OnHTTP2UDPRequest (this=0xb56700, req=..., rsp=...) at CNetCMD.cpp:346
#8 0x00000000004d832e in boost::_mfi::mf2<void, CNetCMDImpl, PReqtInfo&, PRspnInfo&>::operator() (this=0x1b57458, p=0xb56700, a1=...,
a2=...) at /usr/include/boost/bind/mem_fn_template.hpp:280
#9 0x00000000004d7880 in boost::_bi::list3<boost::_bi::value<CNetCMDImpl*>, boost::arg<1>, boost::arg<2> >::operator()<boost::_mfi::mf2<void, CNetCMDImpl, PReqtInfo&, PRspnInfo&>, boost::_bi::list2<boost::shared_ptr<CReqtInfo>&, boost::shared_ptr<CRspnInfo>&> > (
this=0x1b57468, f=..., a=...) at /usr/include/boost/bind/bind.hpp:392
#10 0x00000000004d7264 in boost::_bi::bind_t<void, boost::_mfi::mf2<void, CNetCMDImpl, PReqtInfo&, PRspnInfo&>, boost::_bi::list3<boost::_bi::value<CNetCMDImpl*>, boost::arg<1>, boost::arg<2> > >::operator()<boost::shared_ptr<CReqtInfo>, boost::shared_ptr<CRspnInfo> > (
this=0x1b57458, a1=..., a2=...) at /usr/include/boost/bind/bind_template.hpp:61
#11 0x00000000004d6e58 in boost::detail::function::void_function_obj_invoker2<boost::_bi::bind_t<void, boost::_mfi::mf2<void, CNetCMDImpl, PReqtInfo&, PRspnInfo&>, boost::_bi::list3<boost::_bi::value<CNetCMDImpl*>, boost::arg<1>, boost::arg<2> > >, void, PReqtInfo&, PRspnInfo&>::invoke (function_obj_ptr=..., a0=..., a1=...) at /usr/include/boost/function/function_template.hpp:153
#12 0x00000000005ceb3e in boost::function2<void, PReqtInfo&, PRspnInfo&>::operator() (this=0x1b57450, a0=..., a1=...)
at /usr/include/boost/function/function_template.hpp:767
---Type <return> to continue, or q <return> to quit---
#13 0x00000000005cdd95 in MutiThrHttpSvr::HttpServer (req=0x7f2c203d6ee0, arg=0x1b573e8) at HttpServer.cpp:642
#14 0x00000000006c7ed5 in evhttp_handle_request (req=0x7f2c203d6ee0, arg=<value optimized out>) at http.c:3036
#15 0x00000000006c70f7 in evhttp_read_body (evcon=0x7f2c2006d6e0, req=0x7f2c203d6ee0) at http.c:1028
#16 0x00000000006c8505 in bufferevent_readcb (fd=<value optimized out>, event=<value optimized out>, arg=0x7f2c20048690)
at bufferevent_sock.c:186
#17 0x00000000006b3340 in event_process_active_single_queue (base=0x733ebf0, flags=0) at event.c:1350
#18 event_process_active (base=0x733ebf0, flags=0) at event.c:1420
#19 event_base_loop (base=0x733ebf0, flags=0) at event.c:1621
#20 0x00000000005cd933 in MutiThrHttpSvr::EventDispatch (arg=0x733ebf0) at HttpServer.cpp:589
#21 0x000000368a407aa1 in start_thread () from /lib64/libpthread.so.0
#22 0x000000368a0e893d in clone () from /lib64/libc.so.6
阿神2017-04-17 14:58:37
It is quite difficult to get multi-threading, Socket, and IO right from the start, especially if the foundation is weak. Not to mention you, even programmers with other programming skills will find it difficult to learn C and C++ at the bottom level.
I have to say how to learn. To be honest, I don’t know. Maybe the foundation is still the most important. Computer majors mainly focus on learning algorithms, data structures, network basics, operating system basics, and even compilation principles and microcomputer assembly. These things are very helpful for you to understand many things. It doesn't mean that you have to learn these, but since you are doing C C++, you have no choice but to understand it. There is no shortcut. Unless you are doing high-level business development, it can be much easier based on various frameworks.
I personally recommend not to learn C++ first, because C++ is more difficult. C is relatively pure. Although C is not suitable for developing large-scale projects, it is very important for you to understand the essence of things.
The way to learn is to continue programming, programming and reprogramming. I really don’t agree with reading those boring and complicated low-level books first (algorithm data structures are not low-level books). You should write code, encounter problems, check books, and then write code, this cycle.
The early stage is the best way to practice data structures and algorithms and build your programming mindset. This stage can be done under windows, and relying on IDE can lower the learning threshold to a certain extent.
Then try to develop some small application programs. Such programs often start to use system-level API calls. You can follow this order: file reading and writing, network client program, process communication control, multi-threaded program . At this stage you need to make a choice between windows and unix platforms. My suggestion is to choose unix or linux. At this stage, while practicing, read the books recommended by the masters and slowly digest it. The program does not need to make an interface, it is a waste of time.
If you are interested, you must learn some assembly, and you will really understand how computers work. How every line of C code you write maps to an instruction.
Start looking at some open source programs to see how they are written. If you don’t understand the code, check books or the Internet.
Then switch to C++ and that’s almost it.
伊谢尔伦2017-04-17 14:58:37
程序在内存中是如何分配的,具体哪些在所谓的栈上,哪些在堆上
这个看c/c++书关于内存管理的章节就知道。
从bt跟踪出来的那些错误信息,从下往上看,分别代表的是什么东西
这个要看Call stack 和 stacktrace
https://en.wikipedia.org/wiki/Call_stack
https://en.wikipedia.org/wiki/Stack_trace
但我应该一开始从哪里追踪?应该是第#1处吗?
这个就是从楼上学习过程累积的debugging技巧了。