Pointers the final frontier...WTH

Hi you guys,


it seems that my C++ skills are super rusted these days... i have the simplyfied problem code stated underneath:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int main()
{
	int *bannerPixelBuffer = new int[1024];

	for(int y = 0;y < 1024; y++ )
	{
		*(bannerPixelBuffer++) = 1024;

                //bannerPixelBuffer[y] = 1024; //This works...but is first of al slow as hell, second of al...people did this before me...what am i missing here:s
	}


	delete bannerPixelBuffer; ///EEEEH error...
	// neither does this help delete []bannerPixelBuffer;
	
	return 0;
}


Wel my code works great...(i think) until it hits the 'delete'.
I get "debug assertion failed" from visual studio...yep you guessed it...an invalid pointer...now how do i delete this thing from my memory!!:-D


It's driving me insane!
Kind regards,
Xabre
Hint: what is the value of bannerPixelBuffer by the time you get to line 15, given that you replaced line 11 with line 9?

Also, compiling the following program with gcc -O3 -S, the resultant assembler code for fn1() and fn2()
is absolutely identical so there cannot be a speed difference. Perhaps you are compiling without
optimizations?

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
#include <stdio.h>

int fn1()
{
        int* p = new int[1024];
        int* q = p;

        for( int y = 0; y < 1024; ++y )
                *(p++) = 1024;

        printf( "%d %d\n", q[0], q[1] );
        delete [] q;
}

int fn2()
{
        int* p = new int[1024];

        for( int y = 0; y < 1024; ++y )
                p[ y ] = 1024;

        printf( "%d %d\n", p[0], p[1] );
        delete [] p;
}

int main()
{
        fn1();
        fn2();
}

WOOW you guys! So fast!:p



Anyhow...i found my stupid mistake:-(
The solution was actually very easy:p

You see the pointer index should be 0 when deleting (or so goes my theory)...that's why i made a copy of my allocated memory (starting at index 0) and in the end i deleted my copy, wich is the same as my bannerpixelbuffer memory...

1
2
3
4
5
6
7
8
9
10
11
int *bannerPixelBuffer = new int[1024];
	int *temp = bannerPixelBuffer;

	for(int y = 0;y < 1024; y++ )
	{
		*(bannerPixelBuffer++) = y;
	}

	delete temp;

	return 0;


I can't thank you guys enough for the quick respons!:-D
Could you belief that it took me 8 hours to realize my mistake:s


Kind regards!!
It's delete [] temp;.
But why dynamic allocation?
closed account (Lv0f92yv)
With that code posted above, would the delete temp; line really de-allocate all the memory allocated to bannerPixelBuffer? I would think it would only delete the space contained by one int variable, since temp == temp[0].

Someone correct me if I'm wrong, I can't run valgrind on this to support my conclusion.

I think it is usually best to use:

delete[] variable;, where variable is the array dynamically allocated.
Last edited on
@Desh


temp is the pointer itself
*temp is the value of the pointer
&temp is the address of the pointer

(temp++) is the next value, sizeof(int) shifted


delete [] would call al the destructors of the types in that pointer
where
delete just kill the pointer without calling the destructors


i admit that that is unnecessary for ints.



Kind regards
Allocated with new unallocated with delete
Allocated with new [] unallocated with delete []

Please read these (11 to 13)
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.11
closed account (Lv0f92yv)
@ne555, yes, that is what I was getting at.
woops:p never old enough to learn:p thx!
Topic archived. No new replies allowed.