Sign in to follow this  

memory allocation problem

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

Using C not c++ i am trying to allocate memory to create an array for a data member in my struct but i keep getting a 'Access violation writing location 0xcdcdcdcd'. can any give any suggesstions?
typedef struct
{
	char *pDriver;
	char *pOpponant;
} _sRespectTable;

_sRespectTable *pRespectTable;

void init()
{
	int i,j=0;
	int NoOfDrivers = 10;
	int NoOfOpponants = NoOfDrivers-1;

	//allocate memory
	pRespectTable = malloc(NoOfDrivers * sizeof(_sRespectTable ));

	//now i want to allocate memory to make pOpponant an array
	for(i=0;i<NoOfDrivers;i++)
	{
		for(j=0;j<NoOfOpponants;j++)//This is where i fail
		{
			pRespectTable[i].pOpponant[j] = malloc(NoOfOpponants * sizeof(pRespectTable[i]));
		}
	}
	
}



Share this post


Link to post
Share on other sites
You've read Superpig's Introduction to Debugging, right?

Continuing the conversation you left on IRC:

pOpponant is uninitialized (and, with your debug CRT, ends up set to 0xCDCDCDCD to point this out). You then try to dereference pOpponent:

pRespectTable[i].pOpponant[j]

You then do a completely nonsensical assign, which on MSVC8, this generates the warning:

warning C4047: '=' : 'char' differs in levels of indirection from 'void *'


In short, you're trying to assign a (void*) pointer to something that isn't even a pointer. Pay attention to your damn warnings if you're so masochistic as to use C, this wouldn't even compile as C++.

I honestly can't tell what the hell you're trying to do. Neither could the other people in the IRC channel. And you didn't clarify at all before you left either. Is each _sRespectTable entry supposed to have NoOfOpponants entries? (If so, you almost certianly don't want sizeof(pRespectTable[i])?) What's with the correlation between NoOfDrivers then? And just generally... what are you trying to do? Because I certainly can't tell.

Sidenote: This is my last bone. Unless I've been confusing posters, this is the umpteenth time you've wanted help with something arising out of what appears to be a sore need for a review of language basics -- e.g. pointers -- and so it seems you're not doing so. Even if you aren't, word of advice: Set your compiler to treat warnings as errors. You need 'em. Hell, I'd need them in C.

Share this post


Link to post
Share on other sites
i am using c because the engine is in c, otherwise i would just use a vector, anyhow what i am doing is to just create an array of pRespectTable, i.e pRespectTable[i].pDriver = "Name"; which is all setup and works fine. what i want is to create another array in that iteration to store the names of the other drivers that he has repect for. it all compiles at the moment but i keep
getting 56'8' in my pRespectTable[i].pOpponant[j].



typedef struct
{
char *pDriver;
char *pOpponant;
} _sRespectTable;

_sRespectTable *pRespectTable;

void init()
{
int i,j=0;
int NoOfDrivers = 10;
int NoOfOpponants = NoOfDrivers-1;

pRespectTable = malloc(NoOfDrivers * sizeof(_sRespectTable));

for(i=0;i<NoOfDrivers;i++)
{
pRespectTable[i].pOpponant = malloc(NoOfDrivers * sizeof(pRespectTable[i]));

for(j=0;j<NoOfDrivers;j++)
{
pRespectTable[i].pOpponant[j] = malloc(NoOfOpponants * sizeof(pRespectTable[i]));
}
}

//Test to see if pOpponants array takes a char
for(i=0;i<NoOfDrivers;i++)
{
for(j=0;j<NoOfOpponants;j++)
{
pRespectTable[i].pOpponant[j]="name";
}
}

}


Share this post


Link to post
Share on other sites
Quote:

pRespectTable[i].pOpponant[j]="name";

That's broken. pOpponant is a char*. pOpponant[n] is a char. You cannot perform that assignment and get a reasonable result.

Even if pOpponant[i] was the correct type, you can't do that; you can't assign "strings" like that in C, you just leaked the memory you allocated with malloc(). You need to use strcpy() to copy strings.

I'm with MM here. I think you either need to stop using an engine in a language you obviously have only a passing familiarity with, or you need to get yourself a good book on C and learn it.

And turn on the error/warning level all the way up and listen to them.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Even if pOpponant[i] was the correct type, you can't do that; you can't assign "strings" like that in C, you just leaked the memory you allocated with malloc(). You need to use strcpy() to copy strings.


I would go as far as suggest the use of strncpy, even for adequately-sized buffers, just in case.

Share this post


Link to post
Share on other sites
Ok i know how to automatically bby defining the size, bbut what if i want to dynamically want to get the number of players from a file and then set the char *pOpponant to the size of the opponants?


#define MAX_OPPONANTS 100

typedef struct
{
char *pDriver;
char *pOpponant[MAX_OPPONANTS];
} _sRespectTable;

_sRespectTable *pRespectTable;

void init()
{
int i,j=0;
int NoOfDrivers = 10;
int NoOfOpponants = NoOfDrivers-1;

pRespectTable = malloc(NoOfDrivers * sizeof(_sRespectTable));

//Test to see if pOpponants array takes a char
for(i=0;i<NoOfDrivers;i++)
{
pRespectTable[i].pDriver;//test to see which driver it is
for(j=0;j<NoOfOpponants;j++)
{
//copy the names of the opponants that the driver has to respect
strcpy(RespectTable[i].pOpponant[j],"name");
}
}

}
void exit()
{
free(pRespectTable);
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Prog101
i am using c because the engine is in c,

Fun fact: C++ is a totally broken ass language in many very major ways by bending over completely backwards in an effort -- a successful one, even pre-standard -- to be backwards compatible with C.

Your current problem is that C style strings do not automatically manage themselves. You must explicitly allocate memory for RespectTable[i].pOpponant[j], and it must be of sufficient size for your string to fit. That this is not obvious to you confirms my earlier suspicion that it's not just a mistake, but a fundamental lack of language knowledge that is the problem here.

If you're going to persist with using C here, you need to learn your basic C string manipulation, which is very literally square one of learning C, right after outputting stuff to screen.

(This is the end of my bone to you. Good luck.)

Share this post


Link to post
Share on other sites
Perhaps something like the below is more like what you are wanting to do?
If not, then you'll have to explain things better.
typedef struct
{
char *pDriver;
char *pOpponent;
} _sRespectTable;

struct _sRespectTable *pRespectTable;

const int MAX_DRIVER_SIZE = 32; // Arbitrary, for demonstrtion purposes
const int MAX_OPPONENT_SIZE = 32; // Arbitrary, for demonstrtion purposes

void init(void)
{
int NoOfDrivers = 10;

pRespectTable = malloc(NoOfDrivers * sizeof(_sRespectTable));

for(int i=0; i<NoOfDrivers; i++)
{
pRespectTable[i].pDriver = malloc(MAX_DRIVER_SIZE);
pRespectTable[i].pOpponent = malloc(MAX_OPPONENT_SIZE);
strcpy(pRespectTable[i].pDriver, "Driver");
strcpy(pRespectTable[i].pOpponent , "Opponent");
}
}
Oh, and I corrected your spelling of "opponent".

Share this post


Link to post
Share on other sites

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