Sign in to follow this  
fishleg003

malloc realloc help really wierd error

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
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
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
cstdlib is not a C header. You may want to switch it out for the correct header for C (stdlib.h), or alternatively use C++ memory facilities.

Share this post


Link to post
Share on other sites
Maybe this will help you it's the doc for Realloc from K&R

void * realloc(void *p, size_t size)
realloc changes the size of the object pointed to by p to size. The contents will be unchanged up the minimum of the old and new sizes. If the new size is larger, the new space is uninitialized. realloc returns a pointer to the new space, or Null if the request cannot be satisfied, in which case * p is unchanged.

Share this post


Link to post
Share on other sites
thx alot for your help so far everyone ill try to give u all nice rating.
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+=2;

}
else
{
float *temp, *temp2;

temp = (float*)realloc(e->Ppos, (e->num_mobs * 3) * sizeof(float));
if (temp != e->Ppos)
e->Ppos = temp ;

temp2 = (float*)realloc(e->grid_pos, (e->num_mobs * 3) * sizeof(float));
if (temp2 != e->grid_pos)
e->grid_pos = temp;



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++;
}
}


I did what everyone said but it still crashes the 2nd time it reallocs. What else can i do ?

I even changed the header at the top thx for telling me that i didnt know it was for c++ only.

Share this post


Link to post
Share on other sites
If you're working in C++, you really should be using the new operator instead of malloc/realloc... If you're using C++, you should not be in a C frame of mind. Never use C functions unless you absolutely need to (for example, when interacting with legacy code).



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

//Use
e->Ppos = new float *[m];
e->Ppos[0] = new float[3];





To reallocate memory with the new operator in C++, it works like this:

// Create a temporary array as a backup
float **temp = new float *[m];
for(int i=0; i<n; ++i)
{
temp[i] = new float[n];
}

for(int i=0; i<m; ++i)
{
for(int j=0; j<n; ++j)
{
temp[i][j] = e->Ppos[i][j];
}
}

// Delete the old array
delete[] e->Ppos;

// Rellocate array and copy everything back into it
e->Ppos = new float *[newM];
for(int i=0; i<newM; ++i)
{
e->Ppos[i] = new float[newN];
}

for(int i=0; i<m; ++i)
{
for(int j=0; j<n; ++i)
{
e->Ppos[i][j] = temp[i][j];
}
}

// Delete the temporary array
delete[] temp;





I know it looks like a pain, but its the only way to be sure you're not causing any memory leaks. If you're working in C, you follow the same general idea. malloc a temporary array, copy everything over... free your original malloced array, reallocate the original, copy everything back, free the temporary.

[Edited by - MetalRob on October 22, 2005 3:05:31 PM]

Share this post


Link to post
Share on other sites
Yes I agree are you writing this only in C or are you using C++, if it's only in C then I suggest you keep on trying to get your realloc and malloc's working otherwise start using the new operator it's much easier in my opinion.

Share this post


Link to post
Share on other sites
Quote:
Original post by MetalRob
If you're working in C++, you really should be using the new operator instead of malloc/realloc... If you're using C++, you should not be in a C frame of mind. Never use C functions unless you absolutely need to (for example, when interacting with legacy code).


*** Source Snippet Removed ***

Someone correct me if I'm wrong, I'm not near a compiler right now ;)


I might be wrong on this, but wouldnt you want to use the delete operator once you are done with the new'd floats?

Share this post


Link to post
Share on other sites
Quote:
Original post by supercoder74
Quote:
Original post by MetalRob
If you're working in C++, you really should be using the new operator instead of malloc/realloc... If you're using C++, you should not be in a C frame of mind. Never use C functions unless you absolutely need to (for example, when interacting with legacy code).


*** Source Snippet Removed ***

Someone correct me if I'm wrong, I'm not near a compiler right now ;)


I might be wrong on this, but wouldnt you want to use the delete operator once you are done with the new'd floats?


Technically, no... You'd want to use the delete[] operator, since you're working with arrays. I know it may sound like I'm nitpicking your response, but the delete operator and the delete[] operator are different.

Which brings out a contradiction in my original reply, I should have said "use the new[] operator" instead of the new operator.

Share this post


Link to post
Share on other sites
Yeah im writing this in c for a opengl pacman game. Just loads locations of everything from a bitmap file you see so i need it to add the mobs 1 by 1 as i go through the image data.

I finally solved it, could of been what you said before but i was also over writing the array when it came to realloc it sort of had a heart attack lol.

This is my code at the end of this massive head ache.

The creating a new piece of memory then dumping the old into the new, would that be better than what im doing now ?

thx again for your help everyone doubt i would of figured it out without your support.

any more tips appreciated. (and if anyone knows a good opengl tutorial site having problems with orbiting camera's)


#ifndef _enemy_c_
#define _enemy_c_

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)
{

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;
e->Ppos[0][1] = (float)gy * g_size;
e->Ppos[0][2] = 0;

e->num_mobs+=2;
}
else
{
e->Ppos = (float**)realloc(e->Ppos, e->num_mobs * sizeof(float*));

e->grid_pos = (float**)realloc(e->grid_pos, e->num_mobs * sizeof(float*));

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

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

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

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

e->num_mobs++;
}
}

#endif




Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this