Skip to content

Conversation

@valeriyvan
Copy link
Contributor

Changes type from int to size_t to stop compiler complaining "Implicit conversion loses integer precision: unsigned long to int".

Changes type from `int` to `size_t` to stop compiler complaining "Implicit conversion loses integer precision: `unsigned long` to `int`".
@craigsapp
Copy link
Owner

In this case I would prefer:

		for (int j=(int)events.size() - 1; j >= 0; j--) {

since j >=0 would always be true for data type size_t.

@craigsapp
Copy link
Owner

(I can make the change or you can update the pull request).

@valeriyvan
Copy link
Contributor Author

I don't think what you propose is correct.

@valeriyvan
Copy link
Contributor Author

I see your point!
You are trying work correctly around when size() returns 0.
But downcast from size_t to int is anyway incorrect.
Let me think a moment and propose better change.

@craigsapp
Copy link
Owner

I will have to think about this more. In theory your are correct since arrays cannot have negative sizes, but in practice I do not like using unsigned ints for iterators when I know that the size of the array is guaranteed to be much less than 2000000000.

Here is an example snippet of code:

	for (i=0; i<curtime.size(); i++) {
		tsdur = measuredata[i]->getTimeSigDur();
		if ((tsdur == 0) && (i > 0)) {
			tsdur = measuredata[i-1]->getTimeSigDur();

The problem is that I am using the index of [i -1]. In this particular case it is not a problem since i>0 is a condition that is tested before using that index, but if i were 0 then the index would be four billion. This is of course just as bad as -1, but it seems to me to be less bad. I usually have to fix more problems related to this sort of stuff when I used unsigned in iterators (particularly the case of >= 0 which you tried to do :-)

And probably the main reason I do ints is (or now was) for cross-compiler compatibility. I was using Visual C++ 6.0 a long time ago, and I do not think that it had a size_t data type. I presume Microsoft visual studio now has it (but I have not compiled for Windows since VC++ 6).

Also note that humlib uses tab indentation (unlike verovio which uses four spaces :-)

@valeriyvan
Copy link
Contributor Author

And probably the main reason I do ints is (or now was) for cross-compiler compatibility. I was using Visual C++ 6.0 a long time ago, and I do not think that it had a size_t data type. I presume Microsoft visual studio now has it (but I have not compiled for Windows since VC++ 6).

I believe size_t is in C since at least C99. Same for C++. I am sure it's there in any C/C++ compiler you could find.

@valeriyvan
Copy link
Contributor Author

I will have to think about this more. In theory your are correct since arrays cannot have negative sizes, but in practice I do not like using unsigned ints for iterators when I know that the size of the array is guaranteed to be much less than 2000000000.

You said about portability, not me. What about 32 bit architectures. What about 16 bit? Casting to int reduces max number of elements from 65535 to 32767.

Not to say that casting from size_t to int is just not right thing to do.

@craigsapp
Copy link
Owner

The problem is that the C++ compiler in my head is pre C99 😜 I disliked all of the compiler changes up until C++11, so I am using a hybrid of Borland C++92 and C++11 (with an occasional C++14 added).

What about 16 bit? Casting to int reduces max number of elements from 65535 to 32767.

I was thinking that you would say that, but I am not interested in compiling my code on 16-bit computers anymore :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants