#Segmentation fault

53 messages · Page 1 of 1 (latest)

weary burrow
#

I am trying to convert an int to a string using a function instead of writing sprintf each time.
For some reason i'm getting "Segmentation fault" error.
I have been trying to fix it for a while now

(and yes, i am beginner at c)

#include <stdio.h>
#include <iostream>

char* tostring(int n){
    char* output;
    sprintf(output,"%d",n);
    return output;
}

void writeTable(int tb[3][3]){
    printf("\n");

    for (int x=0;x<3;x++){
        char tmp[3];
        for(int y=0;y<3;y++){
            if (tb[x][y] == 1){tmp[y] = char(88); continue;}
            if (tb[x][y] == 2){tmp[y] = char(79); continue;}
            tmp[y] = char(32);
        };
        printf(" %c | %c | %c\n",tmp[0],tmp[1],tmp[2]);
        if (x!=2){
        printf(" ━━━━━━━━━\n");
        };
    };
}


int main(){
    bool isPlayer2Turn = false;
    int table[3][3] = {
       {2, 2, 2},
       {0, 1, 0},
       {0, 0, 0},
    };

    std::system("cls");
    writeTable(table);

    printf("%s",tostring(69));
    return 0;

}```
modest hearthBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

pearl sonnet
#

char* output; <- this is bad

#

for several reasons

#

it's UB

weary burrow
#

what's ub

pearl sonnet
#

but UB is probably the least of the problems with that

#

you never allocate any memory

weary burrow
#

how am i supposed to fix it?

pearl sonnet
#

allocate memory

#

#include <stdio.h> <- also this is bad

#

it's also illegal according to the spec

#

I mean, maybe it "works", but it's bad, plenty of things that "work" are bad

#

#include <cstdio> instead

#

and use std::sprintf

#

but really, don't use std::sprintf either, it's unsafe, use one of the safe versions

#

like std::snprintf

weary burrow
#

damn 🥶

weary burrow
pearl sonnet
#

why don't you allocate memory? idk, I'm not you

weary burrow
#

wait so

#

i think i got it

#

char* returns a pointer right?

pearl sonnet
#

char* is a pointer

#

return returns something

#

they are different mostly unrelated concepts

weary burrow
#

you seem to know a lot about c

pearl sonnet
#

this is plausible, sure

#

I know rather little about C, but I know a reasonably greater amount about C++

#

I read K&R back in the day, but I don't remember much from it, there's not actually much to know about C

#

I'll suggest that I know more about C than I would like

weary burrow
#

i'm hating c ngl

pearl sonnet
#

C is garbage

#

C++ is pretty great though

weary burrow
#

ok i just googled on how to allocate memory

#

and now this is getting even more confusing

pearl sonnet
#

is this for school? like University?

weary burrow
#

school

#

(personal project, trying to improve)

pearl sonnet
#

ok, do you have a textbook for school?

weary burrow
#

i still can't find anything about memory allocation

#

😩 ig i'll close this

#

!solved

modest hearthBOT
#

Thank you and let us know if you have any more questions!

pearl sonnet
#

that was a bit premature

#

;compile

#include <iostream>
#include <cstdio>
#include <cstdlib>

int main()
{
    int theAnswer = 42;

    char* buf = static_cast<char*>(std::malloc(1000 * sizeof(char)));

    std::snprintf(buf, 1000, "Hello world, the answer is %d", theAnswer);

    std::cout << buf << std::endl;

    std::free(buf);
}
stark patrolBOT
#
Program Output
Hello world, the answer is 42
pearl sonnet
#

there's memory allocation ☝️

#

but really you'd do much better to use a container that manages this for you, like vector

#

;compile

#include <iostream>
#include <vector>

int main()
{
    int theAnswer = 42;

    std::vector<char> buf(1000, '\0');

    std::snprintf(buf.data(), buf.size(), "Hello world, the answer is %d", theAnswer);

    std::cout << buf.data() << std::endl;
}