ariesboy571
Well-known member
I have come across an odd problem, and I'm not sure what's going on.
If I load and run this code on a Teensy 4.1:
I get this response:
The last four numbers in list B remain (generally) the same; they change if I power-cycle the Teensy.
On a stock Arduino Uno, the code generates nearly the same output:
Except that I'm now reduced to two useful digits.
The same code, modified for Linux, and run on Ubuntu 24:
Running on the command line (g++) generates the output I'd expect:
What on earth am I doing wrong?
If I load and run this code on a Teensy 4.1:
C++:
int8_t* testMe(uint8_t* myList) {
//uint8_t* numList = new uint8_t[sizeof(myList)];
uint8_t* numList = (uint8_t*)malloc(sizeof(myList)); // effectively the same as the above statement
memcpy(numList, myList, sizeof(myList));
return numList;
}
void setup() {
Serial.begin(19200);
while (!Serial && millis() < 5000) {
// Wait for Serial
}
Serial.print("Starting...\r\n");
uint8_t mp[8] = {50, 52, 54, 56, 58, 60, 62, 64};
Serial.print("The number list A: ");
for(int a=0; a<8; a++) {
int xxx = static_cast<int>(mp[a]);
Serial.print(xxx);
Serial.print(", ");
}
Serial.println();
Serial.print("The number list B: ");
uint8_t* myTest = testMe(mp);
for(int a=0; a<8; a++) {
int yyy = static_cast<int>(myTest[a]);
Serial.print(yyy);
Serial.print(", ");
}
Serial.println();
free(myTest);
}
void loop() {
// not today, Satan...
}
I get this response:
Code:
00:02:03.036 -> Starting...
00:02:03.036 -> The number list A: 50, 52, 54, 56, 58, 60, 62, 64,
00:02:03.036 -> The number list B: 50, 52, 54, 56, 139, 0, 207, 205,
The last four numbers in list B remain (generally) the same; they change if I power-cycle the Teensy.
On a stock Arduino Uno, the code generates nearly the same output:
Code:
23:52:25.301 -> Starting...
23:52:26.246 -> The number list A: 97, 98, 99, 3, 4, 5, 6, 9,
23:52:26.278 -> The number list B: 97, 98, 2, 0, 0, 1, 163, 89,
Except that I'm now reduced to two useful digits.
The same code, modified for Linux, and run on Ubuntu 24:
C++:
#include <stdio.h>
#include <cstring>
#include <cstdlib>
unsigned char* testMe(unsigned char* myList) {
//unsigned char* numList = new unsigned char[sizeof(myList)];
unsigned char* numList = (unsigned char*)malloc(sizeof(myList));
memcpy(numList, myList, sizeof(myList));
return numList;
}
int main() {
// put your setup code here, to run once:
unsigned char mp[8] = {0, 1, 2, 3, 4, 5, 6, 9};
printf("Number List A: ");
for(int a=0; a<8; a++) {
printf("%x ", mp[a]);
}
printf("\r\n");
unsigned char* myTest = testMe(mp);
printf("Number List B: ");
for(int a=0; a<8; a++) {
printf("%x ", myTest[a]);
}
return 0;
}
Running on the command line (g++) generates the output I'd expect:
Code:
Number List A: 0 1 2 3 4 5 6 9
Number List B: 0 1 2 3 4 5 6 9
What on earth am I doing wrong?