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?
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.
>>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:
Anonymous2009-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:
Anonymous2009-10-26 22:00
>>10
Boost's shared pointer classes make memory management more manageable.
Name:
Anonymous2009-10-26 22:26
>>11
This is what C++ programmers actually believe.
Name:
Anonymous2009-10-26 22:53
Boost's innovations make programming more enjoyable.
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.
████████████████████████████████████████████████████████
██▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓██
██▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓██
██▓▓▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▓▓██
██▓▓▒▒░░ >>15: malloc my anus ░░▒▒▓▓██
██▓▓▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▓▓██
██▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓██
██▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓██
████████████████████████████████████████████████████████
Name:
Anonymous2009-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;
}
>>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
>>23
As >>14 shows, even people who don't know C++ pretend they know it.
Name:
Anonymous2009-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:
Anonymous2009-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:
Anonymous2009-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:
Anonymous2009-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