据我了解,char
C++ 中的 a 需要 1 个字节。所以一个包含 IP 地址的字符串应该有 15 个字节的大小。考虑这段代码:
char *temp = new char[15];
ifstream iss("file.txt"); //contains an ip address
iss >> temp;
cout << "temp : "<< temp << " ";
cout << sizeof (temp) << endl;
它显示大小为 4。有人可以解释为什么吗?
然后我尝试了相同的方法,只是这次我只分配了 4 个字节temp
:
char *temp = new char[4];
这次结果是之前的结果,附加了:
*** glibc detected *** /home/HelloWorld: double free or corruption (out): 0x08557030 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb758bee2]
/lib/i386-linux-gnu/libc.so.6(fclose+0x154)[0xb757b424]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSt12__basic_fileIcE5closeEv+0x47)[0xb7746b87]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSt13basic_filebufIcSt11char_traitsIcEE5closeEv+0x98)[0xb7784328]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSt14basic_ifstreamIcSt11char_traitsIcEED1Ev+0x3f)[0xb77846af]
/home/HelloWorld[0x8048e2d]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb752f4d3]
/home/HelloWorld[0x8048ab1]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:01 536973 /home/HelloWorld
0804a000-0804b000 r--p 00001000 08:01 536973 /home/HelloWorld
0804b000-0804c000 rw-p 00002000 08:01 536973 /home/HelloWorld
08557000-08578000 rw-p 00000000 00:00 0 [heap]
b74e8000-b74ea000 rw-p 00000000 00:00 0
b74ea000-b7514000 r-xp 00000000 08:01 655846 /lib/i386-linux-gnu/libm-2.15.so
b7514000-b7515000 r--p 00029000 08:01 655846 /lib/i386-linux-gnu/libm-2.15.so
b7515000-b7516000 rw-p 0002a000 08:01 655846 /lib/i386-linux-gnu/libm-2.15.so
b7516000-b76ba000 r-xp 00000000 08:01 655851 /lib/i386-linux-gnu/libc-2.15.so
b76ba000-b76bc000 r--p 001a4000 08:01 655851 /lib/i386-linux-gnu/libc-2.15.so
b76bc000-b76bd000 rw-p 001a6000 08:01 655851 /lib/i386-linux-gnu/libc-2.15.so
b76bd000-b76c1000 rw-p 00000000 00:00 0
b76c1000-b76dc000 r-xp 00000000 08:01 655821 /lib/i386-linux-gnu/libgcc_s.so.1
b76dc000-b76dd000 r--p 0001a000 08:01 655821 /lib/i386-linux-gnu/libgcc_s.so.1
b76dd000-b76de000 rw-p 0001b000 08:01 655821 /lib/i386-linux-gnu/libgcc_s.so.1
b76de000-b77bb000 r-xp 00000000 08:01 792998 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.18
b77bb000-b77bf000 r--p 000dc000 08:01 792998 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.18
b77bf000-b77c0000 rw-p 000e0000 08:01 792998 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.18
b77c0000-b77c7000 rw-p 00000000 00:00 0
b77d5000-b77d9000 rw-p 00000000 00:00 0
b77d9000-b77da000 r-xp 00000000 00:00 0 [vdso]
b77da000-b77fa000 r-xp 00000000 08:01 655841 /lib/i386-linux-gnu/ld-2.15.so
b77fa000-b77fb000 r--p 0001f000 08:01 655841 /lib/i386-linux-gnu/ld-2.15.so
b77fb000-b77fc000 rw-p 00020000 08:01 655841 /lib/i386-linux-gnu/ld-2.15.so
bff69000-bff8a000 rw-p 00000000 00:00 0 [stack]
我猜是因为内存分配不足。有人可以解释一下这里是如何管理内存的吗?