**I have a crash happening and I want step-by-step explanation as to why that is. e.g:
**a) the list grows by ...
...
b) the list exceeds at...
...
c) crashes because...
...
## Deliverables
1) Step by step explanation of the why the crash is happening. e.g:
a) the list grows by ...
...
b) the list exceeds at...
...
c) crashes because...
...
2) Provide the right fix. Modify the ArrayInsert function with the complete fix.
3) Will only accept the answer if it fixes the crash in my product when I copy over the modified ArrayInsert function.
**Here is the code that is inserting elements into an array.**
void*MyMem::ArrayInsert (void *ptr, void *element, USHORT elementSize, USHORT insertPoint, USHORT elementCount, USHORT allocCount)
{
unsigned int moveSize;
unsigned int allocSize;
ULONG elementsAlloced;
char *src;
char *dst;
if (ptr == NULL)
{
allocSize = (unsigned int)elementSize * (unsigned int) allocCount;
ptr = Alloc(allocSize);
if (ptr == NULL) {
return (NULL);
}
elementsAlloced = allocCount;
} else {
USHORT size = (USHORT) Util::MemGetSize (ptr); //returns unsigned int size memory
elementsAlloced = (USHORT) (size / elementSize);
} // if
if (elementCount >= elementsAlloced) {
elementsAlloced = elementsAlloced + allocCount;
allocSize = (unsigned int)elementSize * (unsigned int) elementsAlloced;
ptr = ReAlloc(ptr, allocSize);
if (ptr == NULL) {
return (NULL);
} // if
} // if
src = (char *)ptr + ((SLONG)insertPoint * (SLONG)elementSize);
dst = src + elementSize;
moveSize = (unsigned int)elementSize * (elementCount-insertPoint);
if (moveSize > 0) {
memmove(dst, src, moveSize);
} // if
dst = src;
memcpy(dst, element, elementSize);
return (ptr);
} // ArrayInsert
**Here is the how this function is called:**
ArrayInsert(void * ptr=0x00000000, void * element=0x0013bb10, unsigned short elementSize=16, unsigned short insertPoint=0, unsigned short elementCount=0, unsigned short allocCount=20)
**However, when accessing this array, it crashes at index: 2041. Need explanation about the crash and fix for it.
That's it.**
## Platform
WIN XP.
microsoft visual studio 2005.
C++.