memory allocation problem

Started by
9 comments, last by iMalc 16 years, 10 months ago
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.pOpponant[j] = malloc(NoOfOpponants * sizeof(pRespectTable));
		}
	}
	
}



Advertisement
You don't allocate any storage for pDriver or pOpponant, but you attempt to access pOpponant, which causes an access violation.
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.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)?) 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.
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.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.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.pOpponant = malloc(NoOfDrivers * sizeof(pRespectTable));				for(j=0;j<NoOfDrivers;j++)		{			pRespectTable.pOpponant[j] = malloc(NoOfOpponants * sizeof(pRespectTable));		}	}	//Test to see if pOpponants array takes a char	for(i=0;i<NoOfDrivers;i++)	{			for(j=0;j<NoOfOpponants;j++)		{			pRespectTable.pOpponant[j]="name";		}	}			}
Quote:
pRespectTable.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 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.
Quote:Original post by jpetrie
Even if pOpponant 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.
Quote:i am using c because the engine is in c


Honestly that's a bad reason. You can still use the C engine with C++ to protect YOUR code from errors like these.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

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 100typedef 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.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.pOpponant[j],"name");		}	}			}void exit(){   free(pRespectTable);}
extern "C"
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.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.)

This topic is closed to new replies.

Advertisement