Advertisement

Pointer casting and indirection is killing me!

Started by December 10, 2002 02:44 AM
12 comments, last by Alpha_ProgDes 21 years, 11 months ago
ok here''s the code....
  
void AddMember(Member **newMember) 
//in main() I plan to use the function like this:

//Member *newMemberList = NULL;

//newMemberList = (Member*)malloc(10 * sizeof(struct MemberData));

//AddMember(&newMemberList);


//therefore i can add Members directly (hopefully)

//Member == typedef struct MemberData

{
	int tempInt = 0;
	static int memberposition = 1;
	char tempNumber[3] = {0};
	char tempName[10] = {0};
	
	if (numOfMembers > sizeOfList)
		ResizeDatabase(*newMember);
...
}

/////////////////////////////////////////////////


struct MemberData ** ResizeDatabase(Member **moreMembers)
{

	int dbSize = 0;

    if (sizeOfList == MAX_DB_SIZE)
		printf("Sorry, database is full.\n");
    else if ((sizeOfList + RESIZE_PADDING) > MAX_DB_SIZE){
		dbSize = MAX_DB_SIZE - sizeOfList;
		moreMembers = (Member**)realloc(moreMembers, dbSize * sizeof(struct MemberData));
    }
    else {
		dbSize = RESIZE_PADDING;
		moreMembers = (Member**)realloc(moreMembers, dbSize * sizeof(struct MemberData));
    }
	sizeOfList += dbSize;
	return (moreMembers);

}
  
basically newMemberList is already pointing to newly reallocated memory. then i use AddMember to fill in the structs. but i want to automatically resize the array if i am going to go past the limit. it seems like all the pointer indirection is getting me confused. i look on MSDN for info on struct _iobuf... but i couldn''t find anything could someone help me. thanks. oh this is the error i''m getting: C:\Program Files\Microsoft Visual Studio\MyProjects\database\MemberList.cpp(76) : error C2664: ''ResizeDatabase'' : cannot convert parameter 1 from ''struct MemberData *'' to ''struct _iobuf *'' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Error executing cl.exe.

Beginner in Game Development?  Read here. And read here.

 

As wonderful as realloc is, you shouldn''t use it for this because:

1) Constructors / Destructors for class Member won''t get called.
2) Moving them around in memory is bad; what if they were pointing to each other?
3) Just because.

You should probably use a list or a vector.

If not, you could create a list of pointers to Member objects. That way they won''t move around in memory, and the constructors will still be called if you use new to create them. You could also reference the pointer, that would eliminate the need to dereference it in your code and make it easier to read.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Advertisement
Oh, never mind. I see that Member is a struct, not a class.

quote:
ResizeDatabase(*newMember);


I don't think you need to dereference it.

quote:
moreMembers = (Member**)realloc(moreMembers, dbSize * sizeof(struct MemberData));


I think that should be:


      *moreMembers = (Member*)realloc(*moreMembers, dbSize * sizeof(struct Member));    


[edited by - smart_idiot on December 10, 2002 4:20:38 AM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
why not use std::vector that does all of this for you?
struct _iobuf is the internal typedef for FILE.
I think you have to find a better way to handle the number of entries in the list. I think there are some bugs relating to that, too.

Anyway, I think you should use a vector or a list.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Advertisement
if this was for a C++ class (class as in college class) i would. but this is for C [programming class]. but thank you for answering and helping. i appreciate it.

Beginner in Game Development?  Read here. And read here.

 

I guess that would make it hard to use references and vectors. I just assumed that because you had (Member**)realloc, I don''t think it''s necessary to cast void pointers in C.

I think there is something wrong with how you are calculating the value for dbSize but since this is for a class and it''s 6 am where I am, I''m going to go sleep now and not help you. Good luck anyway, though.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Very easy

You have the following prototypes:

void AddMember(Member **newMember)
struct MemberData ** ResizeDatabase(Member **moreMembers)

but when you call ResizeDatabase from AddMember you use:
ResizeDatabase(*newMember);
instead of
ResizeDatabase(newMember);

However I doubt very much about your code correctness.

Question:

if I have a struct called Truck. and its a double pointer like so:
Truck **Mack;
so do i assign a value to it?

*(Mack)->name = name; ??

Beginner in Game Development?  Read here. And read here.

 

This topic is closed to new replies.

Advertisement