When you use non-standard data types like enum, bool, double, long double, it might become a problem if you are sending data to another platform, e.g. sending from x86 to ARM, or even when sending from Windows to Linux. For example, long double is 8 bytes in MSVC but 16 bytes in GCC. These size differences will result in errors when casting the received bytes to data. Always know non-standard data type lengths of the platform that you are communicating with.
C++ and MATLAB Simulink tips for HWIL simulation software engineers
Thursday, January 12, 2023
Wednesday, January 11, 2023
Monday, January 9, 2023
C: char pointer vs array
In C, the difference between a char pointer and char array becomes clear when you want to do assignment:
#include<stdio.h>
#include<string.h> //for strcpy
int main() {
char s[][10] = {"aaa", "bbb", "ccc"};
//char* s[] = {"aaa", "bbb", "ccc"}; //same as above
char* t1 = s[1];
char t2[10]; strcpy(t2, s[1]);
//t2 = s[1];// error: assignment to expression with array type
printf("s[1] = %s\n", s[1]);
printf("t1 = %s\n", t1);
printf("t2 = %s\n", t2);
return 0;
}
s[1] is a char pointer. You can assign a char pointer to another char pointer but you cannot assign a char array to char pointer, you have to use strcpy.
Another interesting difference:
char a[] = "string1";
char *p1 = a;
char *p2 = "string2";
a[0] = 'z';
printf("a[0] = %c\n", a[0]);
p1[0] = 'k'; //works fine, changes a[0]
printf("a[0] = %c\n", a[0]);
p2[0] = 'm'; //results in segmentation fault
printf("p2[0] = %c\n", p2[0]);
Friday, December 9, 2022
C++: Using constant for array size
In C, you can use a constant when declaring an array:
const n = 5; int a[n];
In C++, it would result in compile error "constant expression required". You have to declare it using constexpr:
constexpr int n = 5; int a[n];
However, if you for example want to use the size of a vector to declare an array, you can't use the stack, you have to use the heap:
int* a = new int[myVector.size()]; ... delete[] a;
Tuesday, November 1, 2022
Formatting disk from USB
I recently needed to wipe a laptop's disk. I used Rufus and Puppy Linux (ISO size was 409MB) to create a bootable USB drive. After booting into Puppy Linux, I used the following commands:
- lsblk to see disk partitions and their sizes
- lsblk -f to see partition file systems
- sudo mkfs -t ntfs /dev/<partition>
Formatting a disk (initializing with zeros) of 500GB can take 3 hours.
Wednesday, October 26, 2022
Formatting C++ code in Visual Studio
C++ code is usually formatted as follows:
I like the curly braces to be on the same line so that I can see more code in less lines:
To define this format in Visual Studio 2022, go to Tools - Options - Text Editor - C/C++ - Code Style - Formatting - New Lines and uncheck the boxes at the bottom:
If you also want the code snippet for if to behave similarly, go to Tools - Code Snippet Manager - Visual C++, copy the location of the if snippet file, open the file in a text editor with admin privileges and edit and save it:
if (condition)
{
}
else {
}
I like the curly braces to be on the same line so that I can see more code in less lines:
if (condition) {
} else {
}
To define this format in Visual Studio 2022, go to Tools - Options - Text Editor - C/C++ - Code Style - Formatting - New Lines and uncheck the boxes at the bottom:
After this change, you can update the format of an existing file with Edit - Advanced - Format Document.
Tuesday, October 25, 2022
Initial values in Java and C++
In Java a variable of type int or double has the initial value of zero. But in C++, they can get random values, as you can see in the example below. Note that in C++, the easiest way to set all fields to zero is to use memset.
#include <iostream>
typedef struct {
int n;
double values[12];
} DATA;
int main() {
DATA data;
std::cout << data.n << ", " << data.values[3] << std::endl;
memset(&data, 0, sizeof(data)); //set all fields to zero
std::cout << data.n << ", " << data.values[3] << std::endl;
std::cout << "Press enter..." << std::endl;
std::cin.get();
}
Subscribe to:
Posts (Atom)