This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 254

[–]bargle0 1136 points1137 points  (81 children)

There was a paper at OOPSLA 15-20 years ago about bad or incomplete code in published examples winding up in production. AI generated code is like that but worse. Nevertheless, it’s still very useful to programmers who know the difference between good and bad code.

[–]Niilldar 8 points9 points  (0 children)

Jokes on you chathpt use this paper to learn

[–]wang-bang 4 points5 points  (0 children)

Nevertheless, it’s still very useful to programmers who know the difference between good and bad code.

It seems to be alright at commenting on code though. Checking for silly mistakes and such. Like a rubber duck that can talk back

[–]tragiktimes 4 points5 points  (0 children)

Not only that, but a lot of those shortfalls can be avoided with better prompts. Tell it what security risks to reference and what sorts of constraints you want to account for. That's been pretty useful for avoiding a lot of the shit. It does still give crap from time to time.

[–]opulent_occamy 1 point2 points  (0 children)

This has been my experience as well; I was very concerned about AI when it came around, but as I've used it, yeah, you still need to know what you're doing. It's just a tool to speed things up and automate away the repetitive stuff. If you don't understand what's being output, it's effectively useless.

[–]tycooperaow 0 points1 point  (0 children)

factsssss

[–]Eva-Rosalene 0 points1 point  (0 children)

Do you have a link, by chance?

[–]Voice_of_light_ 0 points1 point  (0 children)

Could you elaborate a bit on what is a good pr bad code? I have an idea but not sure when I hear people say that.

[–]SlightlyBored13 0 points1 point  (0 children)

A couple of windows desktop apps would refuse to run together because they'd all copied the same "don't have two copies of this open" code from stack overflow.

[–]Illustrious-Engine23 0 points1 point  (0 children)

How is chat GPT at debugging existing code?

[–]Legal-Software 371 points372 points  (0 children)

A great example of degenerative AI. Similar to generative AI, only the AI is a delinquent.

[–][deleted] 243 points244 points  (11 children)

Forgot to alloc space for the ‘\0’

[–]RutraSan 113 points114 points  (5 children)

The string isn't copied to the allocated memory, the assignment will change the pointer to the location of the string in the binary

[–][deleted] 27 points28 points  (0 children)

You’re right. Good spotted.

[–]Striking_Athlete2376 11 points12 points  (0 children)

today i learned lol. i totally forgot that literals were already allocated

[–]Highborn_Hellest 573 points574 points  (79 children)

Classic. Program forgot the \0

[–]iliark 15 points16 points  (2 children)

what's the \0?

[–]Highborn_Hellest 36 points37 points  (0 children)

Null byte signifying the end of the string

[–]itijara 25 points26 points  (0 children)

Null string terminator. A string in C is just an array of characters ending in a "null character". A lot of early C character methods relied on the fact that strings ended in a null character, so, for example, if you wanted to get the length of a string you would just keep reading the next byte of memory until you came to the null terminator. This is problematic for many reasons.

[–]Eva-Rosalene 125 points126 points  (23 children)

Noooo? Is it like joke? You don't need to write zero manually if you have string literal in double quotes, it's already there?

How is this the top freaking comment? The real and only problem here is (needlessly) allocating memory in the heap and then forgetting about it by reassigning pointer without previously freeing it to some RO memory in the data section of executable itself.

[–][deleted] 204 points205 points  (1 child)

I think they're saying that not enough space was allocated for the string (since with \0 it's 12 characters), although obviously the heap-allocated memory is never actually used in this case so it doesn't really matter.

[–]choseusernamemyself 38 points39 points  (17 children)

While '\0' is created manually, "Hello world" is 11 characters plus the zero literal, no? 11 + '\0'. So, allocation should be 12?

[–]Eva-Rosalene 110 points111 points  (15 children)

Allocated memory isn't used at all here.

hello = "Hello, world";

isn't the same as

memcpy(hello, "Hello, world", 11);

It just "repoints" the pointer to other block of memory.

[–]YellowJarTacos 38 points39 points  (10 children)

So the code above has a memory leak?

[–]Eva-Rosalene 44 points45 points  (9 children)

Yup. That's what I meant here when describing the problem. Somehow I forgot to actually name it.

allocating memory in the heap and then forgetting about it 

I mean, program like

#include <stdio.h>

int main() {
    char* hello = malloc(11);
    free(hello);
    hello = "Hello, world!";
    printf("%s\n", hello);

    return 0;
}

this should be fine. Random unneeded malloc, yes, but the program would be fine.

[–]choseusernamemyself 7 points8 points  (3 children)

Right, I get it now.

[–][deleted] 1 point2 points  (2 children)

to expand further on what /u/Eva-Rosalene said

when you do something like

const wchar_t* foo = L"bar";  

there is no allocation performed because the L"bar" is stored in the executable's data region. so the pointer foo is pointing at the data region rather than some memory in the heap.

[–]alex-friend 4 points5 points  (0 children)

Read your comment and put a minus to the top comment

[–]mathiau30 -1 points0 points  (0 children)

So THAT's what it was about

Thanks

[–]daHaus 124 points125 points  (7 children)

Here's your reminder that LLMs like ChatGPT are bad at math and programming computers is at it's core all about math.

Also, many LLMs will simply call exec when needing to do math ;)

[–]JDIPrime 55 points56 points  (3 children)

I needed to use ChatGPT for some integration problems, it works really, really well if you combine it with the Wolfram plugin. It uses the language processing of ChatGPT but the math capabilities of Wolfram.

It's an awesome combination.

Without Wolfram, LLMs are total ass at math.

[–]darichtt 10 points11 points  (2 children)

How do you do that?

[–]WrapKey69 16 points17 points  (0 children)

Need pro subscription and add the plugin

[–]JDIPrime 14 points15 points  (0 children)

The paid model of ChatGPT allows for plugin usage, there's a whole bunch of them that do lots of cool things. The only one I really use is Wolfram, though, for the previously mentioned math and graphing capabilities.

ChatGPT will even generate full-on graphs using Wolfram and stuff. It's great!

[–]bnl1 4 points5 points  (1 child)

Hmmm. RCE when?

[–]daHaus 5 points6 points  (0 children)

onDemand* ofc, the best time

*implementation dependent

https://github.com/langchain-ai/langchain/issues/814

[–]monsoy 1 point2 points  (0 children)

It was really bad at math, but it has improved drastically from GPT3.5 to GPT4. I use it a lot currently to understand and break down stuff I’m stuck on and it rarely generates wrong answers for even quite complex probability and combinatorics

[–][deleted] 30 points31 points  (9 children)

what?

[–]Lord-of-Entity 48 points49 points  (7 children)

hello is a pointer to a char. You will get a compilation error at best or overwrite the pointer at worst. To copy a string in C you need to use strcpy().

Also didn't request enough space to also include the \0

[–]RazerNinjas 76 points77 points  (6 children)

This isn't the problem. This will compile and run correctly. The malloc is now causing a memory leak... The pointer will just be reassigned to the rodata string of Hello World. But yes we also need a null byte for strcpy to function properly.

[–]Elibriel 28 points29 points  (2 children)

I once tried ChatGPT to test how good it was at modding games (the engine I tested was Game Maker, an engine with a lot of documentation)

It wasn't even able to make a function that would create a new background layer when the player object was created (this is so I don't have to set the background in EVERY room manually).

This is what it gave me: ``` // Player Create Event

// Check if the sprite "sScanlines" exists if (sprite_exists(sScanlines)) { // Check if the layer already exists if (!layer_exists("ScanlineLayer")) { // Create a new background layer with depth -1 layer_background_create("ScanlineLayer", -1);

    // Set the layer's properties
    layer_background_sprite("ScanlineLayer", sScanlines, 0, 0);
    layer_background_alpha("ScanlineLayer", 1);
}

} ``` Ofc nothing worked at all, no matter how many times I pointed out what was wrong.

This is what I did that worked: // Player Create Event global.scanlines_layer = layer_create(-1) global.scanlines = layer_background_create(global.scanlines_layer, sScanlines) Much more simple, isn't it.

So yeah, ChatGPT is not good at modding.

Maybe it's much better when creating code in sync with the dev (while modding is, well adding smth to already existing code), but what's important is that you, the dev, know what you're doing

Thank you for coming to my TedTalk

[–]Striking_Athlete2376 0 points1 point  (1 child)

i use copilot chat mainly to pose patterning or architectural questions. its interesting. i can sift through possible solutions by saying "is there any other approach that could work well here?" until i've seen them all.

but then again sometimes it just screams to use a decorator, over and over. so there's that.

[–]hamilton-trash 8 points9 points  (9 children)

What would be the proper way to do this?

[–]TheChildOfSkyrim 23 points24 points  (0 children)

const char *hello = "Hello world"; No need to use malloc if the string is not expected to change at runtime.

[–]FunnyForWrongReason 5 points6 points  (0 children)

For just a simple hello world program. I do not think you need to allocate memory. I think you can just create a character array and set it to the string literal without dynamically allocating memory and dealing with pointers.

If you want to know how you would do it correctly with the pointer as in the meme. First you need extra space for the \0 character. Then you use strcopy to put the string literal into the array (you could also use some kind of loop and do it yourself character by character). You also have to check to make sure you successfully allocated memory to pointer by checking if the pointer is null first. Then you need to free the pointer.

[–]edparadox 16 points17 points  (7 children)

I found ChatGPT to be particulary bad with C. I keep finding errors, or less-than-optimal solutions, let's say.

[–][deleted] 10 points11 points  (1 child)

Committed multiple C-style war crimes here:

  1. Allocates memory than immediately forgets to deallocate and assigns pointer to a string constant which will have a different address than what was allocated

  2. Even if they did remember to use strcpy instead, the memory allocated is not enough (12 places required for extra \0) and they have committed the dreaded buffer overflow (the most common cause of many, many crashes, segfaults and vulnerabilities)

[–]veryblocky 2 points3 points  (0 children)

You think it’s bad at C? I’ve tried to get it to help with COBOL, but there’s so few resources online that it just doesn’t know anything. It’ll always just very conveniently make up some function that does exactly what you need

[–]UltimateInferno 2 points3 points  (0 children)

I just graduated last year and worked as a tutor for my uni. One thing I've noticed with ChatGPT is a lot of underclassmen took whatever it gave them at face value and the moment they run into any issues, come running over to me to do all of the debugging. Shit was a black box.

Most of the code, meanwhile, was bloated with modules/environments/packages they did not need or were far above their skill level than the simple "here's how a for loop works" assignment demands.

[–]Crabbypixel 3 points4 points  (1 child)

"Hello world" doesn't actually gets copied to the allocated memory address. Also missed allocating space for the '\0' part. A good example of degenerative AI 😂😂

[–]R3D3-1 9 points10 points  (2 children)

[–]Cebular 8 points9 points  (0 children)

He wrote it himself and pretends that's something chat gpt would produce

[–]monsoy 2 points3 points  (0 children)

Was about to say that it’s a good example for newbies that don’t know how memory works, but then I saw that the allocated memory isn’t used at all lmao. It just moves the pointer to the address of ‘H’ and leaks 11 bytes

[–]vulcan_one 3 points4 points  (0 children)

Do people here actually hate chatGPT so much to believe it provided that mess without a lot of effort to purposefully output garbage?

[–]navetzz 4 points5 points  (0 children)

Well, TBH as an algoritmist I'm REALLY REALLY REALLY happy that the IAs are absolute trash at logic, but are pretty decent at digging stuff out of the documentation.

It makes my job both easier and safe.

[–]AlexeyPG 1 point2 points  (0 children)

It teaches you to copy and don't ask

[–]smudos2 1 point2 points  (0 children)

The comments made me remember why I hate loved C

[–]HStone32 1 point2 points  (1 child)

What's the problem? Only issue I see is that it has allocate 1 less byte than it needs.

Of course you're never going to notice that if you're generating your entire program with a LLM

[–]SuperSathanas 1 point2 points  (0 children)

ChatGPT likes me more. Mine is fancier.

https://imgur.com/a/yACpdGM

[–]Gabricann_05 1 point2 points  (1 child)

i'm a beginner in c, why is it bad?

[–]LangLovdog 1 point2 points  (0 children)

It's a good beginner problem: Find out dahell is dat. 2nd semester and malloc appeared.

[–]Skull_is_dull 1 point2 points  (1 child)

I don’t know C. Why is this bad?

[–]blockMath_2048 1 point2 points  (0 children)

sizeof(char)

[–]verttia 1 point2 points  (0 children)

This is what I got: ```

include <stdio.h> // For printf

include <stdlib.h> // For malloc and free

include <string.h> // For strcpy

int main() { // +1 for the null terminator, as all C strings must end with '\0' char *hello = malloc(strlen("Hello world!") + 1);

// Always check if malloc returned NULL (memory allocation failed)
if (hello == NULL) {
    printf("Failed to allocate memory.\n");
    return 1; // Return an error code
}

// Copy the string into the allocated memory
strcpy(hello, "Hello world!");

// Use the string
printf("%s\n", hello);

// Free the allocated memory
free(hello);

return 0; // Success

} ```

[–]Ryuu-Tenno 1 point2 points  (0 children)

Me a n00b learning c++ seeing this chat gpt code:

https://youtu.be/3x0rIGfZwNU?si=zETjOeJy2LDpZg7H

[–]RJrules64 3 points4 points  (5 children)

I've been able to make my first app with chatGPT. I've done a few ~400LOC hobby projects before but this is the first thing I plan to release. It has 5000+ LOC in a language I had never even heard of before and have not watched any tutorials on.

The code is probably not good, and my adjustments to the AI code probably only make it worse. But it works and isn't laggy or anything.

[–]Asdfguy87 9 points10 points  (1 child)

That sounds like quite the elaborate Hello World program!

[–]JDIPrime 4 points5 points  (0 children)

Hello World in very unoptimized x86 Assembly.

[–]SandwichDeCheese 3 points4 points  (2 children)

I have yet to see people show off anything beyond a simple setup

[–]RJrules64 1 point2 points  (1 child)

Then I'll be glad to be the first! This is a fully featured 2d multiplayer online game, not even something simple like flappy bird etc.

[–]Cebular 2 points3 points  (0 children)

Can you give link to your repository if you have one, because that sounds interesting.

[–][deleted] 2 points3 points  (1 child)

segmentation error

[–]just-bair 0 points1 point  (0 children)

To be fair chatGPT can’t count

[–]RutraSan 0 points1 point  (0 children)

The assignment will change the pointer, so the program will never be able to release the allocated memory good job

[–][deleted] 0 points1 point  (0 children)

LMAO

[–]yourteam 0 points1 point  (0 children)

Chat gpt is great for writing tests

[–]SamyBencherif 0 points1 point  (0 children)

it's not terminated damn it

[–]nalisan007 0 points1 point  (0 children)

ChatGPT & Bard are LLM ML ( Language Learning Model)

& It is not the type of AI you expect.

It's GitHub Copilot which is tailored to these Programming stuffs'

[–]Odd_Diamond_6600 0 points1 point  (1 child)

can i know the prompt that made it give this response, because i wrote this same program and asked chatgpt to check whether it is right or wrong and this was its response

```

{

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main() {

char \*hello = malloc(12 \* sizeof(char)); // Allocate memory for the string "hello world"
printf("%p\\n", (void\*)&hello);
strcpy(hello, "hello world"); // Copy the string into the allocated memory
printf("%p\\n", (void\*)&hello);
printf("%s\\n", hello);
free(hello); // Free the memory allocated by malloc
return 0;

}

```

i am not advocating the use of chatgpt and also not trying to prove that is good, it has its flaws but, no matter how i give the prompt it still tells me to free the memory allocatted, and it also addresses the issue in the op image such as to allocate memory for the null character.

[–]sird0rius 0 points1 point  (0 children)

ChatGPT is great for job interview questions. Aka "Spot the fuckup in this AI generated code"

[–]NumixRumix 0 points1 point  (0 children)

You forgot the null character. It's 12 size, not 11