New to Pointer, there are some wrong.
I am new to pointer.
I want to convert decimal to binary with pointer array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
int main() {
const int max_number = 0;
int* bin_number = new int[max_number];
int dec_number = 0;
int count = 0;
cout<<endl
<<"Enter a positive decimal integer: ";
cin>>dec_number;
do {
*(bin_number+count)=dec_number%2;
count++;
dec_number = dec_number /2;
} while (dec_number!=0);
cout<<endl
<<"Converted into binary: ";
int for_count = count - 1;
for (int i=0; i< count; i++) {
cout<<*(bin_number+for_count);
for_count--;
}
cout<<endl;
delete [] bin_number;
return 0;
}
|
After compiled, When I run, there some wrong.
*** glibc detected *** ./test18.exe: free(): invalid next size (fast): 0x09ff4008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x6ebc2)[0xda6bc2]
/lib/i386-linux-gnu/libc.so.6(+0x6f862)[0xda7862]
/lib/i386-linux-gnu/libc.so.6(cfree+0x6d)[0xdaa94d]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0x78180f]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdaPv+0x1b)[0x78186b]
./test18.exe[0x8048848]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xd51113]
./test18.exe[0x8048671]
======= Memory map: ========
001f9000-001fa000 r-xp 00000000 00:00 0 [vdso]
00235000-00253000 r-xp 00000000 08:0b 263064 /lib/i386-linux-gnu/ld-2.13.so
00253000-00254000 r--p 0001d000 08:0b 263064 /lib/i386-linux-gnu/ld-2.13.so
00254000-00255000 rw-p 0001e000 08:0b 263064 /lib/i386-linux-gnu/ld-2.13.so
0036d000-00389000 r-xp 00000000 08:0b 263098 /lib/i386-linux-gnu/libgcc_s.so.1
00389000-0038a000 r--p 0001b000 08:0b 263098 /lib/i386-linux-gnu/libgcc_s.so.1
0038a000-0038b000 rw-p 0001c000 08:0b 263098 /lib/i386-linux-gnu/libgcc_s.so.1
006d4000-007b2000 r-xp 00000000 08:0b 9049477 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
007b2000-007b3000 ---p 000de000 08:0b 9049477 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
007b3000-007b7000 r--p 000de000 08:0b 9049477 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
007b7000-007b8000 rw-p 000e2000 08:0b 9049477 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
007b8000-007bf000 rw-p 00000000 00:00 0
0080c000-00834000 r-xp 00000000 08:0b 263107 /lib/i386-linux-gnu/libm-2.13.so
00834000-00835000 r--p 00028000 08:0b 263107 /lib/i386-linux-gnu/libm-2.13.so
00835000-00836000 rw-p 00029000 08:0b 263107 /lib/i386-linux-gnu/libm-2.13.so
00d38000-00eae000 r-xp 00000000 08:0b 263077 /lib/i386-linux-gnu/libc-2.13.so
00eae000-00eb0000 r--p 00176000 08:0b 263077 /lib/i386-linux-gnu/libc-2.13.so
00eb0000-00eb1000 rw-p 00178000 08:0b 263077 /lib/i386-linux-gnu/libc-2.13.so
00eb1000-00eb4000 rw-p 00000000 00:00 0
08048000-08049000 r-xp 00000000 08:0b 3148774 /home/cloverstd/文档/cpp/test18.exe
08049000-0804a000 r--p 00000000 08:0b 3148774 /home/cloverstd/文档/cpp/test18.exe
0804a000-0804b000 rw-p 00001000 08:0b 3148774 /home/cloverstd/文档/cpp/test18.exe
09ff4000-0a015000 rw-p 00000000 00:00 0 [heap]
b7700000-b7721000 rw-p 00000000 00:00 0
b7721000-b7800000 ---p 00000000 00:00 0
b784b000-b784e000 rw-p 00000000 00:00 0
b785b000-b785f000 rw-p 00000000 00:00 0
bfb00000-bfb21000 rw-p 00000000 00:00 0 [stack]
已放弃
|
Why this happens?
How to solve this problem?
Update:
When I delete the code "delete [] bin_number;"
It can run normally.
Last edited on
bin_number is pointing to an array in your program so you must access it's components through the subscript operator [ ].
1 2 3 4 5 6
|
bin_number[0] = dec_number%2;
"through"
bin_number[max_number] = dec_number%2;
|
You can put any integer expression in the [ ].
Only use the *(identifier) when you are accessing a single pointers value. Well, at least for the most
part.
Last edited on
Topic archived. No new replies allowed.