gpt4 book ai didi

c++ - strcpy 可以在 Arduino 上编辑内存地址吗?

转载 作者:行者123 更新时间:2023-11-28 04:26:11 26 4
gpt4 key购买 nike

我正在从串行总线解析 char 数组,并将它们的内容复制到全局数组中,以便处理到其他函数中。当我重复使用 strcpy()strtok() 时,我注意到奇怪的行为。使用 Arduino Mega,重复调用 strcpy 是否会破坏内存地址?

这适用于低级仪器,让 Arduino 充当本地微 Controller ,通过串行接收命令。我已经完成了几种不同的初始化全局数组的方法,包括;

//char testDate = "YYmmDD"; //failed
//char testDate[6] = "";
//char testDate[6] = "YYmmDD";
//char testDate[6] = {'Y', 'Y', 'm', 'm', 'D', 'D'};
//char testTime = "HHMMSS"; //failed
//char testTime[6] = "";
//char testTime[6] = "HHMMSS";
//char testTime[6] = {'H', 'H', 'M', 'M', 'S', 'S'};
//char logfile[24] = "BATCH_YYmmDD_HHMMSS$.txt"; // 20 + 4, exact size
//char logfile[24] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};

//char testDate[6] = ""; // Null
//char testTime[6] = ""; // Null
char testDate[6] = {'Y', 'Y', 'm', 'm', 'D', 'D'};
char testTime[6] = {'H', 'H', 'M', 'M', 'S', 'S'}; // is appending?
char logfile[24] = ""; // Null

最简单形式的演示代码;

#include <String.h>

char gva_logfile[24] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
char gva_testDate[6] = {'Y', 'Y', 'm', 'm', 'D', 'D'}; // is appending?
char gva_testTime[6] = {'H', 'H', 'M', 'M', 'S', 'S'};

char lva_testDate[6];

//String y = "BATCH|190117;151442$";
//int lan = y.length(); // should be 20

char x[20] = "BATCH|190117;151442$";
int lan = strlen(x);
//y.toCharArray(x, lan);
//strcpy(x, y);
//y.toCharArray(x, 20);

//strcpy(x, "BATCH|190117;151442$");

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

Serial.print("<");
for(int i=0; i<6; i++){
Serial.print(gva_testDate[i]); // works
//Serial.print(gva_testTime[i]); // works
}
Serial.println(">");
}

void loop() {
// put your main code here, to run repeatedly:
char tele[6] = {' ', ' ', ' ', ' ', ' ', ' '};
while(1){
char flarb[lan];
strcpy(flarb, x);
//Serial.println(flarb);

if(strstr(flarb, "BATCH|")){
char * strtokIndx;
strtokIndx = strtok(x, "|");
//strcpy(tele, strtokIndx); // did nothing?
strtokIndx = strtok(NULL, ";");
strcpy(gva_testDate, strtokIndx); // missing?

Serial.println(gva_testDate); // Not missing
for(int i=0; i<6; i++){
lva_testDate[i] = gva_testDate[i];
}

strtokIndx = strtok(NULL, "$");
strcpy(gva_testTime, strtokIndx); // is fine...

Serial.println(gva_testDate); // MISSING
Serial.println(lva_testDate);

if(strstr(gva_testDate, "YYmmDD")!=NULL || strstr(gva_testTime, "HHMMSS")!=NULL){
//if((gva_testDate == "YYmmDD") || (gva_testTime == "HHMMSS")){
char io[28]; // 16 + 2*6
sprintf(io, "063 ERROR: %s,%s", gva_testDate, gva_testTime);
//logArdData(io);
Serial.print("<");
Serial.print(io);
Serial.println(">");
Serial.flush();
}
//else if((strstr(gva_testDate, "YYmmDD") && strstr(gva_testTime, "HHMMSS"))==NULL){
else if((strstr(gva_testDate, "YYmmDD")==NULL && strstr(gva_testTime, "HHMMSS")==NULL)){
//else if((gva_testDate != "YYmmDD") && (gva_testTime != "HHMMSS")){
char io[26]; // 14 + 2*6

//sendArdData(gva_testDate); // is combinined?
//sendArdData(gva_testTime); // still itself

sprintf(io, "Assigned %s,%s", gva_testDate, gva_testTime); // is combining testDate and testTime, then printing testTime?
Serial.print("<");
Serial.print(io);
Serial.println(">");
//sendArdData(io);
//logArdData(io);
Serial.flush();
}
}
}
}

当我尝试在分配 gva_testTime 后重新打印 gva_testDate 的存储值时,不知何故 gva_testDate 已变为 NULL 而不是 190117。我预计 gva_testDate 会保留其新分配的值,但事实并非如此。

strcpy() 的 C++ 引用页 http://www.cplusplus.com/reference/cstring/strcpy/?kw=strcpystrtok() http://www.cplusplus.com/reference/cstring/strtok/不要提及内存损坏问题/警告,所以我不希望重复调用修改原始字符串 x 或解析的 char 数组 gva_testDate & gva_testTime.

最佳答案

我做了一个测试,确切地发现这里发生了什么。

这是 strtok 的问题,也是您将字符数组提供给所有这些字符串函数而没有空间用于字符串分隔符(即“0”)的事实。

这是我在电脑上写的测试函数

void printAllObjects(int location,char *x, char* strtokIndx, char *gva_testDate,char *gva_testTime)
{
printf("At location %d\n x pointer %d, string %s\n"
" strtokIndx pointer %d, string %s\n "
"gva_testDate pointer %d, string %s\n "
"gva_testTime pointer %d, string %s\n ",location,x,x,strtokIndx,strtokIndx,gva_testDate,gva_testDate,gva_testTime,gva_testTime);
}

然后我将您所有的 arduino 代码复制到我的计算机上,注释掉了所有串行命令,添加了 stdio header ,并在多个位置使用了上述功能。

#include <cstdlib>
#include <String.h>
#include <stdio.h>

using namespace std;

/*
*
*/

char gva_logfile[24] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
char gva_testDate[6] = {'Y', 'Y', 'm', 'm', 'D', 'D'}; // is appending?
char gva_testTime[6] = {'H', 'H', 'M', 'M', 'S', 'S'};

char lva_testDate[6];

//String y = "BATCH|190117;151442$";
//int lan = y.length(); // should be 20

char x[] = "BATCH|190117;151442$";
int lan = strlen(x);
//y.toCharArray(x, lan);
//strcpy(x, y);
//y.toCharArray(x, 20);

//strcpy(x, "BATCH|190117;151442$");


void printAllObjects(int location,char *x, char* strtokIndx, char *gva_testDate,char *gva_testTime)
{
printf("At location %d\n x pointer %d, string %s\n"
" strtokIndx pointer %d, string %s\n "
"gva_testDate pointer %d, string %s\n "
"gva_testTime pointer %d, string %s\n ",location,x,x,strtokIndx,strtokIndx,gva_testDate,gva_testDate,gva_testTime,gva_testTime);
}

void setup() {
// put your setup code here, to run once:
// Serial.begin(9600);
//
// Serial.print("<");
// for(int i=0; i<6; i++){
// Serial.print(gva_testDate[i]); // works
// //Serial.print(gva_testTime[i]); // works
// }
// Serial.println(">");
}



void loop() {
// put your main code here, to run repeatedly:
char tele[6] = {' ', ' ', ' ', ' ', ' ', ' '};
while(1){
char flarb[lan];
strcpy(flarb, x);
//Serial.println(flarb);

if(strstr(flarb, "BATCH|")){
char * strtokIndx;
printAllObjects(1,x,strtokIndx,gva_testDate,gva_testTime);
strtokIndx = strtok(x, "|");
printAllObjects(2,x,strtokIndx,gva_testDate,gva_testTime);
//strcpy(tele, strtokIndx); // did nothing?
strtokIndx = strtok(NULL, ";");
printAllObjects(3,x,strtokIndx,gva_testDate,gva_testTime);
strcpy(gva_testDate, strtokIndx); // missing?
printAllObjects(4,x,strtokIndx,gva_testDate,gva_testTime);

// Serial.println(gva_testDate); // Not missing
for(int i=0; i<6; i++){
lva_testDate[i] = gva_testDate[i];
}

strtokIndx = strtok(NULL, "$");
printAllObjects(5,x,strtokIndx,gva_testDate,gva_testTime);
strcpy(gva_testTime, strtokIndx); // is fine...
printAllObjects(6,x,strtokIndx,gva_testDate,gva_testTime);

// Serial.println(gva_testDate); // MISSING
// Serial.println(lva_testDate);

if(strstr(gva_testDate, "YYmmDD")!=NULL || strstr(gva_testTime, "HHMMSS")!=NULL){
//if((gva_testDate == "YYmmDD") || (gva_testTime == "HHMMSS")){
char io[28]; // 16 + 2*6
sprintf(io, "063 ERROR: %s,%s", gva_testDate, gva_testTime);
//logArdData(io);
// Serial.print("<");
// Serial.print(io);
// Serial.println(">");
// Serial.flush();
}
//else if((strstr(gva_testDate, "YYmmDD") && strstr(gva_testTime, "HHMMSS"))==NULL){
else if((strstr(gva_testDate, "YYmmDD")==NULL && strstr(gva_testTime, "HHMMSS")==NULL)){
//else if((gva_testDate != "YYmmDD") && (gva_testTime != "HHMMSS")){
char io[26]; // 14 + 2*6

//sendArdData(gva_testDate); // is combinined?
//sendArdData(gva_testTime); // still itself

sprintf(io, "Assigned %s,%s", gva_testDate, gva_testTime); // is combining testDate and testTime, then printing testTime?
// Serial.print("<");
// Serial.print(io);
// Serial.println(">");
//sendArdData(io);
//logArdData(io);
// Serial.flush();
}
}
}
}
int main(int argc, char** argv) {
setup();
printf("Entering loop\n");
loop();
return 0;
}

这是我得到的结果

Entering loop
At location 1
x pointer 199946368, string BATCH|190117;151442$
strtokIndx pointer 0, string (null)
gva_testDate pointer 199946344, string YYmmDDHHMMSS
gva_testTime pointer 199946350, string HHMMSS
At location 2
x pointer 199946368, string BATCH
strtokIndx pointer 199946368, string BATCH
gva_testDate pointer 199946344, string YYmmDDHHMMSS
gva_testTime pointer 199946350, string HHMMSS
At location 3
x pointer 199946368, string BATCH
strtokIndx pointer 199946374, string 190117
gva_testDate pointer 199946344, string YYmmDDHHMMSS
gva_testTime pointer 199946350, string HHMMSS
At location 4
x pointer 199946368, string BATCH
strtokIndx pointer 199946374, string 190117
gva_testDate pointer 199946344, string 190117
gva_testTime pointer 199946350, string
At location 5
x pointer 199946368, string BATCH
strtokIndx pointer 199946381, string 151442
gva_testDate pointer 199946344, string 190117
gva_testTime pointer 199946350, string
At location 6
x pointer 199946368, string BATCH
strtokIndx pointer 199946381, string 151442
gva_testDate pointer 199946344, string 190117151442
gva_testTime pointer 199946350, string 151442

根据这些数据,这是我的结论。

strtok 函数修改原始字符串并将搜索字符替换为 null,然后将字符串复制到新索引并返回索引。

strcpy 没有对您的代码做任何事情,您编写缓冲区值的方式似乎 strcpy 正在做令人讨厌的事情,但它只是在做它应该做的事情。复制字符串直到找到空值。在大多数情况下,它会给您带来很多麻烦,但在这种特定情况下不会。

从结果中可以看出,gva_testDate 和 gva_testTime 的指针差异为 6,因此当填充 gva_testTime 时,如果将 gva_testDate 用作字符串,则仅在 12 个字符的末尾看到 null。

此外,lva_testDate 应该是您的缓冲区,您需要给它一个值作为开始,否则它会变成一个空缓冲区,并可能最终根据代码编译的内容修改不同的东西。

为了解决这个问题,我初始化了你所有的变量,输出打印在下面。

char gva_testDate[] = "YYmmDD"; // is appending?
char gva_testTime[] = "HHMMSS";

char lva_testDate[20];

//String y = "BATCH|190117;151442$";
//int lan = y.length(); // should be 20

char x[] = "BATCH|190117;151442$";
int lan = strlen(x);

输出

Entering loop
At location 1
x pointer 107077760, string BATCH|190117;151442$
strtokIndx pointer 0, string (null)
gva_testDate pointer 107077736, string YYmmDD
gva_testTime pointer 107077743, string HHMMSS
At location 2
x pointer 107077760, string BATCH
strtokIndx pointer 107077760, string BATCH
gva_testDate pointer 107077736, string YYmmDD
gva_testTime pointer 107077743, string HHMMSS
At location 3
x pointer 107077760, string BATCH
strtokIndx pointer 107077766, string 190117
gva_testDate pointer 107077736, string YYmmDD
gva_testTime pointer 107077743, string HHMMSS
At location 4
x pointer 107077760, string BATCH
strtokIndx pointer 107077766, string 190117
gva_testDate pointer 107077736, string 190117
gva_testTime pointer 107077743, string HHMMSS
At location 5
x pointer 107077760, string BATCH
strtokIndx pointer 107077773, string 151442
gva_testDate pointer 107077736, string 190117
gva_testTime pointer 107077743, string HHMMSS
At location 6
x pointer 107077760, string BATCH
strtokIndx pointer 107077773, string 151442
gva_testDate pointer 107077736, string 190117
gva_testTime pointer 107077743, string 151442

希望对您有所帮助。

关于c++ - strcpy 可以在 Arduino 上编辑内存地址吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54256634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com