new / delete operators in for-loop

Hi,

I have a question concerning the new/delete-operators: I have written a program:

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
int main()
{
  double *gleichm_stuetzstellenx;
  double *tscheby_stuetzstellenx;
  double *gleichm_stuetzstelleny;
  double *tscheby_stuetzstelleny;
  double *gleichm_koeff;
  double *tscheby_koeff;

  
  int i;
  int j;
  
  printf("Die Werte des Interpolationspolynoms an der Stelle x = pi/4 sind:\n\n");
  printf("n\tGleichmäßige Verteilung\tTschebyscheff-Verteilung\n");
  for (i = 0; i <= sizeof(narray) - 1; i++)
  {
    gleichm_stuetzstellenx = new double[narray[i]];
    tscheby_stuetzstellenx = new double[narray[i]];
    gleichm_stuetzstelleny = new double[narray[i]];
    tscheby_stuetzstelleny = new double[narray[i]];
    gleichm_koeff = new double[narray[i]];
    tscheby_koeff = new double[narray[i]];

    calcstuetzstellen(narray[i], gleichm_stuetzstellenx, gleichm_stuetzstelleny, tscheby_stuetzstellenx, tscheby_stuetzstelleny );
    calcdivdifferenz(narray[i], gleichm_stuetzstellenx, gleichm_stuetzstelleny, tscheby_stuetzstellenx, tscheby_stuetzstelleny, gleichm_koeff, tscheby_koeff);
    printf("%i\t%.8E\t%.8E\n", narray[i], calcpolynom_gleichm(PI / 4, narray[i], gleichm_stuetzstellenx, gleichm_koeff), calcpolynom_tscheby(PI / 4, narray[i], tscheby_stuetzstellenx, tscheby_koeff));

    delete [] gleichm_stuetzstellenx;
    delete [] tscheby_stuetzstelleny;
    delete [] gleichm_stuetzstellenx;
    delete [] tscheby_stuetzstelleny;
    delete [] gleichm_koeff;
    delete [] tscheby_koeff;
}
return 0;
}
(...)


It compiles fine, however it only does the first loop of the for-loop and then crashes by saying

"*** glibc detected *** ./aufgabe3_1: free(): invalid next size (fast): 0x081d6008 ***
"

I suppose, something is wrong with the new/delete-memory-allocation. But I have no idea, what's wrong, since I'm fairly new in programming C/C++.

Thanks in advance for your help.

Jannis
gleichm_stuetzstellenx
Oh. My. God.
I don't mean to insult the German language, but this is an English forum, and to me, this is just a meaningless random sequence of characters. Pretty much the worst possible identifier.
If you're going to post here, at least use identifiers we can understand.

In any case, it's hard to tell what's wrong from just this little piece of code. My guess is that either a) one of the elements in narray is zero, which I think is an invalid array size, b) you're deleting an array more that once, or c) you're over flowing one of the arrays.
I may be wrong, but I think at least part of your problem is that during your first iteration of the loop, you're trying to allocate arrays of type double that ARE ZERO IN SIZE! Then near the bottom of the loop you are trying to release them. I don't speak German either, but whatever you're trying to do in the middle of the loop with ZERO memory allocate I'm sure doesn't help matters much.
Last edited on
It does not take knowledge of German to see that

1
2
3
4
5
6
7
8
  for (i = 0; i <= sizeof(narray) - 1; i++)
  {
    gleichm_stuetzstellenx = new double[narray[i]];
    tscheby_stuetzstellenx = new double[narray[i]];
    gleichm_stuetzstelleny = new double[narray[i]];
    tscheby_stuetzstelleny = new double[narray[i]];
    gleichm_koeff = new double[narray[i]];
    tscheby_koeff = new double[narray[i]];


is probably allocating arrays of unknown size, since nowhere is narray even declared
(I assume it is a global declared above main).
Thank you for your suggestions and sorry for the German named variables. I will do better in future :-). I actually now solved the problem in a different way, namely that I defined a maximal size und all arrays now have this size.
Thank you again.
Is narray been declared a pointer?
you can debug your code and can concentrate on that area only to find out whats the problem. you will soon find it out.
Topic archived. No new replies allowed.