From 5ca72cba5bd09f51c2fd68f89c1c5c84203924a2 Mon Sep 17 00:00:00 2001 From: Jan Engelhardt Date: Fri, 6 Jun 2025 10:53:41 +0200 Subject: [PATCH] Replace sizeof(char) by 1 sizeof(char) is 1 by definition. This simplifies some multiplication statements. https://en.cppreference.com/w/cpp/language/sizeof.html --- Descent3/LoadLevel.cpp | 2 +- cfile/cfile.cpp | 4 ++-- cfile/hogfile.cpp | 2 +- legacy/hogedit/HogEditDoc.cpp | 12 ++++++------ legacy/powerball/powerball.cpp | 18 +++++++++--------- linux/lnxcon_raw.cpp | 8 ++++---- netgames/entropy/EntropyPackets.cpp | 6 ++---- 7 files changed, 25 insertions(+), 27 deletions(-) diff --git a/Descent3/LoadLevel.cpp b/Descent3/LoadLevel.cpp index 16a9e40e..647466fb 100644 --- a/Descent3/LoadLevel.cpp +++ b/Descent3/LoadLevel.cpp @@ -3165,7 +3165,7 @@ void ReadRoomAABBChunk(CFILE *fp, int version) { Rooms[i].bbf_list = mem_rmalloc(Rooms[i].num_bbf_regions); Rooms[i].bbf_list_min_xyz = mem_rmalloc(Rooms[i].num_bbf_regions); Rooms[i].bbf_list_max_xyz = mem_rmalloc(Rooms[i].num_bbf_regions); - Rooms[i].bbf_list_sector = mem_rmalloc(sizeof(char) * Rooms[i].num_bbf_regions); + Rooms[i].bbf_list_sector = mem_rmalloc(Rooms[i].num_bbf_regions); for (j = 0; j < Rooms[i].num_bbf_regions; j++) { Rooms[i].num_bbf[j] = cf_ReadShort(fp); diff --git a/cfile/cfile.cpp b/cfile/cfile.cpp index 16183b16..30aae19b 100644 --- a/cfile/cfile.cpp +++ b/cfile/cfile.cpp @@ -630,7 +630,7 @@ int cfgetc(CFILE *cfp) { if (cfp->position >= cfp->size) return EOF; - fread(ch, sizeof(char), 1, cfp->file); + fread(ch, 1, 1, cfp->file); c = ch[0]; // c = getc( cfp->file ); if (cfeof(cfp)) @@ -644,7 +644,7 @@ int cfgetc(CFILE *cfp) { if (c == 10) // return LF as newline c = '\n'; else if (c == 13) { // check for CR/LF pair - fread(ch, sizeof(char), 1, cfp->file); + fread(ch, 1, 1, cfp->file); int cc = ch[0]; // getc(cfp->file); // if (cc != EOF) { if (!cfeof(cfp)) { diff --git a/cfile/hogfile.cpp b/cfile/hogfile.cpp index b4b9cbad..192409fa 100644 --- a/cfile/hogfile.cpp +++ b/cfile/hogfile.cpp @@ -123,7 +123,7 @@ bool ReadHogHeader(FILE *fp, tHogHeader *header) { } bool ReadHogEntry(FILE *fp, tHogFileEntry *entry) { - if (fread(entry->name, sizeof(char), HOG_FILENAME_LEN, fp) != HOG_FILENAME_LEN) + if (fread(entry->name, 1, HOG_FILENAME_LEN, fp) != HOG_FILENAME_LEN) return false; if (fread(&entry->flags, sizeof(entry->flags), 1, fp) != 1) return false; diff --git a/legacy/hogedit/HogEditDoc.cpp b/legacy/hogedit/HogEditDoc.cpp index dbf1836b..6222d0c6 100644 --- a/legacy/hogedit/HogEditDoc.cpp +++ b/legacy/hogedit/HogEditDoc.cpp @@ -239,7 +239,7 @@ int CHogEditDoc::LoadRib(const char *pathname) { } // Read in the hog filename - if (!fread(Library.filename, sizeof(char), _MAX_PATH, rib_fp)) { + if (!fread(Library.filename, 1, _MAX_PATH, rib_fp)) { fclose(rib_fp); return false; } @@ -379,7 +379,7 @@ int CHogEditDoc::SaveRib(const char *pathname) { } // write out the hog filename - if (!fwrite(Library.filename, sizeof(char), _MAX_PATH, rib_fp)) { + if (!fwrite(Library.filename, 1, _MAX_PATH, rib_fp)) { fclose(rib_fp); return false; } @@ -499,8 +499,8 @@ int CHogEditDoc::UpdatedFileCheck(hog_library_entry *entry) { // Loads a Hog library entry structure bool CHogEditDoc::ReadHogLibEntry(FILE *fp, hog_library_entry *entry) { int res = 0; - res = fread(entry->path, sizeof(char), _MAX_PATH, fp); - res = fread(entry->name, sizeof(char), PSFILENAME_LEN + 1, fp); + res = fread(entry->path, 1, _MAX_PATH, fp); + res = fread(entry->name, 1, PSFILENAME_LEN + 1, fp); res = fread(&entry->flags, sizeof(entry->flags), 1, fp); res = fread(&entry->length, sizeof(entry->length), 1, fp); res = fread(&entry->timestamp, sizeof(entry->timestamp), 1, fp); @@ -516,8 +516,8 @@ bool CHogEditDoc::ReadHogLibEntry(FILE *fp, hog_library_entry *entry) { // Saves a Hog library entry structure bool CHogEditDoc::WriteHogLibEntry(FILE *fp, hog_library_entry *entry) { int res = 0; - res = fwrite(entry->path, sizeof(char), _MAX_PATH, fp); - res = fwrite(entry->name, sizeof(char), PSFILENAME_LEN + 1, fp); + res = fwrite(entry->path, 1, _MAX_PATH, fp); + res = fwrite(entry->name, 1, PSFILENAME_LEN + 1, fp); res = fwrite(&entry->flags, sizeof(entry->flags), 1, fp); res = fwrite(&entry->length, sizeof(entry->length), 1, fp); res = fwrite(&entry->timestamp, sizeof(entry->timestamp), 1, fp); diff --git a/legacy/powerball/powerball.cpp b/legacy/powerball/powerball.cpp index 0e529bda..deb7678d 100644 --- a/legacy/powerball/powerball.cpp +++ b/legacy/powerball/powerball.cpp @@ -918,12 +918,12 @@ void GetGameStartPacket(uint8_t *data) { // who has the powerball int8_t temp; - memcpy(&temp, &data[size], sizeof(char)); - size += sizeof(char); + memcpy(&temp, &data[size], 1); + size += 1; WhoHasPowerBall = temp; - memcpy(&temp, &data[size], sizeof(char)); - size += sizeof(char); + memcpy(&temp, &data[size], 1); + size += 1; NumOfTeams = temp; // Now based on what we have from the server set up our info @@ -947,7 +947,7 @@ void GetGameStartPacket(uint8_t *data) { } void SendGameStartPacket(int pnum) { - int maxsize = sizeof(int) * DLLMAX_TEAMS + 2 * sizeof(char); + int maxsize = sizeof(int) * DLLMAX_TEAMS + 2; int size = 0; uint8_t *data = PBall.StartPacket(maxsize, SPID_NEWPLAYER); @@ -960,13 +960,13 @@ void SendGameStartPacket(int pnum) { int8_t temp; // who has the powerball if anyone temp = WhoHasPowerBall; - memcpy(&data[size], &temp, sizeof(char)); - size += sizeof(char); + memcpy(&data[size], &temp, 1); + size += 1; // number of teams temp = NumOfTeams; - memcpy(&data[size], &temp, sizeof(char)); - size += sizeof(char); + memcpy(&data[size], &temp, 1); + size += 1; // we're done DLLmprintf(0, "Sending Game State to %s\n", PBall.Players[pnum].callsign); diff --git a/linux/lnxcon_raw.cpp b/linux/lnxcon_raw.cpp index f205d01c..522ca50e 100644 --- a/linux/lnxcon_raw.cpp +++ b/linux/lnxcon_raw.cpp @@ -143,14 +143,14 @@ bool con_raw_Create() { Con_raw_rows = 24; // one less, since the bottom window takes up one row // allocate any memory needed for buffers - Con_raw_inp_buf = (char *)malloc(sizeof(char) * (Con_raw_cols + 4)); - Con_raw_read_buf = (char *)malloc(sizeof(char) * (Con_raw_cols + 4)); + Con_raw_inp_buf = static_cast(malloc(Con_raw_cols + 4)); + Con_raw_read_buf = static_cast(malloc(Con_raw_cols + 4)); if (!Con_raw_inp_buf || !Con_raw_read_buf) { // error allocating memory return false; } - memset(Con_raw_inp_buf, 0, sizeof(char) * (Con_raw_cols + 4)); - memset(Con_raw_read_buf, 0, sizeof(char) * (Con_raw_cols + 4)); + memset(Con_raw_inp_buf, 0, Con_raw_cols + 4); + memset(Con_raw_read_buf, 0, Con_raw_cols + 4); Con_raw_last_command[0] = '\0'; Con_raw_inp_pos = 0; diff --git a/netgames/entropy/EntropyPackets.cpp b/netgames/entropy/EntropyPackets.cpp index 9b4f2f11..f9778152 100644 --- a/netgames/entropy/EntropyPackets.cpp +++ b/netgames/entropy/EntropyPackets.cpp @@ -160,9 +160,8 @@ void ReceivePickupVirus(uint8_t *data) { // MTS: only used in this file. void SendRoomInfo(int pnum) { - char *room_info = NULL; int flags, r, i; - room_info = (char *)malloc(sizeof(char) * RoomCount); + auto room_info = static_cast(malloc(RoomCount)); if (!room_info) return; @@ -210,8 +209,7 @@ void SendRoomInfo(int pnum) { void ReceiveRoomInfo(uint8_t *data) { int i, count = 0; int flag; - char *room_info; - room_info = (char *)malloc(sizeof(char) * RoomCount); + auto room_info = static_cast(malloc(RoomCount)); if (!room_info) { FatalError("Out of Memory"); return;