Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 1,103 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
free() #320962
04/26/10 08:30
04/26/10 08:30
Joined: Apr 2009
Posts: 248
Philippines
seecah Offline OP
Member
seecah  Offline OP
Member

Joined: Apr 2009
Posts: 248
Philippines
Hello Guys,

I've been experiencing a very odd problem on using the free() function in lite-C. It works on some other instances but it'll give me a problem when I combine it with memcpy(). I have an accessing problem on the last index of the array.

To give you the idea I have an initializations of structures in a .h file

Code:
typedef struct PLAYER_SCORE_STRUCT
{
	var m_nScore;
	var m_nlevel;
	var m_nSkin;
	double m_nDate;
	STRING *m_timeStr;
	STRING *playerName;
	
}PLAYER_SCORE_STRUCT;

And a pointer on it..

PLAYER_SCORE_STRUCT *playScoreArr;



And now when I read the file and store it on playScoreArr:

Code:
playScoreArr = malloc(MAX_PLAYER_LIST * MAX_LEVEL * sizeof(PLAYER_SCORE_STRUCT));
    
    numRecords = fileRead(fileName, dirPath, playScoreArr, 0);
    
    for(indexer=0; indexer < numRecords; indexer++)
    {
        if(str_cmpi(playScoreArr[indexer].playerName, strPlayerName) == 1 )
        {
            int arrIndx = indexer;        //get the index of the element to be removed
    
            if(indexer == numRecords - 1 && (&playScoreArr[indexer] != NULL)) // last record
            {
                free(&playScoreArr[indexer]);
            }
            else
            {
		        for(arrIndx; arrIndx < numRecords-1; arrIndx++)    //iterate through the array starting from the index
		        {
		            memcpy(&playScoreArr[arrIndx], &playScoreArr[arrIndx+1],sizeof(PLAYER_SCORE_STRUCT));
		        }
		        
		        //free the last record
		        if((&playScoreArr[numRecords-1]) != NULL) free(&playScoreArr[numRecords-1]);
		        indexer--; // to recheck new value of deleted position
            }
            
            numRecords--;
        }
    }



I've got a crashed on this line

free(&playScoreArr[indexer]);

It is the last index of the array.

Any idea why??

Please advise!



Can't is not an option™
Re: free() [Re: seecah] #320970
04/26/10 10:45
04/26/10 10:45
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
It seems to me that you are trying to 'free' something that was never 'malloc'ed.
This is bad.

You have malloc'ed just one big block of memory, not lots of little ones.
Therefore you cant free just the last index, because you cant free "just a part" of a block.
Not as far as I am aware.


PS :: Why are you still using malloc and free, rather than sys_malloc and sys_free? (un-important question)


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: free() [Re: EvilSOB] #321104
04/27/10 06:23
04/27/10 06:23
Joined: Apr 2009
Posts: 248
Philippines
seecah Offline OP
Member
seecah  Offline OP
Member

Joined: Apr 2009
Posts: 248
Philippines
The only reason of using malloc is the experience of using it in C and C++.

Is it the line below allocating the blocks of memory?

MAX_PLAYER_LIST = 100;
MAX_LEVEL = 25

Quote:

playScoreArr = malloc(MAX_PLAYER_LIST * MAX_LEVEL * sizeof(PLAYER_SCORE_STRUCT));


And actually I can free(playScoreArr[numOfRecords-2]), the only problem is that I can't free the index on numOfRecords-1 index (the last element)..



Can't is not an option™
Re: free() [Re: seecah] #321127
04/27/10 10:00
04/27/10 10:00
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
I'll try to explain, as I cant find any CLEAR on-line doco outlining what I mean.

Its up to you to point out any flaws in my argument, as this is MY logical interpretation of how malloc/free work.

AFAIK, "malloc(size)" allocates a single block of memory, regardless of the FORMULA used to calculate 'size'. (FYI - your formula looks fine to me)

So malloc itself, doesnt know or care what size chunks you are going to be using it in. ie - sizeof(PLAYER_SCORE_STRUCT)

So if you say "free(playScoreArr[6]);", how does it know how many bytes to delete?

I would guess the free either fails, or it frees the REST of the block, starting at "playScoreArr[6]". I suspect this is the case...
eg1.
You are trying to free "playScoreArr[numOfRecords-2]", but it is actually freeing "playScoreArr[numOfRecords-2]" AND "playScoreArr[numOfRecords-1]",
so when you later try to free "playScoreArr[numOfRecords-1]", its already been freed, and generates the error.
eg2.
You are trying to free "playScoreArr[2352]", but it is actually freeing "playScoreArr[2352]" RIGHT THROUGH TO "playScoreArr[numOfRecords-1]".
So when you later try to free "playScoreArr[higher-than-2352]", its already been freed, and generates the error.

How does that fit the behaviour you are suffering from?



"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: free() [Re: EvilSOB] #321131
04/27/10 10:30
04/27/10 10:30
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
What you are trying to do is normally done in two steps.

1. Creating a dynamic array of pointers to structs with malloc.
2. Creating structs for each array element and assigning their address to each array element with malloc.

Then your method to free will work.

Other option:

Just use one malloc and free the whole block.


Always learn from history, to be sure you make the same mistakes again...
Re: free() [Re: Uhrwerk] #321143
04/27/10 11:03
04/27/10 11:03
Joined: Apr 2009
Posts: 248
Philippines
seecah Offline OP
Member
seecah  Offline OP
Member

Joined: Apr 2009
Posts: 248
Philippines
Cool.. Nothing I can ever say but bow to you my masters..

Thanks a lot for explaining it clearly. It makes my day, once again thank you very much..



Can't is not an option™
Re: free() [Re: seecah] #321145
04/27/10 11:05
04/27/10 11:05
Joined: Apr 2009
Posts: 248
Philippines
seecah Offline OP
Member
seecah  Offline OP
Member

Joined: Apr 2009
Posts: 248
Philippines
BTW.. I just want to let you know guys that the crashed happens only on Vista an Windows 7.. therefore I conclude that Windows XP is not that strict in memory management..

Thanks..



Can't is not an option™
Re: free() [Re: seecah] #321160
04/27/10 12:17
04/27/10 12:17
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
I would guess that is because the memory handling functions are API based,
and they vary in 'core kernal programming' from OS version to version,
even though they remain the same at a parameter level.


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1