Thread: Class for both dynamic allocated buffer and static buffer

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    Class for both dynamic allocated buffer and static buffer

    I'm trying to figure out if there is any benefit of creating a class template where a buffer can be statically allocated, useful for small buffers located on the stack and dynamically allocated buffers.

    I want to combine these

    Code:
    template<int tsize>
    class MyStaticClass
    {
    	char buffer[tsize];
    public:
    	void Method1();
    	void Method2();
    };

    Code:
    class MyDynamicClass
    {
    	char *buffer;
    public:
    	void Method1();
    	void Method2();
    };
    If we can default the size template<int tsize = 0> so that default is zero which means that the class should be dynamically allocated.

    Code:
    template<int tsize = 0>
    class MyClass
    {
    	char buffer[tsize];
    public:
    	void Method1();
    	void Method2();
    };
    
    template<>
    class MyClass<0>
    {
    	char *buffer;
    public:
    	void Method1();
    	void Method2();
    };
    Now if the template parameter is zero the buffer variable will be a pointer instead of a vector.

    My question is the compiler will insert methods for every size of the tsize or is it smart enough to make it general so that for any tsize > 0 will be generic?

    Also when using dynamically allocated MyClass you still have to have brackets, MyClass<>. Is there a way to make the class so that the brackets aren't needed when it is a dynamic allocated class?

    Should I separate the dynamic case and the static case? Is it better to have a separate class for the dynamic allocated case and static case?

    The reason for the merge is that I want to more generic classes so that both cases originates for the same base class but maybe I'm going to far in this case.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    My question is the compiler will insert methods for every size of the tsize or is it smart enough to make it general so that for any tsize > 0 will be generic?
    Compilers may be smart enough to merge code for different tsize.

    Also when using dynamically allocated MyClass you still have to have brackets, MyClass<>. Is there a way to make the class so that the brackets aren't needed when it is a dynamic allocated class?
    No way.

    Should I separate the dynamic case and the static case? Is it better to have a separate class for the dynamic allocated case and static case?
    The functionality might be a bit different. E.g in the dynamic case you need to be able to set the size, in the other case it can't be changed.

    Do you really need to invent your own container classes? Couldn't you rather write generic free functions instead?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Also when using dynamically allocated MyClass you still have to have brackets, MyClass<>. Is there a way to make the class so that the brackets aren't needed when it is a dynamic allocated class?
    Aren't needed ...where?
    If when making the class...[No way. ^]
    If for using the class....just use a typedef.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    My question is the compiler will insert methods for every size of the tsize or is it smart enough to make it general so that for any tsize > 0 will be generic?
    That's not very likely going to happen; you'll most likely get a lot of duplicate code for every size with what you have here.

    If you want the implementation of the template instances to be shared as much as possible, you can put the static buffer in another class and separate as much of the size bound interface into that class as possible. The real class can then treat that result as being bound through normal polymorphism, but with this approach you lose some of the benefits of having the size bound to the primary interface class.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what's a "dynamic buffer"?
    By MK27 in forum C Programming
    Replies: 22
    Last Post: 08-08-2008, 08:48 AM
  2. Static objects allocated with new
    By Mario F. in forum C++ Programming
    Replies: 12
    Last Post: 07-20-2007, 08:08 AM
  3. Static array is allocated in undefined area
    By esben in forum C Programming
    Replies: 4
    Last Post: 05-13-2006, 05:24 PM
  4. fgets(buffer,sizeof(buffer),stdin);
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 10-28-2003, 10:41 AM
  5. serial programming, threads and dynamic buffer
    By lectrolux in forum C Programming
    Replies: 1
    Last Post: 05-06-2003, 09:59 AM