0

I already looked with a debugger and i get this error when it leaves the main() function. Here's my Code:

#include <iostream>

char * trim(const char * _str) {
    char * newString = new char;

    for (int i = 0; i < 10; i++) {
        newString[i] = _str[i];
    }
    newString[10] = '\0';

    return newString;
}

int main() {
    std::cout << trim("This is a test");
    return 0;
}

Thanks for your help.

NeeRaX
  • 13
  • 2
  • 2
    You’re allocating a single char and writing way more than that. – Taekahn Apr 11 '22 at 03:58
  • Hey @Taekahn , thanks for your answer to my question. I got no error when I did it like this: `#include ` ` ` `char * trim(const char * _str) {` ` char * copiedString = new char;` ` char * editedString = new char;` ` ` ` for (int i = 0; _str[i] != '\0'; i++) {` `copiedString[i] = _str[i];` `}` `for (int i = 0; i < 10; i++){` ` editedString[i] = copiedString[i];` `}` ` editedString[10] = '\0';` ` return editedString;` `}` ` ` `int main() {` `std::cout << trim("This is a test");` ` return 0;` `}` How come? – NeeRaX Apr 11 '22 at 04:05
  • Undefined behavior is undefined. "Seems to work" is one possible manifestation of undefined behavior, program crashing is another. – Igor Tandetnik Apr 11 '22 at 04:08
  • In c++ compiling, linking, and running without error are, sadly, not signs of an error free, correct program. – Taekahn Apr 11 '22 at 04:08
  • Hey @molbdnilo , also thank you for contributing to my question. (And sorry for the comment above, i'm new to StackOverflow and don't really know how this text formatting works). I'm trying to fix my code now thanks. – NeeRaX Apr 11 '22 at 04:10

1 Answers1

1

you error is this

#include <iostream>

char * trim(const char * _str) {
    char * newString = new char; <<<<==== you allocate one character

    for (int i = 0; i < 10; i++) {
        newString[i] = _str[i]; <<<<=== but try to write a whole string
    }
    newString[10] = '\0';

    return newString;
}

int main() {
    std::cout << trim("This is a test");
    return 0;
}

not sure what you are trying to do but this will work

#include <iostream>

char * trim(const char * _str) {
    char * newString = new char[strlen(_str) + 1];

    for (int i = 0; i < 10; i++) {
        newString[i] = _str[i];
    }
    newString[10] = '\0';

    return newString;
}

int main() {
    std::cout << trim("This is a test");
    return 0;
}

really you should use

  • std::string for strings in a c++ program
  • std::vector for arrays of things created on the fly
pm100
  • 48,078
  • 23
  • 82
  • 145
  • I tried to do the number 8 from https://www.codecademy.com/resources/blog/c-plus-plus-code-challenges-for-beginners/ The challenge here is to create a function that trimms a string to 10 characters. i didn't want to use std::string because i thought doing it by myself would be a good idea to improve my c++ skills, i don't why i thought that way. – NeeRaX Apr 11 '22 at 04:20
  • @NeeRaX learning the underlying principles behind the abstractions is always admirable. – Taekahn Apr 11 '22 at 04:22
  • @NeeRaX - its fine to experiment , I was just making sure you know that eventually you should use string and vector – pm100 Apr 11 '22 at 04:24
  • @NeeRaX writing programs in C++ usually means either using STL, or implementing your own classes for functionality STL doesn't provide. It was called "C with Classes" for a reason! – The Dreams Wind Apr 11 '22 at 04:24
  • @NeeRaX - also note that you should `delete[]` the value returned from 'trim' - otherwise this will leak (another advantage of std::string) – pm100 Apr 11 '22 at 04:26
  • @TheDreamsWind yeah, I'm pretty new to c++, just learned how classes work. I use c++ for a few weeks now so I'm really just a beginner. That's also probably why i didn't see this error in the first place. y'all probably think i'm really dumb. – NeeRaX Apr 11 '22 at 04:28
  • @NeeRaX why not see if you can write your own string class as an exercise. You will learn a lot – pm100 Apr 11 '22 at 18:09
  • @pm100 That sounds like a very good idea. I'm going to try this but i do not think that i can recreate all the methods that std::string has. also i don't really know how to `delete[]` a char pointer after I returned it in a function. Still I'm going to try my best. :) – NeeRaX Apr 12 '22 at 03:40
  • @NeeRaX just do the basics, make a string, copy a string and delete it in the destructor, maybe > and < , if you get stuck post a question here – pm100 Apr 12 '22 at 04:40