Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
(void * source, size_t bytes)

I might name bytes as byte_size or byte_count. I generally use plurals to denote collections, and I think that including either size or count in the name clarifies the purpose.

for(i = 0; i != bytes; i++)

As 200_success already pointed out, this could be better written as

for ( i = 0; i < bytes; i++ )

The main reason is that using != is fragile. If something happens that changes i for some other reason than currently planned, you can skip past the single loop ending value. Once you do that, it will run until it loops entirely around or crashes. The < restricts the valid values more. Note: this is not a bug in the current program; nor is it likely to impact this function. The issue is that there are other circumstances where this pattern is much riskier.

 m <<= 8;

Assuming that you take the advice in the other answers, I still think that there's something worth noting here. You are making an assumption here about the size of an unsigned char (that it's one byte or eight bits). This assumption is generally true but not always. This assumption is generally true but not always.

One possible fix is to use a uint8_t, but that type may not exist in your C. A second solution is to change the value based on your platform's byte size:

#include <limits.h>
const size_t CHAR_WIDTH = sizeof(unsigned char) * CHAR_BIT;
// skip irrelevant code
 m <<= CHAR_WIDTH;

That should work on any platform regardless of platform-specific byte sizes.

(void * source, size_t bytes)

I might name bytes as byte_size or byte_count. I generally use plurals to denote collections, and I think that including either size or count in the name clarifies the purpose.

for(i = 0; i != bytes; i++)

As 200_success already pointed out, this could be better written as

for ( i = 0; i < bytes; i++ )

The main reason is that using != is fragile. If something happens that changes i for some other reason than currently planned, you can skip past the single loop ending value. Once you do that, it will run until it loops entirely around or crashes. The < restricts the valid values more. Note: this is not a bug in the current program; nor is it likely to impact this function. The issue is that there are other circumstances where this pattern is much riskier.

 m <<= 8;

Assuming that you take the advice in the other answers, I still think that there's something worth noting here. You are making an assumption here about the size of an unsigned char (that it's one byte or eight bits). This assumption is generally true but not always.

One possible fix is to use a uint8_t, but that type may not exist in your C. A second solution is to change the value based on your platform's byte size:

#include <limits.h>
const size_t CHAR_WIDTH = sizeof(unsigned char) * CHAR_BIT;
// skip irrelevant code
 m <<= CHAR_WIDTH;

That should work on any platform regardless of platform-specific byte sizes.

(void * source, size_t bytes)

I might name bytes as byte_size or byte_count. I generally use plurals to denote collections, and I think that including either size or count in the name clarifies the purpose.

for(i = 0; i != bytes; i++)

As 200_success already pointed out, this could be better written as

for ( i = 0; i < bytes; i++ )

The main reason is that using != is fragile. If something happens that changes i for some other reason than currently planned, you can skip past the single loop ending value. Once you do that, it will run until it loops entirely around or crashes. The < restricts the valid values more. Note: this is not a bug in the current program; nor is it likely to impact this function. The issue is that there are other circumstances where this pattern is much riskier.

 m <<= 8;

Assuming that you take the advice in the other answers, I still think that there's something worth noting here. You are making an assumption here about the size of an unsigned char (that it's one byte or eight bits). This assumption is generally true but not always.

One possible fix is to use a uint8_t, but that type may not exist in your C. A second solution is to change the value based on your platform's byte size:

#include <limits.h>
const size_t CHAR_WIDTH = sizeof(unsigned char) * CHAR_BIT;
// skip irrelevant code
 m <<= CHAR_WIDTH;

That should work on any platform regardless of platform-specific byte sizes.

Remove C99ism.
Source Link
Brythan
  • 7k
  • 3
  • 21
  • 37
(void * source, size_t bytes)

I might name bytes as byte_size or byte_count. I generally use plurals to denote collections, and I think that including either size or count in the name clarifies the purpose.

for(i = 0; i != bytes; i++)

As 200_success already pointed out, this could be better written as

for ( size_t i = 0; i < bytes; i++ )

The main reason is that using != is fragile. If something happens that changes i for some other reason than currently planned, you can skip past the single loop ending value. Once you do that, it will run until it loops entirely around or crashes. The < restricts the valid values more. Note: this is not a bug in the current program; nor is it likely to impact this function. The issue is that there are other circumstances where this pattern is much riskier.

 m <<= 8;

Assuming that you take the advice in the other answers, I still think that there's something worth noting here. You are making an assumption here about the size of an unsigned char (that it's one byte or eight bits). This assumption is generally true but not always.

One possible fix is to use a uint8_t, but that type may not exist in your C. A second solution is to change the value based on your platform's byte size:

#include <limits.h>
const size_t CHAR_WIDTH = sizeof(unsigned char) * CHAR_BIT;
// skip irrelevant code
 m <<= CHAR_WIDTH;

That should work on any platform regardless of platform-specific byte sizes.

(void * source, size_t bytes)

I might name bytes as byte_size or byte_count. I generally use plurals to denote collections, and I think that including either size or count in the name clarifies the purpose.

for(i = 0; i != bytes; i++)

As 200_success already pointed out, this could be better written as

for ( size_t i = 0; i < bytes; i++ )

The main reason is that using != is fragile. If something happens that changes i for some other reason than currently planned, you can skip past the single loop ending value. Once you do that, it will run until it loops entirely around or crashes. The < restricts the valid values more. Note: this is not a bug in the current program; nor is it likely to impact this function. The issue is that there are other circumstances where this pattern is much riskier.

 m <<= 8;

Assuming that you take the advice in the other answers, I still think that there's something worth noting here. You are making an assumption here about the size of an unsigned char (that it's one byte or eight bits). This assumption is generally true but not always.

One possible fix is to use a uint8_t, but that type may not exist in your C. A second solution is to change the value based on your platform's byte size:

#include <limits.h>
const size_t CHAR_WIDTH = sizeof(unsigned char) * CHAR_BIT;
// skip irrelevant code
 m <<= CHAR_WIDTH;

That should work on any platform regardless of platform-specific byte sizes.

(void * source, size_t bytes)

I might name bytes as byte_size or byte_count. I generally use plurals to denote collections, and I think that including either size or count in the name clarifies the purpose.

for(i = 0; i != bytes; i++)

As 200_success already pointed out, this could be better written as

for ( i = 0; i < bytes; i++ )

The main reason is that using != is fragile. If something happens that changes i for some other reason than currently planned, you can skip past the single loop ending value. Once you do that, it will run until it loops entirely around or crashes. The < restricts the valid values more. Note: this is not a bug in the current program; nor is it likely to impact this function. The issue is that there are other circumstances where this pattern is much riskier.

 m <<= 8;

Assuming that you take the advice in the other answers, I still think that there's something worth noting here. You are making an assumption here about the size of an unsigned char (that it's one byte or eight bits). This assumption is generally true but not always.

One possible fix is to use a uint8_t, but that type may not exist in your C. A second solution is to change the value based on your platform's byte size:

#include <limits.h>
const size_t CHAR_WIDTH = sizeof(unsigned char) * CHAR_BIT;
// skip irrelevant code
 m <<= CHAR_WIDTH;

That should work on any platform regardless of platform-specific byte sizes.

Source Link
Brythan
  • 7k
  • 3
  • 21
  • 37
(void * source, size_t bytes)

I might name bytes as byte_size or byte_count. I generally use plurals to denote collections, and I think that including either size or count in the name clarifies the purpose.

for(i = 0; i != bytes; i++)

As 200_success already pointed out, this could be better written as

for ( size_t i = 0; i < bytes; i++ )

The main reason is that using != is fragile. If something happens that changes i for some other reason than currently planned, you can skip past the single loop ending value. Once you do that, it will run until it loops entirely around or crashes. The < restricts the valid values more. Note: this is not a bug in the current program; nor is it likely to impact this function. The issue is that there are other circumstances where this pattern is much riskier.

 m <<= 8;

Assuming that you take the advice in the other answers, I still think that there's something worth noting here. You are making an assumption here about the size of an unsigned char (that it's one byte or eight bits). This assumption is generally true but not always.

One possible fix is to use a uint8_t, but that type may not exist in your C. A second solution is to change the value based on your platform's byte size:

#include <limits.h>
const size_t CHAR_WIDTH = sizeof(unsigned char) * CHAR_BIT;
// skip irrelevant code
 m <<= CHAR_WIDTH;

That should work on any platform regardless of platform-specific byte sizes.

lang-c

AltStyle によって変換されたページ (->オリジナル) /