I am a total newbie but was just wondering if this suggessiont would work
couldn't you just create a copy of the pointer address used in the memory allocation and then compare it later with the current pointer to see if the address's are equal.
Re: I am a total newbie but was just wondering if this suggessiont would work
Quote:
Originally posted by manofsteel972
couldn't you just create a copy of the pointer address used in the memory allocation and then compare it later with the current pointer to see if the address's are equal.
You can. But what if you went around creating lots of pointers before you ever attempt to free anything. I believe conservation of resource(s) is the aim of this person.
Also whilst we're busying arguing about malloc/new, free/delete, Let's remember that they belong to two not so disimilar but fundamentally different environs; C++ = OOP and C = procedural (Yes I heard about objective C).
Re: How to spare resources
Quote:
Originally posted by manofsteel972
THe only reason why I mentioned that is because the Heap is one contiguous section of memory and has a base address so if you can compare the pointers address to the RANGE of heap then you can determine it it came from the heap or not and thus if it was allocated dynamically or am i wrong?
I never thought of that. How would you test that to see if it is a valid method?
I personally think it is the programmer's responsibility to keep track of all malloc()ed memory. (Just my opionion.)
If the programmer can't keep track of it, maybe the program should be re-architected.
Re: Re: How to spare resources
>>How would you test that to see if it is a valid method?
Through no standard means that I know of.
>>I personally think it is the programmer's responsibility to keep track of all malloc()ed memory. (Just my opionion.)
Your opinion, and a good few others too ;)
>>If the programmer can't keep track of it, maybe the program should be re-architected.
... and that's the correct answer too.
Here's one way. Personally, I'd avoid using something like this, but I guess someone might find it useful.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum memorytypes { MT_DYNAMIC, MT_STATIC } memorytypes_t;
typedef struct DATA
{
memorytypes_t MT;
char *msg;
} data_t;
void foo(data_t *Data, int i)
{
char *p = "This is static";
switch (i)
{
case 1:
Data->msg = malloc(20); /* Do some error checking! */
strcpy (Data->msg, "This is dynamic");
Data->MT = MT_DYNAMIC;
break;
default:
Data->msg = p;
Data->MT = MT_STATIC;
break;
}
}
int main(void)
{
data_t dt1, dt2;
foo (&dt1, 1);
foo (&dt2, 2);
printf ("dt1 : %s\n", dt1.msg);
printf ("dt2 : %s\n", dt2.msg);
if (dt1.MT == MT_DYNAMIC)
{
printf ("freeing dt1 %p\n", (void*)dt1.msg);
free (dt1.msg);
}
if (dt2.MT == MT_DYNAMIC)
{
printf ("freeing dt2 %p\n", (void*)dt2.msg);
free (dt2.msg);
}
return(0);
}
/* Output
dt1 : This is dynamic
dt2 : This is static
freeing dt1 00842934
*/
I can't help but feel the whole malloc/new thing is going a tad off topic, as the OP is writing in pure C (with a touch of ASM? :confused: ) :
Quote:
Posted by Xzyx987X
Well, I already commited myself to using pure C for this program. Is there perhaps a way to simulate a try/catch in C using ASM?