chatGPT - C 编程 Linux Windows 跨平台 - 代码审查请求
我一直在尝试使用 chatGPT 提示在 C 中进行编程。
使用增量提示可以让 chatGPT 3.5 做比预期更多的事情。
我想知道代码质量,并且希望获得有关提示和代码的反馈。
功能目标
- 验证路径是否有效并解析路径组件
- MacOS Linux Windows 跨平台兼容性
执行
线程安全代码
静态路径
较大路径字符串的动态路径分配
Linux / MacOs 的标准做法是动态分配(4095 / 1024 图表路径限制),而 Windows(260 路径限制)是静态分配。
迅速的
我逐步构建查询,否则 chatGPT 往往会忘记以前的请求更改。
最后,这看起来像是某种原始形式的元编码,可以强制 LLM 生成所需的代码。
_query LLM perplexity.ai Modify code example Portable Path Parsing and Validation Code in C use thread safe function use function pointer strtok_s() for Windows strtok_r() for linux allocate INITIAL_BUFFER_SIZE 60 char static path_copy declare pointer pfinal_path_copy initialize to path_copy when necessary dynamically allocate final_path_copy pfinal_path_copy = final_path_copy use single strcpy(pfinal_path_copy, path) function
代码一次成形一个功能以匹配所需的风格
modify example replace 60 by equate
一些提示仍然粘着而没有重复。早期添加了“no WSL cygwin”以提示输入 Windows 操作系统特定代码,然后被删除。
write code example parse path in portable way linux windows MacOS no WSL cygwin
代码
生成的代码会在第一次编译并运行。
#include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef _WIN32 #include <io.h> // For _access() on Windows #include <windows.h> // For Windows API #else #include <unistd.h> // For access() on Unix-like systems (Linux, macOS) #include <sys/stat.h> #endif // Define a constant for the initial buffer size #define INITIAL_BUFFER_SIZE 60 // Function to check if a path is valid int path_validate(const char *path) { #ifdef _WIN32 return _access(path, 0) == 0; // 0 means the file exists #else struct stat buffer; return (stat(path, &buffer) == 0); // Returns 1 if valid, 0 if invalid #endif } // Type definition for tokenization function pointer typedef char* (*tokenizer_func)(char*, const char*, char**); // Function to parse the path into components void parse_path(const char *path) { char path_copy[INITIAL_BUFFER_SIZE]; // Static buffer for initial use char *final_path_copy = NULL; // Pointer for dynamic allocation char *pfinal_path_copy = path_copy; // Pointer to point to either path_copy or final_path_copy // Check if the input path length exceeds INITIAL_BUFFER_SIZE if (strlen(path) + 1 > INITIAL_BUFFER_SIZE) { final_path_copy = malloc(strlen(path) + 1); // Dynamically allocate memory if (final_path_copy == NULL) { fprintf(stderr, "Memory allocation failed.\n"); exit(EXIT_FAILURE); } pfinal_path_copy = final_path_copy; // Point to dynamically allocated memory } // Use a single strcpy function to copy the path into the appropriate buffer strcpy(pfinal_path_copy, path); // Copy full path into the selected buffer // Determine the separator based on the platform and assign the tokenizer function const char *separator; tokenizer_func tokenizer; char *saveptr; #ifdef _WIN32 separator = "\"; tokenizer = strtok_s; // Use strtok_s for Windows #else separator = "/"; tokenizer = strtok_r; // Use strtok_r for Linux #endif // Tokenize using the assigned tokenizer function char *token = tokenizer(pfinal_path_copy, separator, &saveptr); while (token != NULL) { printf("Path component: %s\n", token); token = tokenizer(NULL, separator, &saveptr); // Continue tokenizing } free(final_path_copy); // Free dynamically allocated memory after use (if allocated) } int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Usage: %s <path>\n", argv[0]); return EXIT_FAILURE; } const char *path = argv[1]; // Validate the path using the new function name if (path_validate(path)) { printf("The path '%s' is valid.\n", path); printf("Parsing components:\n"); parse_path(path); } else { printf("The path '%s' is invalid.\n", path); } return EXIT_SUCCESS; } ## Unit test Was created but not included here ## Performance The entire exercise took 2:30 hour. I have not tested the code on Windows or MacOS. I'd like some feedback on code quality.
以上是chatGPT - C 编程 Linux Windows 跨平台 - 代码审查请求的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

C#和C 的历史与演变各有特色,未来前景也不同。1.C 由BjarneStroustrup在1983年发明,旨在将面向对象编程引入C语言,其演变历程包括多次标准化,如C 11引入auto关键字和lambda表达式,C 20引入概念和协程,未来将专注于性能和系统级编程。2.C#由微软在2000年发布,结合C 和Java的优点,其演变注重简洁性和生产力,如C#2.0引入泛型,C#5.0引入异步编程,未来将专注于开发者的生产力和云计算。

C#和C 的学习曲线和开发者体验有显着差异。 1)C#的学习曲线较平缓,适合快速开发和企业级应用。 2)C 的学习曲线较陡峭,适用于高性能和低级控制的场景。

静态分析在C 中的应用主要包括发现内存管理问题、检查代码逻辑错误和提高代码安全性。1)静态分析可以识别内存泄漏、双重释放和未初始化指针等问题。2)它能检测未使用变量、死代码和逻辑矛盾。3)静态分析工具如Coverity能发现缓冲区溢出、整数溢出和不安全API调用,提升代码安全性。

C 通过第三方库(如TinyXML、Pugixml、Xerces-C )与XML交互。1)使用库解析XML文件,将其转换为C 可处理的数据结构。2)生成XML时,将C 数据结构转换为XML格式。3)在实际应用中,XML常用于配置文件和数据交换,提升开发效率。

使用C 中的chrono库可以让你更加精确地控制时间和时间间隔,让我们来探讨一下这个库的魅力所在吧。C 的chrono库是标准库的一部分,它提供了一种现代化的方式来处理时间和时间间隔。对于那些曾经饱受time.h和ctime折磨的程序员来说,chrono无疑是一个福音。它不仅提高了代码的可读性和可维护性,还提供了更高的精度和灵活性。让我们从基础开始,chrono库主要包括以下几个关键组件:std::chrono::system_clock:表示系统时钟,用于获取当前时间。std::chron

C 的未来将专注于并行计算、安全性、模块化和AI/机器学习领域:1)并行计算将通过协程等特性得到增强;2)安全性将通过更严格的类型检查和内存管理机制提升;3)模块化将简化代码组织和编译;4)AI和机器学习将促使C 适应新需求,如数值计算和GPU编程支持。

1)c relevantduetoItsAverity and效率和效果临界。2)theLanguageIsconTinuellyUped,withc 20introducingFeaturesFeaturesLikeTuresLikeSlikeModeLeslikeMeSandIntIneStoImproutiMimproutimprouteverusabilityandperformance.3)

C#使用自动垃圾回收机制,而C 采用手动内存管理。1.C#的垃圾回收器自动管理内存,减少内存泄漏风险,但可能导致性能下降。2.C 提供灵活的内存控制,适合需要精细管理的应用,但需谨慎处理以避免内存泄漏。
