0

I am using an object called tArray which is an array of objects and does work correctly to perform all the other tasks. I need to form an array of strings using buyerName from the array of objects to fill it our new array and then return it. however I keep getting the error as stated in my title, does anyone know why? I am struggling on only this method on my program so far. thanks

this first chunk of code is used to call the method in my analyser class

std::string *topBuyers = analyser.topFiveBuyers();
for(int idx = 0; idx < 5; idx++)
    std::cout << "Top buyer" << (idx+1) << ": " << topBuyers[idx] << std::endl;
std::cout << std::endl;
delete [] topBuyers;

the next portion of code is the method in question.

string* Analyser::topFiveBuyers()
{
const int sSize = 5;

string calcString[sSize] = {tArray[0].buyerName, tArray[1].buyerName, 
tArray[2].buyerName, tArray[3].buyerName, tArray[4].buyerName}; 

return calcString;
}
user2075995
  • 23
  • 1
  • 7
  • 0xCCCCCCCC is uninitialized memory [When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](http://stackoverflow.com/q/370195/995714) – phuclv Apr 28 '15 at 15:44

1 Answers1

5

You are returning a pointer to a temporary variable from topFiveBuyers.

The correct way to do this would be to return a vector<string> or array<string,5> from your method instead of a string*.

If for some extremely weird reason, you absolutely have to return a pointer, you'll need to allocate space using new - with all the problems that come with it, since the callee will have to clean up after it. You're currently using automatic storage within function scope, which is not guaranteed to still be available/accessible after the function returns, hence your access violation.

us2012
  • 16,083
  • 3
  • 46
  • 62
  • 1
    Yep. And from what I remember, the Visual Studio debugger fills all other memory locations that you didn't use with 0xcccccccc to make it obvious when you're accessing memory you shouldn't be (in this case, the stack after the stack frame with the temp object has been destroyed.) – Alex Mar 08 '13 at 20:09
  • @AlcoJaguar I agree with the general sentiment of what you edited into my answer, but in general such a substantial edit of someone else's answer is a bit out of place in my humble opinion. – us2012 Mar 08 '13 at 20:14
  • Thanks for the replies, this is a small part of some homework and I would rather not change the pointers right now. however I cant get my head around how to solve this, I am confused :/ It is probably hitting me on the face but I cant see it lol – user2075995 Mar 08 '13 at 20:15
  • @us2012 Apologies, still getting a feel for how much I can edit or when. I thought it would be best to consolidate information into a single answer instead of having competing ones that were essentially the same. I defer to your judgement, edit as needed :) – AlcoJaguar Mar 08 '13 at 20:17
  • @AlcoJaguar No worries (and anyways, thanks for the suggestions!). I also just saw that I can't even blame you, as your edit would have been on the review queue and the reviewers apparently decided that it was okay. Oh well :) . – us2012 Mar 08 '13 at 20:19
  • @user2075995 Well, you would use `string *calcString = new string[5];`, fill it and then return `calcString`. But, I repeat, this is only a quick fix, quite a bad idea in general and nothing good can come of it. – us2012 Mar 08 '13 at 20:20
  • Thanks a lot everybody who helped me out here - I will keep in mind the part on the pointers and will try to intergrate the suggested solutions down the line, perhaps tomorrow, thanks :) – user2075995 Mar 08 '13 at 20:23