{"id":1803,"date":"2024-05-30T17:40:04","date_gmt":"2024-05-30T17:40:04","guid":{"rendered":"https:\/\/cppdepend.com\/blog\/?p=1803"},"modified":"2024-06-12T10:20:12","modified_gmt":"2024-06-12T10:20:12","slug":"to-developers-who-heavily-rely-on-ai-for-generating-c-code-be-careful","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/to-developers-who-heavily-rely-on-ai-for-generating-c-code-be-careful\/","title":{"rendered":"To developers who heavily rely on AI for generating C++ code. Be careful."},"content":{"rendered":"\n<p>AI-driven code generation has become an increasingly important aspect of software development, leveraging advanced models to write, optimize, and understand code. However, AI is not intended to replace developers but rather to aid them in their tasks. Ultimately, it is up to humans to determine how to utilize AI suggestions. Let&#8217;s consider this query we posed to GPT-4o:<\/p>\n\n\n\n<p><strong>give me a c++ functon to add 2 big integers<\/strong><\/p>\n\n\n\n<p>Here&#8217;s the answer:<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>To add two big integers in C++, you can use the <code>string<\/code> data type to handle arbitrarily large numbers. Here is a function that adds two big integers represented as strings:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;algorithm&gt;\n\n\/\/ Function to add two big integers\nstd::string addBigIntegers(const std::string&amp; num1, const std::string&amp; num2) {\n    std::string result = &quot;&quot;;\n    int carry = 0;\n\n    \/\/ Make sure num1 is the longer string\n    std::string n1 = num1.size() &gt;= num2.size() ? num1 : num2;\n    std::string n2 = num1.size() &lt; num2.size() ? num1 : num2;\n\n    \/\/ Reverse the strings to make addition easier\n    std::reverse(n1.begin(), n1.end());\n    std::reverse(n2.begin(), n2.end());\n\n    \/\/ Add digit by digit\n    for (size_t i = 0; i &lt; n1.size(); ++i) {\n        int digit1 = n1[i] - '0';\n        int digit2 = i &lt; n2.size() ? n2[i] - '0' : 0;\n        int sum = digit1 + digit2 + carry;\n        carry = sum \/ 10;\n        result.push_back((sum % 10) + '0');\n    }\n\n    \/\/ If there is a carry left, add it\n    if (carry) {\n        result.push_back(carry + '0');\n    }\n\n    \/\/ Reverse the result to get the final sum\n    std::reverse(result.begin(), result.end());\n\n    return result;\n}\n\nint main() {\n    std::string num1, num2;\n\n    \/\/ Prompt the user for the first big integer\n    std::cout &lt;&lt; &quot;Enter the first big integer: &quot;;\n    std::cin &gt;&gt; num1;\n\n    \/\/ Prompt the user for the second big integer\n    std::cout &lt;&lt; &quot;Enter the second big integer: &quot;;\n    std::cin &gt;&gt; num2;\n\n    \/\/ Call the addBigIntegers function and store the result\n    std::string sum = addBigIntegers(num1, num2);\n\n    \/\/ Output the result\n    std::cout &lt;&lt; &quot;The sum of &quot; &lt;&lt; num1 &lt;&lt; &quot; and &quot; &lt;&lt; num2 &lt;&lt; &quot; is &quot; &lt;&lt; sum &lt;&lt; &quot;.&quot; &lt;&lt; std::endl;\n\n    return 0;\n}\n<\/pre><\/div>\n\n\n\n<p>The issue with this response lies in the interpretation of the query. In this instance, GPT provides a solution suitable for handling extremely large numbers, which aligns with numerous online resources offering solutions for such cases. However, what if the developer&#8217;s definition of &#8220;big number&#8221; encompasses figures only up to one billion?<\/p>\n\n\n\n<p>In this scenario, we encounter an over-engineered solution that might unnecessarily complicate the code due to the lack of well-defined context. Context plays a significant role when generating code from AI, emphasizing the importance of precisely specifying requirements rather than offering general instructions.<\/p>\n\n\n\n<p>Let&#8217;s explore another example to underscore the significance of context.<\/p>\n\n\n\n<p><strong>give me a c++ code to store and get objects from a file<\/strong><\/p>\n\n\n\n<p> The answer is:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;fstream&gt;\n#include &lt;string&gt;\n\nclass Person {\npublic:\n    std::string name;\n    int age;\n\n    \/\/ Constructor\n    Person() : name(&quot;&quot;), age(0) {}\n    Person(const std::string&amp; n, int a) : name(n), age(a) {}\n\n    \/\/ Method to display the person's information\n    void display() const {\n        std::cout &lt;&lt; &quot;Name: &quot; &lt;&lt; name &lt;&lt; &quot;, Age: &quot; &lt;&lt; age &lt;&lt; std::endl;\n    }\n\n    \/\/ Friend functions for serialization and deserialization\n    friend std::ofstream&amp; operator&lt;&lt;(std::ofstream&amp; ofs, const Person&amp; p);\n    friend std::ifstream&amp; operator&gt;&gt;(std::ifstream&amp; ifs, Person&amp; p);\n};\n\n\/\/ Overload the &lt;&lt; operator for writing a Person object to a file\nstd::ofstream&amp; operator&lt;&lt;(std::ofstream&amp; ofs, const Person&amp; p) {\n    ofs &lt;&lt; p.name &lt;&lt; std::endl;\n    ofs &lt;&lt; p.age &lt;&lt; std::endl;\n    return ofs;\n}\n\n\/\/ Overload the &gt;&gt; operator for reading a Person object from a file\nstd::ifstream&amp; operator&gt;&gt;(std::ifstream&amp; ifs, Person&amp; p) {\n    std::getline(ifs, p.name);\n    ifs &gt;&gt; p.age;\n    ifs.ignore();  \/\/ Ignore the newline character after reading age\n    return ifs;\n}\n<\/pre><\/div>\n\n\n\n<p>And the main function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">int main() {\n    \/\/ Create a Person object\n    Person p1(&quot;John Doe&quot;, 30);\n\n    \/\/ Save the object to a file\n    std::ofstream ofs(&quot;person.txt&quot;);\n    if (ofs.is_open()) {\n        ofs &lt;&lt; p1;\n        ofs.close();\n        std::cout &lt;&lt; &quot;Person saved to file.&quot; &lt;&lt; std::endl;\n    } else {\n        std::cerr &lt;&lt; &quot;Error opening file for writing.&quot; &lt;&lt; std::endl;\n    }\n\n    \/\/ Retrieve the object from the file\n    Person p2;\n    std::ifstream ifs(&quot;person.txt&quot;);\n    if (ifs.is_open()) {\n        ifs &gt;&gt; p2;\n        ifs.close();\n        std::cout &lt;&lt; &quot;Person read from file:&quot; &lt;&lt; std::endl;\n        p2.display();\n    } else {\n        std::cerr &lt;&lt; &quot;Error opening file for reading.&quot; &lt;&lt; std::endl;\n    }\n\n    return 0;\n}\n<\/pre><\/div>\n\n\n\n<p>In this instance, we encounter another issue where the code reinvents the wheel to accomplish the task, resulting in more lines of code and consequently, increased complexity.<\/p>\n\n\n\n<p>However, if we pose the same query with additional specifications:<\/p>\n\n\n\n<p><strong>give me a c++ code to store and get objects from a file by using most know libraries capable to do it<\/strong><\/p>\n\n\n\n<p>The answer in this case use the boost lib:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;fstream&gt;\n#include &lt;vector&gt;\n#include &lt;boost\/archive\/text_oarchive.hpp&gt;\n#include &lt;boost\/archive\/text_iarchive.hpp&gt;<\/pre><\/div>\n\n\n\n<p>To summarize, when generating C++ code using AI, never underestimate the importance of context. It&#8217;s the key to receiving relevant answers. Therefore, pay close attention to your query\u2014it&#8217;s the secret sauce!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AI-driven code generation has become an increasingly important aspect of software development, leveraging advanced models to write, optimize, and understand code. However, AI is not intended to replace developers but rather to aid them in their tasks. Ultimately, it is up to humans to determine how to utilize AI suggestions. Let&#8217;s consider this query we &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/to-developers-who-heavily-rely-on-ai-for-generating-c-code-be-careful\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;To developers who heavily rely on AI for generating C++ code. Be careful.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[663,659,664,665,666,660,71,661,18,662,668,86,667],"class_list":["post-1803","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-ai-assistance-in-coding","tag-ai-driven-code-generation","tag-avoiding-over-engineering-in-code","tag-boost-library-in-c","tag-c-big-integer-addition","tag-c-code-generation","tag-c-programming","tag-context-in-code-generation","tag-cppdepend","tag-precision-in-code-generation","tag-serialization-and-deserialization-in-c","tag-software-development","tag-storing-and-retrieving-objects-in-c"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=1803"}],"version-history":[{"count":3,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1803\/revisions"}],"predecessor-version":[{"id":1806,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1803\/revisions\/1806"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=1803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=1803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=1803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}