Jump to content
  • Advertisement
Sign in to follow this  
fishleg003

malloc realloc help really wierd error

This topic is 4628 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!