Code doesent work

hi again,

since i got such a nice and fast answer yesterday i want to ask you again about a new program i wrote. its task is simply to put out all prime numbers {2, ..., n -1} and i can compile it without errors. also the computation works for almost all n's but for some (a.e. 100) it gives me out the prime numbers first and then puts out some errors.

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
32
33
34
35
36
37
38
39

#include <iostream>

int main()
{
  // input
  std::cout << "Number till you want to compute =? ";
  int n;
  std::cin >> n;
  std::cout << "Components k =? ";

  // computation 
  bool* prime = new bool[n];
  int quantity_prime_numbers = 0;
  int* prime_numbers = new int[n];
  for (int i = 0; i < n; ++i) {
    prime[i] = true;
  }
  for (int i = 2; i <= n; ++i) {
    if (prime[i] == true) {
      prime_numbers[quantity_prime_numbers] = i;
      ++quantity_prime_numbers;
    }
    for (int j = 2 * i; j <= n; j += i) {
      prime[j] = false;
    }
  }

  // output
  for (int i = 0; i < quantity_prime_numbers; ++i) {
    std::cout << prime_numbers[i] << " "; 
  }
  std::cout << std::endl;

  delete[] prime;
  delete[] prime_numbers;
  return 0;
}


*** glibc detected *** ./k_composite: double free or corruption (!prev): 0x09e44008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x73e42)[0x8bae42]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0x30dadf]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdaPv+0x1b)[0x30db2b]
./k_composite[0x804884e]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x8604d3]
./k_composite[0x80488c1]
======= Memory map: ========
00110000-0012c000 r-xp 00000000 08:01 177248     /lib/i386-linux-gnu/libgcc_s.so.1
0012c000-0012d000 r--p 0001b000 08:01 177248     /lib/i386-linux-gnu/libgcc_s.so.1
0012d000-0012e000 rw-p 0001c000 08:01 177248     /lib/i386-linux-gnu/libgcc_s.so.1
002c5000-003a1000 r-xp 00000000 08:01 80517      /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
003a1000-003a5000 r--p 000dc000 08:01 80517      /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
003a5000-003a6000 rw-p 000e0000 08:01 80517      /usr/lib/i386-linux-gnu/libstdc++.so.6.0.17
003a6000-003ad000 rw-p 00000000 00:00 0 
0054c000-0056c000 r-xp 00000000 08:01 130781     /lib/i386-linux-gnu/ld-2.15.so
0056c000-0056d000 r--p 0001f000 08:01 130781     /lib/i386-linux-gnu/ld-2.15.so
0056d000-0056e000 rw-p 00020000 08:01 130781     /lib/i386-linux-gnu/ld-2.15.so
005f2000-0061c000 r-xp 00000000 08:01 130833     /lib/i386-linux-gnu/libm-2.15.so
0061c000-0061d000 r--p 00029000 08:01 130833     /lib/i386-linux-gnu/libm-2.15.so
0061d000-0061e000 rw-p 0002a000 08:01 130833     /lib/i386-linux-gnu/libm-2.15.so
00847000-009e6000 r-xp 00000000 08:01 130801     /lib/i386-linux-gnu/libc-2.15.so
009e6000-009e8000 r--p 0019f000 08:01 130801     /lib/i386-linux-gnu/libc-2.15.so
009e8000-009e9000 rw-p 001a1000 08:01 130801     /lib/i386-linux-gnu/libc-2.15.so
009e9000-009ec000 rw-p 00000000 00:00 0 
00db1000-00db2000 r-xp 00000000 00:00 0          [vdso]
08048000-08049000 r-xp 00000000 08:01 202506     /home/ifmp12/myProgs/k_composite
08049000-0804a000 r--p 00000000 08:01 202506     /home/ifmp12/myProgs/k_composite
0804a000-0804b000 rw-p 00001000 08:01 202506     /home/ifmp12/myProgs/k_composite
09e44000-09e65000 rw-p 00000000 00:00 0          [heap]
b7772000-b7775000 rw-p 00000000 00:00 0 
b7783000-b7788000 rw-p 00000000 00:00 0 
bf852000-bf873000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)


could anybody please help me and tell me what my mistake is? i am just trying to learn how to use arrays...

thank you,

s
@sinnex

You're going out of range with the loops using the <=. Remove them from lines 19 and 24, and things should work a LOT better
You are stepping out of bounds of the array.
The index should go from 0 to n-1
Seems like you have a memory leak in your program, i took your code and ran it on my schools windowsXP computer with codeblocks and everything works fine. What compiler do you use?
@ ne555, whitenite1

ah, of course!! thanks to both for your fast replies!

@ need4sleep

actually what they said helped. i just used one undefined space of my array... after deleting the "<=" it worked... i used the g++ compiler on linux...
Last edited on
Topic archived. No new replies allowed.