This is a best practices question. I am making an array
type * x = malloc(size*sizeof(type));
AFAIK sizeof gives a return value of size_t. Does that mean that I should use a size_t to declare, or pass around size? Also when indexing the array should I also use a size_t for the index variable? What is the best practice for these these? This is not something that they taught in school, and now that I’m getting into serious c++ I want to know.
Also if anyone has references of where I can find best practices for this kind of stuff it would be helpful? Kind of an etiquette for programmers book.
EDIT:
The malloc should be cudaHostAlloc, or cudaMalloc, since I am developing a class that stores an array simultaneously on the device and host, and updates both at the same time. So malloc here is just a place holder for what I’ll actually be doing.
In general, I use whatever minimizes the number of implicit or explicit casts and warning errors. Generally there is a good reason why things are typed the way they are.
size_tis a good choice for array index, since it’sunsignedand you don’t generally want to accessmyarray[-1], say.btw since this is C++ you should get out of the habit of using
malloc(free) which is part of CRT (C runtime library). Usenew(delete), preferably with smart pointers to minimize manual memory handling.Once you have mastered the basics, a good practices reference (language-specific) is Effective C++ by Scott Meyers. The logical next step is Effective STL.