Click here to Skip to main content
15,917,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi there:)

I want to create a generic list, but I want the data to be compact, so I use an array into which I want to add the values of the elements, not the pointers to the elements. How could I do that?

What I have so far:
-In the constructor I get the number of elements and the dimension of one element.
-A void *list, which I allocate as number_elements*dimension;
-When I add, I get a void *pointer, and I add it to the index position as
list[index*dimension], but I don't know how to dereference the void pointer as to add the value, not the pointer, because the pointer may point to some temporary data, and I want the list to be compact, not spread everywhere in the memory, for a later memcpy.

Thank you :)
Posted
Comments
Aescleal 18-Aug-10 7:47am    
Reason for my vote of 1
Having seen Eugen's answer you're really heading for having some long term maintenance nightmares. Use std::vector is does what you want it to do without you having to muck about with raw memory.

Awooga! Mentions of void pointers and memcpy in anything to do with generic programming is usually a sign that you're doing something wrong...

From what you're describing above std::vector will do what you want. And if it doesn't it's what you should be thinking of using in your list implementation.

Cheers,

Ash
 
Share this answer
 
If you're attending a C++ course, you should really be using std::vector. Ask your professor if that's ok for your assignment if you are unsure.

If you're not attending a C++ course, use std::vector.

Really.

Edit: Avoid the temptation of falling back to C implementations using a C++ compiler. If you are learning C++, learn and make use of it's powerful features.
 
Share this answer
 
v2
This is common pattern when you want to keep things close together in memory for cache performance. If you look into the vc or gcc implementations of std::vector you'll see they do interesting things with void* and placement new. vector is great and suits 99% of cases but sometimes its not exactly what you need.

There is lots of template fun to be had as well.

Can you invoke the constructor, copy constructor and destructor of the type in the array? Might get you some more marks.
 
Share this answer
 
When I have anderstood correctly :) :
void YourList::SetDataElement(void* pDataElement, int iIndex)
{
  if (m_list && pDataElement &&
      0 <= iIndex && iIndex < m_ElemNum) {
    memcpy(&m_list[m_ElemSize * iIndex], pDataElement, m_ElemSize);
  }
}
 
Share this answer
 
Comments
Mihai Zaharescu 18-Aug-10 6:49am    
Reason for my vote of 5
Thank you, that is what I wanted :D
Aescleal 18-Aug-10 7:46am    
Reason for my vote of 1
Congratulations, you've just implemented something that vector does in a type safe fashion using std::initialised_fill or placement new. Why on earth people keep perpetuating this kind of rubbish amazes me...
Eugen Podsypalnikov 18-Aug-10 8:05am    
Hi Ash! It is usefull to be amazed or to learn how to "dereference" a dump pointer, I think... The both "tasks" are OK for me :)
Aescleal 18-Aug-10 8:14am    
The user isn't trying to analyse memory in case of a crash or other program weirdness - I've got no problem with that. However everyday I have to work through "optimised" "generic" code that makes all sorts of assumptions about the memory layout of the object it manages.

As a thought experiment what happens if the user of the code you've written tries to bung something like a shared_ptr in his "cache coherrent optimised for high speed" "list"?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900