Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type should I use for an index variable

Tags:

c++

cuda

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.

like image 287
Andrew Redd Avatar asked Oct 27 '10 15:10

Andrew Redd


People also ask

What type of variable is an index?

Scales and indexes have several similarities. First, they are both ordinal measures of variables. That is, they both rank-order the units of analysis in terms of specific variables.

What is an example of an index variable?

For example, a questionnaire might have a respondent indicate five products and then indicate the number of times each of the five products was purchased. This results in five product variables and five variables representing the number of products purchased.

How do you use index variables?

You write INDEX, followed by the variables in braces: INDEX {variable 1, variable 2, . . .} results in variables 1, 3 and 5 stacked atop each other and variables 2, 4 and 6 stacked atop each other. where # refers to the order in which the variable appears in the INDEX statement.


3 Answers

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_t is a good choice for array index, since it's unsigned and you don't generally want to access myarray[-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). Use new (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.

like image 199
Steve Townsend Avatar answered Oct 19 '22 22:10

Steve Townsend


In reference to your follow-on question:

The best reference I have used for general high-level programming "current good practices" sort of thing is:

Code Complete by Steve McConnell (ISBN 0-7356-1967-0)

I reference it all the time. When my company formalized its coding standards, I wrote them based off of it. It doesn't go into design or architecture as much, but for actually banging out code, the book is appropriately named.

like image 32
Nate Avatar answered Oct 19 '22 22:10

Nate


cudaMalloc takes a size of type size_t, so for consistency, that's what you should use for indices.

like image 43
Steve M Avatar answered Oct 19 '22 22:10

Steve M