gpt4 book ai didi

OS X 版本之间的 C 问题导致未定义的行为

转载 作者:太空宇宙 更新时间:2023-11-04 04:28:21 26 4
gpt4 key购买 nike

编辑

我尝试将 c 初始化为 char c[2] = {'\0'}; 并得到相同的结果,并更改了 strncpy(( char *)c, tmp, 1);strncpy(c, tmp, 1); 也有相同的结果。

还将 strcat(tmp, (char *)c) 更改为 strcat(tmp, c),结果相同。

我还尝试在 c 上使用 memset(),使用空字节,结果相同。它在 10.10.5 上仍按预期执行,但在 10.11.6 上还向字符串添加 ??s? 执行。


我一直在练习一些 C,并决定在工作中编写一个文本解析器。它从文件中读取数据并根据传递给它的不同参数对其执行操作。一种选择是将文件中的所有文本转换为 Pig Latin,这在 OS X 10.10.5 上运行良好,但我在家里展示了我的女朋友 (OS X 10.11.6)(谁帮助我从简单的脚本迁移语言 Python/JS),它开始在每个单词的同一位置的字符之间放置随机字节。这是我的 10.10 机器上的代码和输出。

ma​​in.c

#include "parser.h"

int main(int argc, char *argv[])
{
if(argc < 2){err("Program Requires at Least One Argument.");}
if(argc > 6){err("Too many arguments.");}

int i = 1; // for indexing args (don't care about arg 0)

/* loop over the arguments to find any parameters
we don't look at last argument, as it should be
the file name */
for(i = 1; i < argc - 1; i++) {
if(strcmp(argv[i], "-v") == 0) {v = 1;}
else if(strcmp(argv[i], "-p") == 0) {p = 1;}
else if(strcmp(argv[i], "-c") == 0) {c = 1;}
else {err("Invalid Argument.");}
}

strcpy(flnm, argv[argc-1]);

parse(flnm);

return 0;
}

解析器.c

void parse(const char *fn)
{
FILE *f = fopen(fn, "r");
if(!f) {err("File does not exist.");}

if(v == 1) {
while(xfscanf(f) != 0) {
vwlCnt(buff);
printf("%s\n", buff);
}

printf("File \"%s\" contains %d vowels.\n", fn, v_cnt);
}

if(p == 1) {
FILE *fp = fopen("out.txt", "w+");
if(!fp) {err("File write error.");}

while(xfscanf(f) != 0) {
pigLat(buff);
fputs(buff, fp);
}

fclose(fp);
}

if(c == 1 && v + p != 2) {
if(p == 1) {FILE *fp = fopen("out.txt", "r"); cat(fp);}
else {cat(f);}
}

fclose(f);
}

void pigLat(char *str)
{
int con = 0;
char *c[1];
char *tmp = TEMP(str);

switch(tmp[0]) {
case 'a':
case 'A':
con = 0;
break;
case 'e':
case 'E':
con = 0;
break;
case 'i':
case 'I':
con = 0;
break;
case 'o':
case 'O':
con = 0;
break;
case 'u':
case 'U':
con = 0;
break;
default:
con = 1;
break;
}

if(con == 1) {
printf("%c\n", tmp[0]);
strncpy((char *)c, tmp, 1);

for(int i = 0; i < sizeof(tmp); i++) {
tmp[i] = tmp[i+1];
}

strcat(tmp, (char *)c);
strcat(tmp, "ay ");

strcpy(buff, tmp);
} else {strcat(tmp, "ay "); strcpy(buff, tmp);}

printf("%s\n", tmp);
}

解析器.h

#ifndef __parser_h__
#define __parser_h__

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

int p; // pig latin argument;
int v; // vowel count argument
int c; // cat arg
int v_cnt; // vowel count
char vwl; // vowel char
char flnm[64]; // file name
char buff[32]; // file scan buffer


void err(const char *msg); // error function
char *newTemp(char *str); // creates a 32B temp pointer on heap
int xfscanf(FILE *f); // simple fscan
void cat(FILE *f);
void parse(const char *fn); // file parsing function
void vwlCnt(char *str); // counts vowels in string
void pigLat(char *str); // pig latinize string

#define TEMP(X) newTemp(X)

#endif

输入文件

With less than one week of competition left in the 2016 Rio Olympic Games, empty stadium seats seem to be stealing the spotlight away from some of the world's most decorated athletes.So far 88% of the more than 6 million total tickets have been sold in Rio de Janeiro, Olympic organizers said Wednesday. That's less than the London Games in 2012 and the Beijing Games in 2008, which both sold 96% of their tickets. But it's much better than the 2004 Games in Athens, where just 67% of the event tickets were purchased.Attendance has been strong for marquee events such as gymnastics and sports in which Brazil has been expected to excel, such as soccer and beach volleyball. Still, large swaths of empty seats can be seen at many venues, most notably at the track and field stadium.

输出

ithWay esslay hantay oneay eekway ofay ompetitiioncay eftlay inay hetay 0162ay ioRay Olympicay ames,Gay emptyay tadiumsay eatssay eemsay otay ebay tealingsay hetay potlighttsay awayay romfay omesay ofay hetay orld'sway ostmay ecorateddday athletes.ay oSay arfay 8%8ay ofay hetay oremay hantay 6ay illionmay otaltay icketstay avehay eenbay oldsay inay ioRay eday aneiro,Jay Olympicay organizersay aidsay ednesdayy.Way hat'sTay esslay hantay hetay ondonLay amesGay inay 0122ay anday hetay eijingBay amesGay inay 008,2ay hichway othbay oldsay 6%9ay ofay heirtay ickets.tay utBay it'say uchmay etterbay hantay hetay 0042ay amesGay inay Athens,ay hereway ustjay 7%6ay ofay hetay eventay icketstay ereway urchasedd.pay Attendanceay ashay eenbay trongsay orfay arqueemay eventsay uchsay asay ymnasticcsgay anday portssay inay hichway razilBay ashay eenbay expecteday otay excel,ay uchsay asay occersay anday eachbay olleyballl.vay till,Say argelay wathssay ofay emptyay eatssay ancay ebay eensay atay anymay enues,vay ostmay otablynay atay hetay racktay anday ieldfay tadium.say

例如,在 10.11.6 上,seatssayseats?c?ay 或任何其他随机的几个字节结束。我不确定造成这种情况的原因,当我注意到该行为但无法找到解决方案时,我尝试更改一些代码,我只会让事情变得更糟。

我没有包含整个 parser.c 文件,因为其他函数是不相关的。 xfscanf 简单地执行 fscanf 并在到达 EOF 时返回 0。 *newTemp 从传递给它的字符串中创建一个临时字符串,返回一个指向该字符串新副本的指针,这样元音计数函数就不会破坏原始字符串(我用 X 替换了元音在那个函数中)。

如能提供任何帮助或指导,我们将不胜感激!

最佳答案

问题似乎是 Google 云端硬盘在进行用户在评论中建议的更改后无法正确同步的结果。代码现在可以工作,使用这些更改:


重组 pigLat 以使用 While 循环而不是 For 循环

    int i = 0;
while(tmp[i] != '\0') {
tmp[i] = tmp[i+1];
i++;
}

Ensures that null bytes don't end up being moved to where they should not be moved, and sizeof(tmp) is not going to return the actual size, only 32B as is defined

初始化c

char c[2] = {'\0'};

程序现在按预期运行。

关于OS X 版本之间的 C 问题导致未定义的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39040336/

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