0

I have a banking system application written in c++ which includes an array of pointers to objects. The program seems to work when using Ubuntu 12.04, however it crashes when running in Windows on the following loop when only one object is in the array.

if(tempPtr->getAccount()->hasCustomer() == true)
{
  while(tempPtr->getAccount()->getCustomer()[i] && i<4)
  {
    tempPtr->getAccount()->getCustomer()[i]->showCustomerDetail();
    i++;
  }
//... 

I have a similar array but with strings that prints out "1.66912-e-307 -" when it is empty but works as soon as I add a string.

Any help would be appreciated Thanks

3
  • You're never initializing i? Commented Dec 3, 2013 at 21:56
  • where does i come from? if it somehow ends being a negative value...it might explain your behavior. Commented Dec 3, 2013 at 21:58
  • alos, not sure it's related to your problem but characters in linux and windows are represented differently Commented Dec 3, 2013 at 22:00

4 Answers 4

2

The line

while(tempPtr->getAccount()->getCustomer()[i] && i<4)

is fishy. Make it

while(i<4 && tempPtr->getAccont()->getCustomer()[i])

to avoid undefined behaviour (if the array has a size four).

Sign up to request clarification or add additional context in comments.

2 Comments

how exactly is this fishy?? you just switched the order of the conditions in the expression - why would this make it less fishy?
@Pandrei because of short circuit evaluation. If i>=4, then an element beyond the array is accessed before the range check is made. Hence the crash.
1

You're not checking the boundaries. Make sure that you're always reading memory that was allocated for the array. Use valgrind or similar to make sure you don't have problems like this.

If not, in practice, you will get out of the array and read random data, or step outside the allocated memory and crash your program. The exact behaviour is undefined in the standards and depends on the platform/compiler combination, but it's always a bad sign.

2 Comments

There is no "expected" behaviour for access outside of array boundaries. It is undefined by the standard. Otherwise a useful answer.
I will try valgrind and see what I find. If its any more help I used mingw32 to compile.
0

there are several differences between the two OS, but related to characters, unicode on Windows is UTF-16LE, and each character is 2 or 4 bytes. Linux uses UTF-8, and each character is between 1 and 4 bytes.

here is a nice article which may be of help The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

6 Comments

Thank you I will give this a read tomorrow. I altered the while loop so the argument was swapped round and it didn't change anything and the i variable was set to 0 each time so Ill put it down to this.
let me see if I understand; you have an array of pointers to objects and "only one object in the array"- which I guess means only one pointer is assigned a memory location representing an object. You access that one object with tempPtr->getAccount()->getCustomer()[i] (i=0); then i++; then the while checks the next pointer tempPtr->getAccount()->getCustomer()[i] - which, if i understand correctly point "nowhere". Is this what is happening? or did I miss something?
I think this may be the case as i have tried to copy the code and use visual studio on a windows computer to compile and still the same problem. It only seems to happen though when only one object is being pointed to any more and it works fine.
if tempPtr->getAccount()->getCustomer()[1] points nowhere and you try to read using that pointer no wonder you get a crash; Please let me know if this is the case.
I did think it was something that simple to begin with but having 2 objects works fine and if you delete one of those objects it also works fine with just one. The only problem Im having is when the initial object is added and I try to print.
|
0

I have managed to solve this with an easy fix. To be able to run it correctly on Windows I just changed...

    if(tempPtr->getAccount()->hasCustomer() == true)
    {
       while(tempPtr->getAccount()->getCustomer()[i+1] && i<4)
       {
          tempPtr->getAccount()->getCustomer()[i]->showCustomerDetail();
          i++;
       }
       }

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.