-
problem in windows vista
Hi all.
I am developing a RAID configuration tool. As a part of creation of a volume, i am
allocating some memory dynamically , using malloc() function for n number of pointers and later i am trying to free the memory using the free function.
However the free() function throws an exception some times. It shows some hexadecimal address and says that the memory at that location could not be read.
This is not happening in any other platforms like server2003 ,windows xp but only on Vista.
the code some thing like this
Code:
pptr = (struct xyz **) malloc(
sizeof(struct xyz *) * 8);
......
// code to create the RAID volume. The 8 pointers that have been allocated memory above, are intialized to some addresses here.
......
for(i=0; i< numdisks; i++)
{
if(pptr[i]!=NULL)
{
free(pptr[i]);
}
}
Here the value of numdisks is always less than or equal to 8. So there is no chance of freeing an uninitialized pointer.
Please let me know if there is any thing specific to windows vista that is throwing such error message.
-
You need to free ONLY ONCE, if in your code you malloc only one time like you show:
Strange that is is only a Vista thing
EDIT: If by chance you do this afterwards (not shown in code)
Code:
for(i=0;i<8;++i)
pptr[i] = malloc(sizeof(struct xyz)*something));
then you shouldn't get an error the way you free if you have NOT already have free(pptr)
-
malloc() & free() are C functions, therefore they cannot throw exceptions.
-
I am assuming "throwing an exception" means something along the lines of access violation, or segmentation fault, not actually throwing as in C++'s meaning of throwing.
-
> Here the value of numdisks is always less than or equal to 8. So there is no chance of freeing an uninitialized pointer.
Says who?
If you didn't
a) assign a value with malloc
b) assign a value of NULL
when you created the thing, then your if(pptr[i]!=NULL) thing is utterly pointless. If the memory pointer is garbage, it won't be NULL and it won't be a valid pointer. Either way, you lose.
It's also pointless if you did those things, because free(NULL) is a harmless thing.
It would seem to me that you're assuming that malloc initialises memory (it doesn't). As such, if you don't use all 8 slots, then you're freeing garbage.
-
Everyone has got on you about the one thing your code did correctly, instead of pointing out the one area where you did things wrong
Example:
Code:
#define RAID_SIZE 8
// later on in your code...
pptr = reinterpret_cast<xyz **>(malloc(sizeof(xyz *) * RAID_SIZE));
if(!pptr)
return; // or whatever
for(int i = 0; i < RAID_SIZE; ++i)
pptr[i] = malloc(sizeof(xyz));
// later on
if(pptr)
{
for(int i = 0; i < RAID_SIZE; ++i)
{
if(pptr[i])
free(pptr[i]);
}
free(pptr);
}
-
By the way, the missing step that I added coupled with the fact that you were trying to free each un-allocated pointer as if you had generated them as demonstrated in my example. So although everyone flamed you for doing something that technically wasn't wrong, they are right in stating that it is not doing what you think it is doing. Using my sample code though you should be a-ok.