Reputation: 73
I want to go through a vector of string and split each string by space. but it throws an error, I use print to find the error:
for(int client_i=0; client_i<l.size();client_i++){
...
}
The error does not occur in client_i=0, as it can print out all successfully message once, but I am sure the vector size is 2, so it should print all successfully message twice.
======= Backtrace: =========
/lib64/libc.so.6(+0x81609)[0x7f05ad207609]
/lib64/libstdc++.so.6(_ZNSsD1Ev+0x43)[0x7f05adb28b63]
./assignment1[0x404d8a]
./assignment1[0x4044ba]
./assignment1[0x403d17]
./assignment1[0x403477]
./assignment1[0x402e46]
./assignment1[0x40645c]
./assignment1[0x406017]
./assignment1[0x4065ee]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f05ad1a8495]
./assignment1[0x401e79]
======= Memory map: ========
00400000-00409000 r-xp 00000000 00:2a 49026461 /home/csdue/yichenwa/cse489589_assignment1/yichenwa/assignment1
00609000-0060a000 rw-p 00009000 00:2a 49026461 /home/csdue/yichenwa/cse489589_assignment1/yichenwa/assignment1
00c43000-00c64000 rw-p 00000000 00:00 0 [heap]
7f05a8000000-7f05a8021000 rw-p 00000000 00:00 0
7f05a8021000-7f05ac000000 ---p 00000000 00:00 0
7f05acb53000-7f05acb69000 r-xp 00000000 08:08 1835074 /usr/lib64/libresolv-2.17.so
7f05acb69000-7f05acd68000 ---p 00016000 08:08 1835074 /usr/lib64/libresolv-2.17.so
7f05acd68000-7f05acd69000 r--p 00015000 08:08 1835074 /usr/lib64/libresolv-2.17.so
7f05acd69000-7f05acd6a000 rw-p 00016000 08:08 1835074 /usr/lib64/libresolv-2.17.so
7f05acd6a000-7f05acd6c000 rw-p 00000000 00:00 0
7f05acd6c000-7f05acd71000 r-xp 00000000 08:08 1835062 /usr/lib64/libnss_dns-2.17.so
7f05acd71000-7f05acf71000 ---p 00005000 08:08 1835062 /usr/lib64/libnss_dns-2.17.so
7f05acf71000-7f05acf72000 r--p 00005000 08:08 1835062 /usr/lib64/libnss_dns-2.17.so
7f05acf72000-7f05acf73000 rw-p 00006000 08:08 1835062 /usr/lib64/libnss_dns-2.17.so
7f05acf73000-7f05acf7f000 r-xp 00000000 08:08 1835064 /usr/lib64/libnss_files-2.17.so
7f05acf7f000-7f05ad17e000 ---p 0000c000 08:08 1835064 /usr/lib64/libnss_files-2.17.so
7f05ad17e000-7f05ad17f000 r--p 0000b000 08:08 1835064 /usr/lib64/libnss_files-2.17.so
7f05ad17f000-7f05ad180000 rw-p 0000c000 08:08 1835064 /usr/lib64/libnss_files-2.17.so
7f05ad180000-7f05ad186000 rw-p 00000000 00:00 0
7f05ad186000-7f05ad348000 r-xp 00000000 08:08 1835046 /usr/lib64/libc-2.17.so
7f05ad348000-7f05ad548000 ---p 001c2000 08:08 1835046 /usr/lib64/libc-2.17.so
7f05ad548000-7f05ad54c000 r--p 001c2000 08:08 1835046 /usr/lib64/libc-2.17.so
7f05ad54c000-7f05ad54e000 rw-p 001c6000 08:08 1835046 /usr/lib64/libc-2.17.so
7f05ad54e000-7f05ad553000 rw-p 00000000 00:00 0
7f05ad553000-7f05ad568000 r-xp 00000000 08:08 1850415 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f05ad568000-7f05ad767000 ---p 00015000 08:08 1850415 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f05ad767000-7f05ad768000 r--p 00014000 08:08 1850415 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f05ad768000-7f05ad769000 rw-p 00015000 08:08 1850415 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f05ad769000-7f05ad86a000 r-xp 00000000 08:08 1835054 /usr/lib64/libm-2.17.so
7f05ad86a000-7f05ada69000 ---p 00101000 08:08 1835054 /usr/lib64/libm-2.17.so
7f05ada69000-7f05ada6a000 r--p 00100000 08:08 1835054 /usr/lib64/libm-2.17.so
7f05ada6a000-7f05ada6b000 rw-p 00101000 08:08 1835054 /usr/lib64/libm-2.17.so
7f05ada6b000-7f05adb54000 r-xp 00000000 08:08 1835336 /usr/lib64/libstdc++.so.6.0.19
7f05adb54000-7f05add53000 ---p 000e9000 08:08 1835336 /usr/lib64/libstdc++.so.6.0.19
7f05add53000-7f05add5b000 r--p 000e8000 08:08 1835336 /usr/lib64/libstdc++.so.6.0.19
7f05add5b000-7f05add5d000 rw-p 000f0000 08:08 1835336 /usr/lib64/libstdc++.so.6.0.19
7f05add5d000-7f05add72000 rw-p 00000000 00:00 0
7f05add72000-7f05add94000 r-xp 00000000 08:08 1850417 /usr/lib64/ld-2.17.so
7f05adf8a000-7f05adf93000 rw-p 00000000 00:00 0
7f05adf93000-7f05adf94000 r--p 00021000 08:08 1850417 /usr/lib64/ld-2.17.so
7f05adf94000-7f05adf95000 rw-p 00022000 08:08 1850417 /usr/lib64/ld-2.17.so
7f05adf95000-7f05adf96000 rw-p 00000000 00:00 0
7ffe298ca000-7ffe298eb000 rw-p 00000000 00:00 0 [stack]
7ffe29937000-7ffe29939000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Abort
I run valgrind --leak-check=yes to check..and this is the result: It is hard to understand :(
valgrind --leak-check=yes ./assignment1 c 3434
==15589== Memcheck, a memory error detector
==15589== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==15589== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==15589== Command: ./assignment1 c 3434
==15589==
==15589== Conditional jump or move depends on uninitialised value(s)
==15589== at 0x4019BD3: index (in /usr/lib64/ld-2.17.so)
==15589== by 0x4007510: expand_dynamic_string_token (in /usr/lib64/ld-2.17.so)
==15589== by 0x400799C: fillin_rpath (in /usr/lib64/ld-2.17.so)
==15589== by 0x40082D8: _dl_init_paths (in /usr/lib64/ld-2.17.so)
==15589== by 0x4003466: dl_main (in /usr/lib64/ld-2.17.so)
==15589== by 0x4017F2D: _dl_sysdep_start (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001BB0: _dl_start (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001127: ??? (in /usr/lib64/ld-2.17.so)
==15589== by 0x2: ???
==15589== by 0xFFF000442: ???
==15589== by 0xFFF000450: ???
==15589== by 0xFFF000452: ???
==15589==
==15589== Conditional jump or move depends on uninitialised value(s)
==15589== at 0x4019BE2: index (in /usr/lib64/ld-2.17.so)
==15589== by 0x4007510: expand_dynamic_string_token (in /usr/lib64/ld-2.17.so)
==15589== by 0x400799C: fillin_rpath (in /usr/lib64/ld-2.17.so)
==15589== by 0x40082D8: _dl_init_paths (in /usr/lib64/ld-2.17.so)
==15589== by 0x4003466: dl_main (in /usr/lib64/ld-2.17.so)
==15589== by 0x4017F2D: _dl_sysdep_start (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001BB0: _dl_start (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001127: ??? (in /usr/lib64/ld-2.17.so)
==15589== by 0x2: ???
==15589== by 0xFFF000442: ???
==15589== by 0xFFF000450: ???
==15589== by 0xFFF000452: ???
==15589==
==15589== Conditional jump or move depends on uninitialised value(s)
==15589== at 0x4019BF1: index (in /usr/lib64/ld-2.17.so)
==15589== by 0x4007510: expand_dynamic_string_token (in /usr/lib64/ld-2.17.so)
==15589== by 0x400799C: fillin_rpath (in /usr/lib64/ld-2.17.so)
==15589== by 0x40082D8: _dl_init_paths (in /usr/lib64/ld-2.17.so)
==15589== by 0x4003466: dl_main (in /usr/lib64/ld-2.17.so)
==15589== by 0x4017F2D: _dl_sysdep_start (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001BB0: _dl_start (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001127: ??? (in /usr/lib64/ld-2.17.so)
==15589== by 0x2: ???
==15589== by 0xFFF000442: ???
==15589== by 0xFFF000450: ???
==15589== by 0xFFF000452: ???
==15589==
==15589== Conditional jump or move depends on uninitialised value(s)
==15589== at 0x4019B90: index (in /usr/lib64/ld-2.17.so)
==15589== by 0x4007510: expand_dynamic_string_token (in /usr/lib64/ld-2.17.so)
==15589== by 0x40084DC: _dl_map_object (in /usr/lib64/ld-2.17.so)
==15589== by 0x400130D: map_doit (in /usr/lib64/ld-2.17.so)
==15589== by 0x400F703: _dl_catch_error (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001DE9: handle_ld_preload (in /usr/lib64/ld-2.17.so)
==15589== by 0x40042A1: dl_main (in /usr/lib64/ld-2.17.so)
==15589== by 0x4017F2D: _dl_sysdep_start (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001BB0: _dl_start (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001127: ??? (in /usr/lib64/ld-2.17.so)
==15589== by 0x2: ???
==15589== by 0xFFF000442: ???
==15589==
==15589== Conditional jump or move depends on uninitialised value(s)
==15589== at 0x4019B9F: index (in /usr/lib64/ld-2.17.so)
==15589== by 0x4007510: expand_dynamic_string_token (in /usr/lib64/ld-2.17.so)
==15589== by 0x40084DC: _dl_map_object (in /usr/lib64/ld-2.17.so)
==15589== by 0x400130D: map_doit (in /usr/lib64/ld-2.17.so)
==15589== by 0x400F703: _dl_catch_error (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001DE9: handle_ld_preload (in /usr/lib64/ld-2.17.so)
==15589== by 0x40042A1: dl_main (in /usr/lib64/ld-2.17.so)
==15589== by 0x4017F2D: _dl_sysdep_start (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001BB0: _dl_start (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001127: ??? (in /usr/lib64/ld-2.17.so)
==15589== by 0x2: ???
==15589== by 0xFFF000442: ???
==15589==
==15589== Conditional jump or move depends on uninitialised value(s)
==15589== at 0x4019BAE: index (in /usr/lib64/ld-2.17.so)
==15589== by 0x4007510: expand_dynamic_string_token (in /usr/lib64/ld-2.17.so)
==15589== by 0x40084DC: _dl_map_object (in /usr/lib64/ld-2.17.so)
==15589== by 0x400130D: map_doit (in /usr/lib64/ld-2.17.so)
==15589== by 0x400F703: _dl_catch_error (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001DE9: handle_ld_preload (in /usr/lib64/ld-2.17.so)
==15589== by 0x40042A1: dl_main (in /usr/lib64/ld-2.17.so)
==15589== by 0x4017F2D: _dl_sysdep_start (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001BB0: _dl_start (in /usr/lib64/ld-2.17.so)
==15589== by 0x4001127: ??? (in /usr/lib64/ld-2.17.so)
==15589== by 0x2: ???
==15589== by 0xFFF000442: ???
==15589==
==15589== Use of uninitialised value of size 8
==15589== at 0x4C2BA1C: strcpy (vg_replace_strmem.c:506)
==15589== by 0x40628D: hostname_Client() (client.cpp:221)
==15589== by 0x4057E2: setup_Client(int) (client.cpp:55)
==15589== by 0x4065F5: main (yichenwa_assignment1.cpp:77)
==15589==
==15589== Conditional jump or move depends on uninitialised value(s)
==15589== at 0x4C2BA47: is_overlap (vg_replace_strmem.c:128)
==15589== by 0x4C2BA47: strcpy (vg_replace_strmem.c:506)
==15589== by 0x40628D: hostname_Client() (client.cpp:221)
==15589== by 0x4057E2: setup_Client(int) (client.cpp:55)
==15589== by 0x4065F5: main (yichenwa_assignment1.cpp:77)
==15589==
==15589== Conditional jump or move depends on uninitialised value(s)
==15589== at 0x4C2BA53: is_overlap (vg_replace_strmem.c:137)
==15589== by 0x4C2BA53: strcpy (vg_replace_strmem.c:506)
==15589== by 0x40628D: hostname_Client() (client.cpp:221)
==15589== by 0x4057E2: setup_Client(int) (client.cpp:55)
==15589== by 0x4065F5: main (yichenwa_assignment1.cpp:77)
==15589==
==15589== Conditional jump or move depends on uninitialised value(s)
==15589== at 0x4C2BAB8: is_overlap (vg_replace_strmem.c:140)
==15589== by 0x4C2BAB8: strcpy (vg_replace_strmem.c:506)
==15589== by 0x40628D: hostname_Client() (client.cpp:221)
==15589== by 0x4057E2: setup_Client(int) (client.cpp:55)
==15589== by 0x4065F5: main (yichenwa_assignment1.cpp:77)
==15589==
==15589== Conditional jump or move depends on uninitialised value(s)
==15589== at 0x4C2BA62: strcpy (vg_replace_strmem.c:506)
==15589== by 0x40628D: hostname_Client() (client.cpp:221)
==15589== by 0x4057E2: setup_Client(int) (client.cpp:55)
==15589== by 0x4065F5: main (yichenwa_assignment1.cpp:77)
==15589==
My hostname_Client() code is:
char* hostname_Client(){
...
char *hostname;
strcpy(hostname,hostbuffer);
...
}
Upvotes: 0
Views: 253
Reputation: 182883
char* hostname_Client(){
char hostbuffer[1024]={0};
gethostname(hostbuffer,sizeof(hostbuffer));
char *hostname;
strcpy(hostname,hostbuffer);
return hostname;
}
You never initialize hostname
, so it has some random value and doesn't point to anything. Then you pass its random value to strcpy
and tell it to copy the string to some random location.
Since you're using C++, you may wish to consider using std::string
instead of char *
because it does most of the memory management for you.
Upvotes: 1