• Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

We're also offering banner ads on our site from just \$5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.

### #Actualrhuala

Posted 13 February 2013 - 01:19 AM

rhuala, on 12 Feb 2013 - 18:49, said:



int i, a[10];
for(i=0;i&lt;15;i++)
a[i]=i;

That's not a memory leak. That's a buffer overrun. You can avoid it by not using magic numbers:


const size_t ARRAY_LENGTH = 10; //or whatever
int ary[ARRAY_LENGTH];
for(int i=0; i < ARRAY_LENGTH; ++i) {
ary[i] = i;
}

QFT - the OP's examples are all about memory corruption, not memory leaks at all

Another technique that you can use for arrays (but not arrays that have been cast/decayed into pointers) is:


template <typename T, int N> uint ArraySize(T(&)[N]) { return N; }
...
for(int i=0; i < ArraySize(ary); ++i) {
ary[i] = i;
}

okay my bad, I apologize... whatever it's called it leads to crashes or flaky behavior, I guess my technique sucks... although I never do allocate an array with a number, it's always a #define or const

### #1rhuala

Posted 13 February 2013 - 01:16 AM

rhuala, on 12 Feb 2013 - 18:49, said:

int i, a[10];
for(i=0;i&lt;15;i++)
a[i]=i;

That's not a memory leak. That's a buffer overrun. You can avoid it by not using magic numbers:
const size_t ARRAY_LENGTH = 10; //or whatever
int ary[ARRAY_LENGTH];
for(int i=0; i < ARRAY_LENGTH; ++i) {
ary[i] = i;
}

QFT - the OP's examples are all about memory corruption, not memory leaks at all

Another technique that you can use for arrays (but not arrays that have been cast/decayed into pointers) is:
template <typename T, int N> uint ArraySize(T(&)[N]) { return N; }
...
for(int i=0; i < ArraySize(ary); ++i) {
ary[i] = i;
}

okay my bad, I apologize... whatever it's called it leads to crashes, I guess my technique sucks... although I never do allocate an array with a number, it's always a #define or const

PARTNERS