Sign in to follow this  

help needed with struct memory

This topic is 3870 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

hi there i am using C and trying to set my memory so it points one after another so that my data is not stored all over the place, when i go into my patch function my data is lost can anyone help by the way MemoryAllocatePtr does the same job as malloc

struct sDriverData
{
	float skill;
	float temper;

} sDriverData;

struct sAIData
{
	int nbAIDrivers;
	sDriverData *pAIDriverData;

} sAIData;

struct sAIDataFile
{
	int magic; 
	int version;
	sAIData *pAIData;

} sAIDataFile;

void AIBLoader_Init()
{
	int i;
	/* allocate memory for our POINTERS */
	pAIDataFile = (sAIDataFile *)MemoryAllocatePtr(sizeof(sAIDataFile),MF_NONE);
	pAIDataFile->pAIData = (sAIData *)MemoryAllocatePtr(sizeof(sAIData),MF_NONE);
	pAIDataFile->pAIData->pAIDriverData = (sDriverData *)MemoryAllocatePtr(sizeof(sDriverData)*nbDrivers,MF_NONE);
	/* add some data */
	pAIDataFile->version = 1;
	pAIDataFile->pAIData->nbAIDrivers = 10;
	pAIDataFile->pAIData->pAIDriverData->skill = 0.2f;
	pAIDataFile->pAIData->pAIDriverData->temper = 0.5f;
	/* CopyAndCollate */
	pAIDataFile = AIBLoaderCopyAndCollate(pAIDataFile);
	/* Patch */
	AIBLoaderPatch(pAIDataFile);
}

sAIDataFile *AIBLoaderCopyAndCollate(sAIDataFile *pAIDataFile)
{
	int size,i;
	sAIDataFile *pNewAIDataFile;
	/* First calc how much memory we need */
	size = sizeof(sAIDataFile);
	size += sizeof(sAIData);
	size += (sizeof(sDriverData) * pAIDataFile->pAIData->nbAIDrivers);
	/* Allocate memory */
	pNewAIDataFile = (sAIDataFile *)MemoryAllocatePtr(size, MF_LOCK);
	/* Set pointers */
	pNewAIDataFile->pAIData = pNewAIDataFile + sizeof(sAIDataFile);
	pNewAIDataFile->pAIData->pAIDriverData = pNewAIDataFile->pAIData + sizeof(sAIData);
	/* Copy data */
	pNewAIDataFile->pAIData->nbAIDrivers = pAIDataFile->pAIData->nbAIDrivers;
	for (i=0; i<pNewAIDataFile->pAIData->nbAIDrivers; i++)
	{
		pNewAIDataFile->pAIData->pAIDriverData[i].skill =  pAIDataFile->pAIData->pAIDriverData[i].skill;
		pNewAIDataFile->pAIData->pAIDriverData[i].temper =  pAIDataFile->pAIData->pAIDriverData[i].temper;
	}
	/* Free old one */
	MemoryFreePtr(pAIDataFile->pAIData->pAIDriverData);
	MemoryFreePtr(pAIDataFile->pAIData);
	MemoryFreePtr(pAIDataFile);
	pAIDataFile = NULL;
	/* Return the new one */
	return(pNewAIDataFile);
}

void AIBLoaderPatch(sAIDataFile *pAIDataFile)
{
	pAIDataFile->pAIData->pAIDriverData -= (int32)pAIDataFile->pAIData;
	pAIDataFile->pAIData -= (int32)pAIDataFile;
}	



Share this post


Link to post
Share on other sites
I don't understand your problem. What do you mean by "one after the other and not all over the place"?
What do you mean by "my data is lost"? Are you saying that the values of the struct being passed are not what you expected?

If you provide some more information about your problem maybe I'll be able to help.

Share this post


Link to post
Share on other sites
I presume he wants to something like with SecurityDescriptors which can be self-relative or absolute. Or if you're unfamiliar with that, like a variable length struct.

The obvious answer is: Dont do it!
It is perhaps premature optimisation. Why would you not just use malloc and let it allocate memory where it feels like?

Share this post


Link to post
Share on other sites
when i go in collate all my data in the struct such as skill is fine but then when i go into patch the data values are lost?

Share this post


Link to post
Share on other sites

This topic is 3870 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.

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