0

I have a function that generates values in an array and returns a pointer to that array. Here's the MWE code:

int *f(size_t s)
{
    int *ret=new int[s];
    for(size_t a=0;a<s;a++)
    {
    ret[a]=a;
    cout << ret[a] << endl;
    }
    return ret;
}

note that I have a cout line in for for loop to prove to myself that the array is being populated properly.

Now, here's my problem. I can't find the correct method of using the returned array. Here's what I've been doing:

int main (void)
{
 int ary_siz = 10;
 int ary[ary_siz];
 *ary = *f(ary_siz);
 cout << ary[0] << endl;
 cout << ary[2] << endl;
 cout << ary[3] << endl;
}

The first element in ary seems to be right. The others (ary[1],ary[2]...) are not. Can anyone tell me what I'm doing wrong?

2
  • After addressing the points made in the answers, you might be surprised to find that ary[1] still doesn't print out. Commented Oct 16, 2011 at 21:38
  • Thanks Marcelo. ary[1] and I have never gotten along anyway. Commented Oct 17, 2011 at 19:10

4 Answers 4

4
int main (void)
{
 int ary_siz = 10;
 int *ary = f(ary_siz);
 cout << ary[0] << endl;
 cout << ary[2] << endl;
 cout << ary[3] << endl;
 delete [] ary;
}
Sign up to request clarification or add additional context in comments.

7 Comments

mmmm... you stole the delete[] part :)
Didn't mean to -- just didn't think about it until after I posted the first answer. +1 to you for posting that first though.
Vaughn (and sehe) - Gracias amigos. This works -- AND it points up the core of my confusion. Most of the error msgs I was getting related to type errors (e.g. 'can't convert int to *int...blah blah), and, given this Q&A: c-faq.com/aryptr/aryptrequiv.html, I thought the problem was that I was just not getting what the returned type really was. BUT, in your answer, you are not declaring a pointer-to-int-array to initialize to the returned array, you just have a normal pointer-to-int. But then you are treating it like an array, which I thought the Q&A I posted said was verboten.
I'm frustrated because I'm reduced to code-copying without understanding what's happening. I've read many, seemingly contradictory things about pointers and arrays. Blerk.
@bev: that's good. I'm very happy your not simply accepting it. This means you'll be one of those who will actually understand in the end. However, it is also largely off topic! Did you know there is a chat room for c++ on StackOverflow here: chat.stackoverflow.com/rooms/10/loungec?
|
1

The assignment

*ary = *f(ary_siz);

copies a single element. Use

int main (void)
{
 int ary_siz = 10;
 int *ary = f(ary_siz);

 delete[] ary;
}

fixing the memory leak as well

1 Comment

Thanks sehe. I have my own memory leak when it comes to memory leaks :-).
1

You allocate an array in the function and you just assign its first element to the first element of your stack-allocated array, instead of just using the returned array.

you should do something like that:

int main (void)
{
 int ary_siz = 10;
 int *ary;
 ary = f(ary_siz);
 cout << ary[0] << endl;
 cout << ary[2] << endl;
 cout << ary[3] << endl;
 delete[] ary // don't forget to release the memory
 return 0; // You should return something in the main function
}

Moreover, in C++ you should use vectors instead of "bare-metal" arrays whenever possible.

7 Comments

Using std::array<> is also acceptable (in addition to std::vector<>). Also, main does not require a return value; 0 is returned by default in the absence of an explicit return statement.
Never heard of std::array: is it a new feature of C++11? As for the missing return statement, I consider it bad style, more of an indication of a forgot explicit return.
std::array<> is the C++11 implementation of TR1's std::tr1::array<>, which in turn is TR1's implementation of Boost's boost::array<>. As for missing return, I'd say it's highly unusual for an application's return code to have any relevance or meaning, so it should only be present if it's a non-default return value. But, this is of course entirely subjective.
@akappa - thanks. And thanks for the reminder about releasing the memory and returning something in main. I hadn't known that.
Also, I'm implementing your suggestion about vectors. Here's a question: Is a vector a class wrapper around a 'bare-metal' array? In other words, are all the things that are true for arrays true for vectors?
|
0

How about this?

int *ary = f(ary_siz);

The you can just use the [] operator as you do in the couts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.