can a function returnan array? i dont think it can, but someone please tell me, it on my C++ final
Name:
Anonymous2005-08-08 18:35
Well, yes and no. Strictly speaking, an array is just a pointer, and functions can return pointers, so yes. However, the array itself is destroyed when it goes out of scope, so the data won't remain intact. You can cheat by declaring the array static, like so:
That way, the variable remains in scope, although you can't access it directly.
Of course, in real C++ programming, you'd just use an object.
Name:
Anonymous2005-08-08 18:37
cool, thanks! so a function can only technically return a value without it going nuts?
Name:
Anonymous2005-08-08 18:39
Err, a "duh" moment...why would I put "holder" in there? You could just "cout << *ptr;". *smacks self*
Name:
Anonymous2005-08-08 18:41
>>3
Pretty much. You can cheat by returning structs or, in C++, objects; you only return a memory address, but that address lets you access multiple things.
Name:
Anonymous2005-08-09 6:10
If you're gonna return pointers, don't forget to allocate memory with new. And then don't forget to delete it afterwards.
Name:
Anonymous2005-08-09 12:10
The general idiom in shitty low-level languages is to pass the function an array for it to write into so you don't have to worry about scoping and memory management.
Name:
Anonymous2005-08-09 16:35
>>1
And the good question is: why do you need to return an array? Keep it as a member instead unless you're forced to by the subject. BTW, it's a "final" and you didn't knew this? Read "Thinking in C++", it's free.
>>1
The real question is - why the fuck are you using an array in C++? If you're going to use C idioms, use fucking C. If you're using C++, either accept a reference to a std::vector (if you need to modify the array), or return a std::vector allocated on the stack.
>>15
Let me rephrase that. Using non-static arrays in C++ is retarded. There's no reason not to statically allocate an array on the stack. Using new/malloc to allocate an array (or statically allocating it, hackhackhack) is just silly when there are safer high-level constructs which don't incur significant performance penalties.
And if you're concerned that much about performance, you shouldn't be using C++.
Name:
Anonymous2007-10-08 2:25
>>16 There's no reason not to statically allocate an array on the stack
You've never done any multi-threaded programming, have you? Using static variables is a hack from the (non thread-safe) C library. It's time to evolve, let the chips fall where they may. Fuck static variables man, fuck redemption.
>>22
YOU FUCKING IDIOT
THAT IS LOCAL NOT STATIC
YOU FUCKIING MORORN FUCK UFKCYOUFRIFNMG
Name:
Anonymous2007-10-08 7:40
>>24
There seems to be some confusion here. We're not talking about static variables, but rather arrays with a ``static'' (constant) size. This thread has ended peacefully.
Name:
Anonymous2007-10-08 7:40
If you're writing C++, as >>15 said, write C++ and use a vector instead. Oh, and this scenario is ideal for the auto_ptr template to ensure that the calling code cleans up (auto_ptr, despite bad naming, transfers ownership of objects).
Even after many years, I still regularly find features that let me write more maintainable C++. I'm not sure if this is a good thing (the features exist) or a bad thing (it took this long for me to learn them).
Name:
Anonymous2007-10-08 8:08
C++, THREAD OVER.
Name:
Anonymous2007-10-08 8:55
C++ compilers are more incomplete than perl6 ones, and that says a lot.
Name:
Anonymous2007-10-08 8:58
perl6 compilers are more incomplete than C++ ones, and that says a lot.