Thread: Need Help

  1. #16
    Registered User
    Join Date
    Oct 2019
    Posts
    43
    Code:
        regarding header : memorytest.h
       /*#ifndef MEMORYTEST_H
       #define MEMORYTEST_H
        #include <stdlib.h>
        template <class type1, class type2>
        void allocateData1DbyMalloc(type1** data1D, type2 nRow) {
    	int size = (char*) &(*data1D)[1] - (char*) & (*data1D)[0];
    	*data1D = (type1*)malloc(nRow * size); // sizeof(type1));
         }
    
    
         template <class type1>
         void deAllocateData1DbyFree(type1** data1D) {
    	free(*data1D);
         }
    
    
           template <class type1, class type2>
           void allocateData2DbyMalloc(type1*** data2D, type2 nRow, type2 nColumn) {
    	int size = (char*) & (*data2D)[1] - (char*) & (*data2D)[0];
    	*data2D = (type1 * *)malloc(size * nRow); // sizeof(type1*)
    	if (*data2D) {
    		size = (char*) & (**data2D)[1] - (char*) & (**data2D)[0];
    	for (int i = 0; i < nRow; i++) {
    				(*data2D)[i] = (type1*)malloc(size * nColumn); // sizeof(type1)
    		}
    	}
          }
    
    
          template <class type1, class type2>
          void destroyMatrix2DbyFree(type1*** data2D, type2 numberOfRow) {
    	for (int row = 0; row < numberOfRow - 1; row++)
    		free((*data2D)[row]);
    	free(*data2D);
          }
          #endif

  2. #17
    Registered User
    Join Date
    Oct 2019
    Posts
    43
    you can try this header. hope, it should work

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It doesn't work for certain class types. I already explained why.

    Why don't you want to use sizeof?

    You explained why you don't want to use containers, but your explanation goes against best practice which is to default to containers, then measure and if containers turn out to be unacceptable, implement your own. Why don't you want to program in best practice C++?

    You explained your thinking behind using malloc so that you can replace malloc with something else. I informed you that the normal approach in C++ is to define a custom allocator for that "something else". Why don't you want to do this?
    Last edited by laserlight; 10-27-2019 at 10:26 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh sorry, I missed this because it was at the bottom of the previous page.
    Quote Originally Posted by Shafiul View Post
    Hi Laserlight, well, i don't know the answer of "I'm talking about correctly invoking constructors" because I don't get the question honestly. Can you explain your question a bit please if possible. Then i will try to do my best to answer it.
    Are you familiar with classes and constructors in C++?

    Quote Originally Posted by Shafiul
    Well, the idea is GPU system need a memory mapping between cudamalloc and malloc to transfer the data from CPU to GPU. Based on the memory mapping, sometimes I need to call realloc() to optimise the memory mapping between CPU and GPU.
    I'm not familiar with what you want to do, but as far as I know, the allocator system in C++ was designed to be flexible enough to handle unusual allocation requirements without sacrificing efficiency. Have you looked into implementing a custom allocator?

    The thing is, from post #1 it looked like you decided to write something that is entirely in C other than the use of function templates for generics. The problematic thing is that your code does resource allocation, and in a C++ context where there's the danger of exceptions being propagated around, failing to do things the "C++ way" means that your code might not be exception safe if you aren't exceptionally careful. So, you should either make use of C++ facilities for resource management, or rewrite your code in C even if that means pain from being unable to use templates.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Tags for this Thread