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

Returning an array in C++

Name: Anonymous 2009-10-26 20:39

PLEASE /prog/ help me out here. I've read and tested at least 5 "answers" to my question that I've found on google, but I can't get any of them to work. You guys are better than them though, right?

I wanted to call an array by reference in a function. Then, I learned that that was impossible. I tried returning the array, but you can't do that either.

Apparently, I need to do some shit with pointers to return the array. How the fuck do I do this?

Name: Anonymous 2009-10-26 20:41

read sicp

Name: Anonymous 2009-10-26 20:48

malloc()

Name: Anonymous 2009-10-26 20:51

>>2-3

hilariousreactionimage.cpp

Name: Anonymous 2009-10-26 21:03

>>4
get back to /g/.
malloc() is what you want if you need to return an array from a function.
goddamn sepplers

Name: Anonymous 2009-10-26 21:12

Apparently, I need to do some shit with pointers to return the array. How the fuck do I do this?

First you must ask and answer yourself, "what is an array?"

Then, and only then, will all be revealed.

Name: Anonymous 2009-10-26 21:12

>>5

My bad, I thought it was some LISP thing.
Can you explain how I would use it to return my array? Sorry, I'm not an EXPERT PROGRAMMER, just a newbie who's trying to get ahead of the rest of his class.

Name: Anonymous 2009-10-26 21:16

>>5
they could just use new, but of course that doesn't spare them from learning some minimal pointer arithmetic

Name: Anonymous 2009-10-26 21:17

>>7
imagine you want an array of 20 integers. you would do this:
int *int_ptr = malloc(20 * sizeof(int));
malloc allocates memory and returns a pointer to it. so the above would allocate 20 ints worth of memory.
you then treat the memory as if it was a normal array or pointer, except that it is not bound by scope.
free(int_ptr); when you're finished with it.

Name: Anonymous 2009-10-26 21:34

Hey, morons, this is C++. Use new, not malloc.

>>1
In general, you want to let the caller allocate it and pass it in to the function, unless the function's sole purpose is to allocate an array and return it. This is because C++ memory management is from retardo-land. Same for C.

Name: Anonymous 2009-10-26 22:00

>>10
Boost's shared pointer classes make memory management more manageable.

Name: Anonymous 2009-10-26 22:26

>>11
This is what C++ programmers actually believe.

Name: Anonymous 2009-10-26 22:53

Boost's innovations make programming more enjoyable.

Name: Anonymous 2009-10-26 23:54

>>1

char** array(){
    char* array = new char[8];
    for(int c = 0;c < 8;++c){
        (array)[c] = '\\';
    }
    return &array;
}

int main(int argc, char*argv[]){
    char*strnig = *array();

    return 0;
}


This is a little wrong, but it's right enough. Think of an array as a pointer(if you don't know what a pointer is, think of it as a variable which points to a specific memory location), to change the value of a regular variable in a function in c you'd pass it's memory location to the function like so,


void edit_value(int *point){ //'declaring that point is pointing to an integer
    *point = 3; //'here the '*' value changes a little, to refer to the value which point is pointing to.
}


For an array, which is already a pointer, we need to pass a pointer of a pointer, **. And the rest is just the same as editing point's value.

Name: Anonymous 2009-10-27 0:28

>>10
C > sepples.
malloc() > new.

Name: Anonymous 2009-10-27 0:58


if(malloc == new){
    return ``>>15 == FAGGOT'';
}

Name: Anonymous 2009-10-27 1:20

████████████████████████████████████████████████████████
██▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓██
██▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓██
██▓▓▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▓▓██
██▓▓▒▒░░      >>15: malloc my anus              ░░▒▒▓▓██
██▓▓▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▓▓██
██▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓██
██▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓██
████████████████████████████████████████████████████████

Name: Anonymous 2009-10-27 2:15

>>14
For an array, which is already a pointer, we need to pass a pointer of a pointer, **. And the rest is just the same as editing point's value.
Are you retarded? If it's a pointer, why don't you pass the pointer?

Returning the address of a local variable.
Yup, you're retarded.

char* array() {
    char* array = new char[8];
    for(int c = 0;c < 8;++c){
        array[c] = '\\';
    }
    return array;
}

int main(int argc, char* argv[]) {
    char* strnig = array();
    return 0;
}

Name: Anonymous 2009-10-27 3:39

>>17 = (anus*)malloc(mysize * sizeof(anus));

Name: Anonymous 2009-10-27 3:40

>>18
This is /prog/ not /pr/.

Name: Anonymous 2009-10-27 3:41

namespace Anii {
    char * createArray(int arraySize = 8) {
        char *array = new char[arraySize];
        arrayFill(array, '\\', arraySize);
        return array;
    }

    void arrayFill(char *arr, char filler, int size) {
        for (int c = 0; c < size; ++c) {
            arr[c] = filler;
            }
    }
}

int main(int argc, char *argv[]) {
    char *strnig = Anii::createArray();

    return 0;
}

Name: Anonymous 2009-10-27 3:51

new(faggot) is what you do in java.
malloc()(manly) is what you do in C.

Name: Anonymous 2009-10-27 3:58

I would never suspect people from /prog/ to admit to know C++.

Name: Anonymous 2009-10-27 4:17

>>23
/prog/ is tsundere for C++

Name: Anonymous 2009-10-27 4:39

>>23
yeah yeah yeah.
that's what all the newfags who are here because they want to learn how to program games say.

Name: Anonymous 2009-10-27 5:44

>>22
C++:

faggot myFaggot = new faggotFactory.createFaggot();

C:

manly_t *Man = malloc(sizeof(manly_t));

Name: Anonymous 2009-10-27 6:40

DONT FORGET TO free() MY ANUS AFTERWARDS

Name: Anonymous 2009-10-27 6:43

>>27
NEVER!
ALL YOUR
anus ARE BELONG TO ME.

Name: Anonymous 2009-10-27 6:52

>>23
We all know it, we all hate it, and some of us,me, even have ~4 books on the subject(none are specifically C++, but use it as the example language)
>>24
That would require us to secretly like it

Name: Anonymous 2009-10-27 7:16

>>24
Cprusprusmoe!

Name: Anonymous 2009-10-27 12:42

>>23
As >>14 shows, even people who don't know C++ pretend they know it.

Name: Anonymous 2009-10-27 13:29

Why does anyone bother to learn sepples? I remember when I tried. I thought I was really learning something, it felt difficult and strangely rewarding. Then I learned a decent language and I realize I had been trolled hard.

Name: Anonymous 2009-10-27 14:18

>>32
Wow, that is the same experience I had. But what I find really fascinating is that a lot of people can't accept that they have wasted years trolling themselves, so they cling to Sepples like their lives depend on it.

Name: Anonymous 2009-10-27 14:58

YOU RETURN A POINTER POINTING TO THE ARRAY YOU MADE IN THE FUNCTION.  GOD DAMN EVEN I KNOW THAT AND IM A PYTHON FAGGOT.

Name: Anonymous 2009-10-27 15:12

>>34
As a Python faggot, you forgot that the locally created array will be destroyed on function return, thus your pointer will point to nothing (of value) (was lost). malloc/new is necessary

Name: Anonymous 2009-10-27 15:17

>>35
Don't help him!

Name: Anonymous 2009-10-27 15:18


void *getArray(int length, int bytes)
{
   return new char[length * bytes];
}


NO EXCEPTIONS

Name: Anonymous 2009-10-27 15:20

>>37
you've essentially used new to remake malloc(). what is wrong with you

Name: Anonymous 2009-10-27 15:21

>>38
well, calloc, more accurately

Name: Anonymous 2009-10-27 15:22

>>38
I answered his question on how to return an array so redditers like you would stop bumping this shitty thread.  Now please use sage in the future!

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