|
Help!
by VoroneTZ. 10/14/25 05:04
|
|
|
|
|
|
|
|
2 registered members (AndrewAMD, 1 invisible),
7,036
guests, and 2
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Errr. DUH! Need help with advanced SYS_MALLOCing ....[RESOLVED]
#388454
12/03/11 06:18
12/03/11 06:18
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Its a bit of a dumb one, I know. But I cant see whats going wrong here... I create a 2D array of pointers like so...
ENTITY* *slab_ents[4];
void main()
{
for(LOD=0; LOD<4; LOD++)
{ slab_ents[LOD] = (ENTITY*)sys_malloc(a_set_size); }
...
And it works fine. BUT when I try to clear it with
...
for(LOD=0; LOD<4; LOD++) { sys_free(slab_ents[LOD]); }
I get "Malfunction W1516 : Invalid memory area" errors. Any ideas? Its obviously a pointer issue, but I just want some second opinions...
Last edited by EvilSOB; 12/04/11 17:00. Reason: RESOLVED
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Errr. DUH! Need help with advanced SYS_MALLOCing please.
[Re: EvilSOB]
#388460
12/03/11 10:17
12/03/11 10:17
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
You're lucky that worked in the first place. You forgot to assign memory to slab_ents, so in your first loop you're overwriting memory. In your second loop you then free random memory area as the data at that memory address could point anywhere in the meantime.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Errr. DUH! Need help with advanced SYS_MALLOCing please.
[Re: Uhrwerk]
#388468
12/03/11 15:28
12/03/11 15:28
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
O...K....
But why do I need to assign memory to the first array? I told it in the define I needed 4 elements?
What I wanted was to define four(4) pointers that are pointing to an array of Entity pointer.
So do I need to malloc the four? Seeing as I put that in the define? If that is the case, what use is having the 4 in there at all? What is it now doing?
NOTE: Yes I know I could use just "ENTITY **slab_ents;" (or is that "ENTITY ***slab_ents;") and a double layer of sys_malloc, but Im trying to avoid that for simplicity.
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Errr. DUH! Need help with advanced SYS_MALLOCing please.
[Re: Superku]
#388505
12/03/11 21:55
12/03/11 21:55
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Its shorthand for 'an un-important number'.
In reality it is short for "sys_malloc(sizeof(ENTITY*)*(ent_count[LOD]+1))"
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Errr. DUH! Need help with advanced SYS_MALLOCing please.
[Re: EvilSOB]
#388514
12/03/11 23:04
12/03/11 23:04
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
OK Uhrwerk, Im not ARGUING with you, but I want to try to find out where my logic falls down. Because, to my mind, it should work... So please follow MY logical progression and point out where it goes from right to wrong... Also be aware that I am operating under the BELIEF that the declarations
"ENTITY** slab;" and "ENTITY **slab;" and "ENTITY* *slab;" are all functionally identical.So here goes, from blindingly obvious to hideously obscure...
long slab; [allocates a single LONG in memory]
long slab[4]; [allocates four countiguous LONGs in memory]
long* slab[4]; [allocates four countiguous pointers in memory to other LONGs]
ENTITY* slab[4]; [allocates four countiguous pointers in memory to ENTITYs]
ENTITY* *slab[4]; [allocates four countiguous pointers in memory to an
array of ENTITY pointers (ie ENTITY**) ]
I will shortly follow up with a WORKING code sample of my REQUIRED results are using the nested sys_malloc system, so everyone can see what Im TRYING to achieve...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Errr. DUH! Need help with advanced SYS_MALLOCing please.
[Re: EvilSOB]
#388517
12/03/11 23:22
12/03/11 23:22
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
O.....K..... Ive done the working example as found below inside the spoiler. This works (well) as expected, but I still want to declare the array with ENTITY **slab_ents[4]; instead of ENTITY ***slab_ents; So Im going to try reverse engineering this example and see how we go...
ENTITY ***slab_ents;
void create_slabs()
{
var LOD, i;
slab_ents = (ENTITY***)sys_malloc(sizeof(ENTITY**)*4);
for(LOD=0; LOD<4; LOD++)
{
slab_ents[LOD] = (ENTITY**)sys_malloc(sizeof(ENTITY*)*10);
for(i=0; i<10; i++)
{
(slab_ents[LOD])[i] = ent_create("slab3.mdl", nullvector, NULL);
}
}
}
void slabs_terminate()
{
var i, LOD;
for(LOD=0; LOD<4; LOD++)
{
for(i=0; i<10; i++)
{
ent_remove((slab_ents[LOD])[i]);
}
sys_free(slab_ents[LOD]);
}
sys_free(slab_ents);
}
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Errr. DUH! Need help with advanced SYS_MALLOCing please.
[Re: EvilSOB]
#388551
12/04/11 09:48
12/04/11 09:48
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
long slab; [allocates a single LONG in memory] Correct!
long slab[4]; [allocates four countiguous LONGs in memory]Correct!
long* slab[4]; [allocates four countiguous pointers in memory to other LONGs]Wrong! see below
ENTITY* slab[4]; [allocates four countiguous pointers in memory to ENTITYs] Wrong! see below
ENTITY* *slab[4]; [allocates four countiguous pointers in memory to an
array of ENTITY pointers (ie ENTITY**) ]Wrong! see below.
I think I got your misunderstanding now. Ok, you first example was "long slab;" which in fact used 8 bytes of memory. Now why is it using 8 bytes? Because that's the size of a long variable. So far so good. "long slab[4]" will allocate 8 * 4 = 32 bytes. Why? Because one long needs 8 bytes and you need space for four of them. Now to the examples below which work all the same way. In "long* slab[4];" you declare a pointer (!!) to some longs. This will require 4 bytes of memory, because that's the size of a pointer. The compiler does not care about the fact, that the pointer should point to an array of four longs. It just sees that you're declaring a pointer and that's it for memory consumption - 4 bytes. The rest of the declaration is just used by the static type system. Until now your example is a 32 bit pointer pointing to mount doom. So you better initialize it like "long* slab[4] = NULL;". This will show you and other programmers it's not pointing to anywhere at the moment. If you want to use your pointer to the array you have to allocate memory for it. "long* slab[4] = sys_malloc(4 * 8);" Not doing this will result in overwriting random memory or reading from random memory when accessing the variable.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Errr. DUH! Need help with advanced SYS_MALLOCing please.
[Re: Uhrwerk]
#388567
12/04/11 14:55
12/04/11 14:55
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
OK, why IS long using 8 bytes? Damn good question! Because the manual, in the 'Variables' entry, in the variable-types table, it states that int & long use 4 bytes, not 8! Only double uses 8 bytes...
And are you sure about "long* slab[4];" only allocating 4 bytes? And does anyone have a way of testing this?
To MY thinking (and my intention) it should allocate 32 bytes. It SHOULD be creating 4 pointers to 1 long EACH. In the same way as "ENTITY* slab[4];'" does...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Errr. DUH! Need help with advanced SYS_MALLOCing please.
[Re: EvilSOB]
#388573
12/04/11 15:52
12/04/11 15:52
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
Because the manual, in the 'Variables' entry, in the variable-types table, it states that int & long use 4 bytes, not 8! You're right about this, my bad. Sorry, too much java programming.  For the rest. I am pretty sure about this. But my c/c++ expertise is limited. Maybe someone else with more experience can jump in? Basically you can test the size of any type with the following code snippet:
#include <acknex.h>
int main()
{
printf("%d",(long)sizeof(long*));
}
Always learn from history, to be sure you make the same mistakes again...
|
|
|
|