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

Pages: 1-

C++ question

Name: Anonymous 2005-08-08 18:17

can a function returnan array? i dont think it can, but someone please tell me, it on my C++ final

Name: Anonymous 2005-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:

#include <iostream>

using namespace std;

char *foo()
{
    static char bar[] = "foobar";
    return bar;
}

int main()
{
    char holder, *ptr = foo();
    while(*ptr != '\0')
    {
        holder = *ptr;
        cout << holder;
        ptr++;
    }
    cout << endl;

    return 0;
}

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: Anonymous 2005-08-08 18:37

cool, thanks! so a function can only technically return a value without it going nuts?

Name: Anonymous 2005-08-08 18:39

Err, a "duh" moment...why would I put "holder" in there? You could just "cout << *ptr;". *smacks self*

Name: Anonymous 2005-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: Anonymous 2005-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: Anonymous 2005-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: Anonymous 2005-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.

Name: Anonymous 2005-08-09 21:31

>>8 is it online?

Name: Anonymous 2005-08-09 22:27

Name: Anonymous 2007-10-07 20:17

>>7
The same thing happens in "high-level" languages. Just because it is disguised doesn't change that.

Name: Anonymous 2007-10-07 21:02

>>12
ITT we program with the front panel switches because our programs just compile to machine code anyway.

Name: Anonymous 2007-10-08 1:55

When I was a child, I used 0's and 1's to program, now I only use 0's and program 52% faster.

Name: Anonymous 2007-10-08 2:07

>>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.

Using raw arrays in C++ is just retarded.

Name: Anonymous 2007-10-08 2:11

>>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: Anonymous 2007-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.

Name: Anonymous 2007-10-08 3:45

Name: sage 2007-10-08 3:46

forgot to sage

Name: Anonymous 2007-10-08 6:03

>>17
please elaborate.
static variables are perfectly threadsafe.

Name: Anonymous 2007-10-08 6:03

I mea variables in stack*

Name: Anonymous 2007-10-08 6:24

>>17
I think he meant:


void function ()
{
   char myarray[12]; // Static SIZE, not keyword static
}


and not:

void function ()
{
   static char myarray[12];
}

Name: Anonymous 2007-10-08 6:28

Ah I see. I blame this on >>16's poor choice of words.

Name: Anonymous 2007-10-08 7:30

>>22
YOU FUCKING IDIOT
THAT IS LOCAL NOT STATIC
YOU FUCKIING MORORN FUCK UFKCYOUFRIFNMG

Name: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-10-08 8:08

C++, THREAD OVER.

Name: Anonymous 2007-10-08 8:55

C++ compilers are more incomplete than perl6 ones, and that says a lot.

Name: Anonymous 2007-10-08 8:58

perl6 compilers are more incomplete than C++ ones, and that says a lot.

Name: Anonymous 2007-10-08 9:20

>>1
Yes, BBCode example following.

[lambda [] '[1 2 3]]

Name: Anonymous 2010-09-23 17:11

N-N-N-N-NECRO BUMP

Name: Anonymous 2010-12-17 1:39

This post brought to you by the Gay Nigger Association of America

Name: Anonymous 2010-12-25 13:23

Name: Anonymous 2011-02-04 17:47

Name: Anonymous 2013-07-30 18:07

Lain

Name: Anonymous 2013-07-30 20:16

>>2
You helped him?

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