"Jason Pawloski" <jpawlo...@gmail.com> wrote in message
news:1182379285.195245.4720@p77g2000hsh.googlegroups.com...
On Jun 20, 3:19 pm, Ian Collins <ian-n...@hotmail.com> wrote:
Jason Pawloski wrote:
Relevant:
I have a local integer pointer:
int *nr1 = NULL;
within this function, I call another function
int *function2([parameters])
This function mallocs an int *ptr and returns it. So I use the
function like:
nr1 = function2([parameter]);
Now when I try to free it later in my first function (the one that
calls function2), I get an MSVC++ error. The exact wording is:
Debug Error!
Program: <path>
DAMAGE: after Normal block (#46) at 0x00431D80
Any suggestions on what might cause this? thanks!
Can you post function2?
--
Ian Collins.- Hide quoted text -
- Show quoted text -
Sure. Again, not a programmer by trade, etc.:
NB size is the absolute length of *list (ie number of elements), while
position is the INDEX of what to remove from the list. Confusingly
stupid, I know, but working it this way makes other things in the
program not relevant to this simple.
int *dropreel(int *list,int size,int position)
{
int *nl = NULL;
/*Put the index in range */
while(position > size-1) position = position - size;
while(position < 0) position = position + size;
nl = malloc(sizeof(int)*(size-1));
if(nl == NULL)
printf("WARNING!! NO MEMORY!\n");
memset(nl,0,sizeof(int)*(size-1));
if(position != size-1)
nl[0] = list[size-1];
else
nl[0] = list[size-2];
memcpy(&nl[1],list,sizeof(int)*(position));
memcpy(&nl[position+1],&list[(position+1)],sizeof(int)*(size-
position-1));
return nl;
}
It's a little confusing code, but it seems that your list[0] stores the
size
of the list, with list[1] starting the actual data. Does the size parm
being passed in include one for list[0] ? That is, are you simply
passing
size as list[0] into the function?
And if so, why are you decrementing size in the malloc? For example, if
you
had a list of three values:
list[0] = 3;
list[1] = 1;
list[2] = 2;
list[3] = 3;
you would be passing in size as 3, although it actually contains 4 ints.
Now, say you are wanting to remove element number 2. So you would pass
list, list[0], 2
correct?
Now, when you malloc, sizeof(int) * (size - 1)
size is 3. 3-1 = 2. You are only allocating 2 ints, when you acutally
need
3.
This is obvoiusly C code and not C++ (the giveaway is malloc's return
value
not being cast which is required in C++) otherewise I'd say just use
std::vector and be done with it.
Your while code is somewhat flakey (while (position > size - 1, etc...)
and
if it was me, if position was > size I'd print an error and return null.
It sounds most likely like you are not allocating enough memory and
overflowing the array with your memcpys.
it out, it does crash, so for whatever reason the free is the culprit.
list.
anything. But I'm required to do 100,000,000 iterations of this