Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

is this bad?

Name: Anonymous 2013-08-27 12:46


struct something floattosomething(float f){
       struct something * ret = (struct something*)malloc(sizeof(struct something));
       ret->type = 3;
       ret->f = f;
       return *ret;
}

Name: Anonymous 2013-08-27 12:50

something * ret
Terrible!

Name: Anonymous 2013-08-27 12:55

>>2
not the naming
I wanna know why it crashes my program

Name: Anonymous 2013-08-27 13:16

return *ret;

go back to /g/

Name: Anonymous 2013-08-27 13:42

>>3
return type of *ret is value of pointer at ret. It crashes because it tries to copy the value of entire struct to the stack. instead of the pointer
you should try
 return ret;

Name: Anonymous 2013-08-27 14:28

>>5
but then why does

struct something floattosomething(float f){
       struct something ret;
       ret.type = 3;
       ret.f = f;
       return ret;
}

work?

Name: Anonymous 2013-08-27 14:30

>>6
please read K&R or fuck off back to /g/

Name: Anonymous 2013-08-27 14:31

try returning to /g/

Name: Anonymous 2013-08-27 14:48

is there a better way to do this?

struct something add(struct something x, struct something y){
       struct something sum;
       if (x.type==2 && y.type==2){//both ints
          sum.type=2;
          sum.i = x.i+y.i;
       } else if (x.type==2 && y.type==3){// || (x.type==3 && y.type==2)){
         sum.type=3;
         sum.f = (float) x.i+y.f;      
       } else if (x.type==3 && y.type==2){
         sum.type=3;
         sum.f = (float) x.f+y.i;
       } else if (x.type==3 && y.type==3){
         sum.type=3;
         sum.f = x.f+y.f;
       } else {
              cout << "attempt to sum non-numbers\n";
              exit(EXIT_FAILURE);
       }
       return sum;
}

Name: Anonymous 2013-08-27 14:49

>>7
I'm not gonna read a whole book for a simple curiosity

Name: Anonymous 2013-08-27 14:52

>>10
why the fuck are you even programming C if you can't even read a goddamn ~200 page book

back to /g/ with your retardation

Name: Anonymous 2013-08-27 14:55

>>11
I read a book on C++ ages ago, so I don't remember some things
but I'm managing without it

Name: Anonymous 2013-08-27 14:59

>>12
Not >>11-san, but have you considered reading a simple C reference? You're missing many basic concepts here, I don't think you're ``managing without it'' as you said.

Name: Anonymous 2013-08-27 15:06

>>13
you guys are saying that it doesn't return a copy? or that copies don't crash it?
if they don't crash it then what crashed it?

Name: Anonymous 2013-08-27 15:08

Name: Anonymous 2013-08-27 15:09

>>14
Your last example does return a copy. Your first post shows you don't know the pointer syntax quite well, so I recommend you to read on that.

I won't be replying anymore.

Name: Anonymous 2013-08-27 15:26

>>15
>>16
so it is a copy
so why does >>1 crash?

Name: Anonymous 2013-08-27 15:33

Name: Anonymous 2013-08-27 16:05

>>17
Post a complete program that crashes.
There's nothing in >>1's snippet that would cause a crash (unless you call this function millions of times and run out of memory due to the memory leak)

Name: Anonymous 2013-08-27 16:29

>>19
mental mid/g/et spotted

Name: Anonymous 2013-08-27 17:04

>>20
how about you fuck off to that board you love so much
dumb kike

Name: Anonymous 2013-08-27 17:08

>>21
and how about you fuck off to that board you love so much

Name: Anonymous 2013-08-27 17:24

Someone please escort >>21,22-san-tachi back to their private subreddit.

Name: Anonymous 2013-08-27 18:12

>>23
who are you callin/g/ a tachi, nigger?

Name: Anonymous 2013-08-27 18:16

>>24
貴方と>>21さん、もちろん。

Name: Anonymous 2013-08-27 19:20

Yes, that is very bad. Upon returning the intended value, you have left the memory allocated for ret to sit there until the program's termination. Don't use pointers unless they're necessary, else you will be leaking memory.

Name: Anonymous 2013-08-27 20:09

倍黒人達

Name: tyrone namibi 2013-08-27 20:20

>>27
who you calling a kurojin, bitchnigga

Don't change these.
Name: Email:
Entire Thread Thread List