0

I am going mental, boiled something down to simple, no matter how or what i try, an array will not work? whats up?

The code is simply a single cpp helloworld from cocos2dx. nothing more,

    double *Array = new double[333];
if (Array == nullptr)
    CCLOG("Error: memory could not be allocated");

//initialize it
for( int i = 0; i != 333; ++i){
    Array[i] = 333 - i;

}

for( int i = 0; i != 333; ++i){

    CCLOG("Hi %ld", Array[i] );     
}

The loop always prints 0....

Ive tried many loops, test, the array is never array. its ALWAYS just an int, or double, or whatever type of array i try?

Any thoguhts?

VS2012 cocos2dx helloworld stripped to nothing but an array now. Windows 10

5
  • 3
    You probably have the CCLOG formatting wrong. Commented Jan 4, 2015 at 10:24
  • I agreee with @juanchopanza. Instead of CCLOG, try cout << Array[i] << "\n"; (or printf ("%ld\n", Array[i]); if you prefer). Commented Jan 4, 2015 at 10:28
  • 1
    %d is for integers not doubles. Commented Jan 4, 2015 at 10:32
  • 2
    With a conforming C++ implementation the condition in if (Array == nullptr) will always be false, because new throws an exception if it fails. Commented Jan 4, 2015 at 10:36
  • 2
    Instead of a raw pointer and new, do consider using std::vector. It takes care of the memory management for you, and is resizable and copyable. Commented Jan 4, 2015 at 10:36

3 Answers 3

6

Because you are trying to display a double with %ld format. %ld should be used only for long. IMHO you should :

  • either convert Array[i] to a long :

    CCLOG("Hi %ld", (long) Array[i] );
    
  • or use %g format :

    CCLOG("Hi %g", Array[i] );
    

Both methods should give you correct display.

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

1 Comment

the formatting was my mistake, i had not noticed to change it after trying, int, double, long, float etc. The problem..went away after a reboot of my machine? std::srray people say? How would this be used dynamically ?
0

%ld is for long int. Use %lf for double.

Comments

0

the formatting was my mistake, i had not noticed to change it after trying, int, double, long, float etc.

The problem..went away after a reboot of my machine?

std::srray? How would this be used dynamically / resized?

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.