Data not being stored in void * struct.
Apr 24, 2013 at 4:32pm UTC
I have the following struct:
1 2 3 4 5
struct memoryPointer {
int memoryIndex;
void * memoryLoc;
int memorySize;
};
When this returns 0:
1 2
memoryAlloc[freeMemoryLocation].memoryLoc=(void *)(vMemory + freeMemoryLocation + 1);
return memoryAlloc[freeMemoryLocation].memoryLoc;
But this does not:
1 2
memoryAlloc[freeMemoryLocation].memoryLoc=(void *)(vMemory + freeMemoryLocation + 1);
return (void *)(vMemory + freeMemoryLocation + 1);
Can someone please explain what I'm doing wrong, and how to fix it?
Thanks heaps!
Last edited on Apr 24, 2013 at 4:32pm UTC
Apr 24, 2013 at 5:30pm UTC
There is no enough information. The return type of the function is unknown. Also it is not known how you determinated that in the first case it is the zero that is returned.
Apr 24, 2013 at 5:40pm UTC
Hey Vlad,
Thank you for your reply, and sorry I didn't provide the information in the first post. Let me fill in your questions for you:
The return type of the function is a void *:
void * MemHeap::vcalloc(size_t size) {
When running the code, it would crash using:
return memoryAlloc[freeMemoryLocation].memoryLoc;
But work correctly when using
return (void *)(vMemory + freeMemoryLocation + 1);
I printed the results to console as such:
1 2
std::cout << memoryAlloc[freeMemoryLocation].memoryLoc << endl;
std::cout << (void *)(vMemory + freeMemoryLocation + 1) << endl;
The first output printed 0, and the second prints a memory address similar to 0x800102a8, which is what I expected.
Thank you very much for helping me with this issue.
Last edited on Apr 24, 2013 at 5:41pm UTC
Apr 24, 2013 at 8:00pm UTC
Well where is defined memoryAlloc? Could you show more code of the function?
Apr 24, 2013 at 8:17pm UTC
Hey Vlad,
Here's the function, how it's used, and some relevant headers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
struct memoryPointer {
int memoryIndex;
void * memoryLoc;
int memorySize;
};
memoryPointer * memoryAlloc;
MemHeap::MemHeap(): VHeap()
{
memoryAlloc = new memoryPointer[memorySize];
}
void * MemHeap::vcalloc(size_t size)
{
int freeMemoryLocation;
freeMemoryLocation = findFreeMemory(size);
if (freeMemoryLocation == -1)
{
std::cout << "Memory full." << endl;
}
else
{
memoryAlloc[freeMemoryLocation].memoryIndex=freeMemoryLocation;
memoryAlloc[freeMemoryLocation].memoryLoc=(vMemory + freeMemoryLocation + 1);
memoryAlloc[freeMemoryLocation].memorySize=size;
}
for (int i = 1;i<size;i++)
{
vMemory[i + freeMemoryLocation]=0;
}
return (void *)(vMemory + freeMemoryLocation + 1); //Wanting to place memoryAlloc[freeMemoryLocation].memoryLoc here instead.
}
int main(int argc, char ** argv) {
MemHeap cMemHeap;
int *myNewInt = (int *)cMemHeap.vmalloc(sizeof (int ));
}
If I use:
return (void *)(vMemory + freeMemoryLocation + 1);
As the return in the function, it gives *myNewInt the correct memory address.
However, if I use:
1 2
memoryAlloc[freeMemoryLocation].memoryLoc=(void *)(vMemory + freeMemoryLocation + 1);
return memoryAlloc[freeMemoryLocation].memoryLoc;
It returns 0. When printing from inside the function using:
std::cout << memoryAlloc[freeMemoryLocation].memoryLoc << endl;
It outputs 0, but if I place:
std::cout << (void *)(vMemory + freeMemoryLocation + 1) << endl;
It prints out the memory address of *myNewInt.
I'm not sure why it doesn't store the pointer in memoryPointer.memoryLoc since it's of type void * (Like the return value is).
Last edited on Apr 24, 2013 at 8:18pm UTC
Apr 24, 2013 at 8:24pm UTC
It seems to be working if I change the struct to to have memoryLoc to be a void **:
1 2 3 4 5
struct memoryPointer {
int memoryIndex;
void ** memoryLoc;
int cellSize;
};
But am unsure why XD.
Apr 24, 2013 at 8:47pm UTC
It looks loke that in the loop
1 2 3 4
for (int i = 1;i<size;i++)
{
vMemory[i + freeMemoryLocation]=0;
}
you are overwriting memory location of
memoryAlloc[freeMemoryLocation].memoryLoc;
I can not say more because I do not know how vMemory is defined.
Also it is strange that this loop is started from 1 and not from 0.
Topic archived. No new replies allowed.