malloc realloc help really wierd error

Started by
18 comments, last by fishleg003 18 years, 5 months ago
Basically im just trying to store a 2d array that will store the mobs on a pacman level. so i can go mobs.positionofmob[0][0] >> x mobs.positionofmob[0][1] >> y mobs.positionofmob[0][2] >> z The problem is i first create the array no problems it dumps the values in but when i realloc to add another mob it screws up the first mobs data. I think i must be reallocing wrong sorry my understanding of this isnt very good yet :(. thx alot for any help.
#ifndef _enemy_c_
#define _enemy_c_

#include <stdio.h>
#include <cstdlib>

typedef struct Tag_Enemy
{
	int num_mobs;

	float **Ppos; //position in actual coords 2.433 5.3423 etc
	float **grid_pos; //position relative to the grid 2, 3 etc

}Enemy;

void Add_Mob(Enemy *e, float g_size, int gx, int gy);

Enemy E;

void main()
{

	Add_Mob(&E, 0.4, 2, 4);
        Add_Mob(&E, 0.4, 2, 4);
	Add_Mob(&E, 0.4, 4, 8);
	Add_Mob(&E, 0.4, 6, 10);
	Add_Mob(&E, 0.4, 8, 12);
	Add_Mob(&E, 0.4, 10, 14);
return;
}

void Add_Mob(Enemy *e, float g_size, int gx, int gy)
{

	if(e->num_mobs == 0)
	{
		e->Ppos = (float**)malloc(sizeof(float*));

		e->Ppos[0] = (float*)malloc(3 * sizeof(float));

		e->grid_pos = (float**)malloc(sizeof(float*));

		e->grid_pos[0] = (float*)malloc(3 * sizeof(float));

		e->grid_pos[0][0] = gx; 
		e->grid_pos[0][1] = gy; 

		e->Ppos[0][0] = (float)gx * (g_size * 2);
		e->Ppos[0][1] = (float)gy * (g_size * 2);
		e->Ppos[0][2] = 0;

		e->num_mobs++;
	}
	else
	{
		(float**)realloc(e->Ppos[0], e->num_mobs * sizeof(float*));

		(float**)realloc(e->grid_pos[0], e->num_mobs * sizeof(float*));

		e->Ppos[e->num_mobs] = (float*)malloc(sizeof(float) * 3);

		e->grid_pos[e->num_mobs] = (float*)malloc(sizeof(float) * 3);

		e->grid_pos[e->num_mobs][0] = gx; 
		e->grid_pos[e->num_mobs][1] = gy; 

		e->Ppos[e->num_mobs][0] = gx * (g_size * 2);
		e->Ppos[e->num_mobs][1] = gy * (g_size * 2);
		e->Ppos[e->num_mobs][2] = 0;

		e->num_mobs++;
	}
}

#endif
Advertisement
You need to assign your pointers when you call realloc just as you would when you call malloc. In your else clause you are not doing that.
mmmm.

I tried doing this i think this is what you mean
e->Ppos = (float**)realloc(e->Ppos[0], e->num_mobs * sizeof(float*));
e->grid_pos = (float**)realloc(e->grid_pos[0], e->num_mobs * sizeof(float*));

But it crashes right as i add an extra one.
Quote:Original post by fishleg003
Basically im just trying to store a 2d array that will store the mobs on a pacman level.

so i can go mobs.positionofmob[0][0] >> x
mobs.positionofmob[0][1] >> y
mobs.positionofmob[0][2] >> z


If you're working in C++ (as the angle brackets indicate), please use real tools for the job. You won't gain anything by old C techniques except extra effort.

Read this. Oh, and this, and this.

You need to capture the return of the Realloc as the pointer that you provide it might not be pointing to the same space of memory as before. First realloc tries to increase the current size of the memory that you are pointing to if it cannot do that then it just assigns you a new piece of memory somewhere else.
Quote:Original post by Vanke
You need to capture the return of the Realloc as the pointer that you provide it might not be pointing to the same space of memory as before. First realloc tries to increase the current size of the memory that you are pointing to if it cannot do that then it just assigns you a new piece of memory somewhere else.



Im not sure what you mean isnt that what this does ?
e->Ppos = (float**)realloc(e->Ppos, e->num_mobs * sizeof(float*));
e->grid_pos = (float**)realloc(e->grid_pos, e->num_mobs * sizeof(float*));

I try it this way and it works sort of, the 1st entry stays and the one after that gets stored but as soon as i realloc the previous entry vanishes and becomes corrupt memory.

Its definately the realloc ive searched my brains out cant find a decent example of mallocing / reallocing a 2d array.
I'm sorry, but I would just ditch the 2d array and use a simple array of objects of "Points" or any class that you can make up that represents coordinates.

Robin
Please help i even tried a 1d array it reallocs once but the 2nd time the program crashes heres the new code,

#ifndef _enemy_c_#define _enemy_c_#include <stdio.h>#include <cstdlib>typedef struct Tag_Enemy{	int num_mobs;	float *Ppos; //position in actual coords 2.433 5.3423 etc	float *grid_pos; //position relative to the grid 2, 3 etc}Enemy;void Add_Mob(Enemy *e, float g_size, int gx, int gy);Enemy E;void main(){	Add_Mob(&E, 0.4, 2, 4);		Add_Mob(&E, 0.4, 2, 4);		Add_Mob(&E, 0.4, 4, 8);		Add_Mob(&E, 0.4, 6, 10);		Add_Mob(&E, 0.4, 8, 12);		Add_Mob(&E, 0.4, 10, 14);return;}void Add_Mob(Enemy *e, float g_size, int gx, int gy){		if(e->num_mobs == 0)	{		e->Ppos = (float*)malloc(sizeof(float) * 3);		e->grid_pos = (float*)malloc(sizeof(float) * 3);		e->grid_pos[0] = gx; 		e->grid_pos[1] = gy; 		e->grid_pos[2] = 0; 		e->Ppos[0] = (float)gx * (g_size * 2);		e->Ppos[1] = (float)gy * (g_size * 2);		e->Ppos[2] = 0;		e->num_mobs++;	}	else	{		realloc(e->Ppos, (e->num_mobs * sizeof(float)) * 3);		realloc(e->grid_pos, (e->num_mobs * sizeof(float)) * 3);		e->Ppos[(e->num_mobs*3)] = gx;		e->Ppos[(e->num_mobs*3)+1] = gy;		e->Ppos[(e->num_mobs*3)+2] = 0;		e->grid_pos[(e->num_mobs*3)] =  (float)gx * (g_size * 2);;		e->grid_pos[(e->num_mobs*3)+1] = (float)gy * (g_size * 2);;		e->grid_pos[(e->num_mobs*3)+2] = 0;				e->num_mobs++;	}}#endif


I also tried it this way,
e->Ppos = (float*)realloc(e->Ppos, (e->num_mobs * sizeof(float)) * 3);
e->grid_pos = (float*)realloc(e->grid_pos, (e->num_mobs * sizeof(float)) * 3);

Driving me bonkers....

Everything gives same result reallocs once fine stores values the 2nd time it reallocs to store the 3rd values it crashes.
Quote:Original post by fishleg003
Please help i even tried a 1d array it reallocs once but the 2nd time the program crashes heres the new code,

*** Source Snippet Removed ***


You absolutly need to capture the return address of your Realloc

doing this
(cast)Realloc(Pointer,Size)

will work sometimes but you CANNOT use realloc like this. What I do is this

TempVar = (cast Realloc(pointer,size)

if TempVar != pointer then
pointer = tempvar.


There using realloc like that is safe.

also this
e->Ppos = (float**)realloc(e->Ppos[0], e->num_mobs * sizeof(float*));
e->grid_pos = (float**)realloc(e->grid_pos[0], e->num_mobs * sizeof(float*));

is wrong. why are you supplying e->Ppos[0] and not simply e->Ppos????? is there a reason that you are only supplying the first item in the array and then assigning the resized first elemtn to the entir array?

this might work better.

float * temp = (float*)realloc(e->Ppos[0], e->num_mobs * sizeof(float*));
if temp != e->Ppos[0] then
e->Ppos[0] = temp
float* temp2 = (float*)realloc(e->grid_pos[0], e->num_mobs * sizeof(float*));
if temp2 != e->grid_pos[0] then
e->grid_pos[0] = temp

This topic is closed to new replies.

Advertisement