Click here to Skip to main content
15,900,378 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionReusing an Incremented Variable within a Single Statement Pin
Skippums24-Aug-10 5:24
Skippums24-Aug-10 5:24 
AnswerRe: Reusing an Incremented Variable within a Single Statement Pin
Sauro Viti24-Aug-10 5:41
professionalSauro Viti24-Aug-10 5:41 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Skippums24-Aug-10 6:26
Skippums24-Aug-10 6:26 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
norish24-Aug-10 19:30
norish24-Aug-10 19:30 
AnswerRe: Reusing an Incremented Variable within a Single Statement PinPopular
Luc Pattyn24-Aug-10 5:50
sitebuilderLuc Pattyn24-Aug-10 5:50 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Skippums24-Aug-10 6:15
Skippums24-Aug-10 6:15 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Luc Pattyn24-Aug-10 6:33
sitebuilderLuc Pattyn24-Aug-10 6:33 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Skippums24-Aug-10 6:54
Skippums24-Aug-10 6:54 
I have two arrays being sent to my function from an outside source. The first array is a list of 1-based indices into the the second array that contains the data. When the first array has an index of 0, this means that there is no associated data element in the second array. So, for example:
const size_t indexArray[] = {1, 0, 3, 2, 0};
DATA_TYPE *const dataArray = new DATA_TYPE[3] - 1; // This is a 1-indexed array and has three elements in it (since max(indexArray) == 3)
Now, as I said, no matter what I do I have to read in the values of dataArray using API calls on a per-element basis. I could very easily copy the values from indexArray into another variable in my C-code, subtracting 1 from each element as I perform the copy. However, I think it is more straightforward (and more efficient) to just get a pointer to the existing indexArray data (using an API call) and subtract one from my dynamically allocated dataArray object as I illustrated in the above code. Using this approach, I can compare each element from indexArray to 0 to test for the special condition, whereas if I did a copy and subtracted one I would have to test against numeric_limits<size_t>::max(), and pray that the max() macro from windows.h wasn't included by some other header file that I am including. I may also need to make the copy loop more complex, if I don't want to assume that the result of (0u - 1u) is the maximum unsigned integer.

Hopefully that explains in better detail why I am not just converting it to 0-based indices.
Sounds like somebody's got a case of the Mondays

-Jeff

GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Aescleal24-Aug-10 6:42
Aescleal24-Aug-10 6:42 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Skippums24-Aug-10 7:06
Skippums24-Aug-10 7:06 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Aescleal24-Aug-10 8:23
Aescleal24-Aug-10 8:23 
GeneralRe: Reusing an Incremented Variable within a Single Statement [modified] Pin
Joe Woodbury24-Aug-10 6:52
professionalJoe Woodbury24-Aug-10 6:52 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Niklas L24-Aug-10 7:08
Niklas L24-Aug-10 7:08 
GeneralRe: Reusing an Incremented Variable within a Single Statement [modified] Pin
Joe Woodbury24-Aug-10 7:25
professionalJoe Woodbury24-Aug-10 7:25 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Niklas L24-Aug-10 7:55
Niklas L24-Aug-10 7:55 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Skippums24-Aug-10 7:17
Skippums24-Aug-10 7:17 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Joe Woodbury24-Aug-10 7:38
professionalJoe Woodbury24-Aug-10 7:38 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Paul Michalik24-Aug-10 8:00
Paul Michalik24-Aug-10 8:00 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Joe Woodbury24-Aug-10 8:50
professionalJoe Woodbury24-Aug-10 8:50 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Paul Michalik24-Aug-10 21:28
Paul Michalik24-Aug-10 21:28 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Stefan_Lang24-Aug-10 22:02
Stefan_Lang24-Aug-10 22:02 
RantRe: Reusing an Incremented Variable within a Single Statement Pin
JFDR_0225-Aug-10 6:37
JFDR_0225-Aug-10 6:37 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Skippums25-Aug-10 7:04
Skippums25-Aug-10 7:04 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Niklas L25-Aug-10 8:02
Niklas L25-Aug-10 8:02 
GeneralRe: Reusing an Incremented Variable within a Single Statement Pin
Skippums25-Aug-10 8:08
Skippums25-Aug-10 8:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.