Strange issue with printing array value

This is a discussion on Strange issue with printing array value within the C Programming forums, part of the General Programming Boards category; The problem below is related to my other recent forum post, http://cboard.cprogramming.com/c-pro...tml#post981978 I've learned more about what's going on, and ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    67

    Strange issue with printing array value

    The problem below is related to my other recent forum post,

    Variable shows NAN after return from function call

    I've learned more about what's going on, and decided to start a new thread to succinctly summarize this ongoing strange problem:

    In my code, within one of my functions (functionA()), I call another function (functionB( )), which calculates and assigns the values in MyArray[ ], which is an array of doubles that is created and intialized to 0.0 within functionA(). I've verified that the calculated values assigned to MyArray[ ] within functionB( ) are always correct at the very end of functionB().

    Immediately upon return from functionB(), back in functionA(), when I print out the values in MyArray(), strange things are happening.

    Initially, I had simply had the print statement:

    Code:
    printf("Back from functionB(): MyArray[0] = %f, MyArray[1] = %f, MyArray[2] = %f, ...\n", MyArray[0],MyArray[1],MyArray[2],...);
    Every time, the value of MyArray[0] always showed NAN instead of a real value (but all other values in MyArray were fine), which led me to think that there was some problem with the actual value of MyArray[0].

    But now that I've added some additional print statements, I've found something odd: When I inserted the following print statements *around* my original one above:

    Code:
    printf("Back from funcB() #1: MyArray[0] = %f.\n", MyArray[0]);
    printf("Back from funcB() #2: MyArray[0] = %f.\n", MyArray[0]);
    
    printf("Back from functionB(): MyArray[0] = %f, MyArray[1] = %f, MyArray[2] = %f, ...\n", MyArray[0],MyArray[1],MyArray[2],...);
    
    printf("Back from funcB() #3: MyArray[0] = %f.\n", MyArray[0]);
    printf("Back from funcB() #4: MyArray[0] = %f.\n", MyArray[0]);
    ...*all* of the new MyArray[0] values printed out, before and after my original statement, are all fine! And yet the longer print statement above (in the middle of the new additions) continues to show MyArray[0] = NAN.

    If I substitute MyArray[1] or some other value in for MyArray[0] in the long print statement above, *any* of these other values *also* prints out as NAN! So apparently, there is a problem with whichever first value is being printed in this long print statement, and not specifically a problem with the value of MyArray[0].

    Also, I made a copy of the long print statement above, and pasted it on the next line after my original long print statement. Every value in this 2nd copy is fine, including MyArray[0]. However, if I comment out the original long statement and only allow the copy of this long statement to remain, the first value printed out is now NAN.

    I've checked and checked again for a syntax error in my long printf( ) statement, but I haven't found anything...

    My code system is fairly complex, so I don't think that including extensive code in this thread is going to be useful or necessary to solve this problem. This *seems* to be a weird but isolated issue... any suggestions about what might possibly be going wrong, such that the value of MyArray[0] could be printed fine before my long print statement above, but then always displays erroneously later on, even though no changes to the values in MyArray[ ] are being made?

    Thanks in advance for your input.
    Last edited by CodeKate; 11-24-2010 at 10:41 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    It sounds symptomatic of undefined behaviour, e.g., a buffer overflow problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    hmm, and what would be the best way to pinpoint the source of the problem?

    I have the gdb debugger installed on the Cygwin platform that I use, but haven't been able to figure out how to use gdb with my whole system of .c and .h files required to compile my code... Any other feasible way to locate the source of this issue?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Quote Originally Posted by CodeKate
    hmm, and what would be the best way to pinpoint the source of the problem?
    You could use a static analysis tool as an aid to discover potential problems, one of which may be the actual problem. You could post the relevant part of the program, if it is small enough, or write the smallest and simplest compilable program that demonstrates the problem. You could get a colleague to do a code review.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    >> You could use a static analysis tool as an aid to discover potential problems

    Thanks for the suggestion... I wasn't familiar with static analysis tools before your post, but I see that there is an array of available tools (BLAST, Clang, Frama-C, Lint, Sparse, Splint). Does anyone have recommendations about which of these you've found useful? If, as I mentioned, my code system involves multiple .c and .h files, is there a particular static analysis tool that would be suited for analysis of this system?

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    If MyArray is double, perhaps you can try %lf in all of the printf formats. Because it sounds like the compiler did not stack the arguments properly.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    Quote Originally Posted by nonoob View Post
    If MyArray is double, perhaps you can try %lf in all of the printf formats. Because it sounds like the compiler did not stack the arguments properly.
    Thanks for your suggestion. I tried this, and unfortunately it didn't solve the problem. But it's probably good practice for me to consistently use %lf, anyway...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Char Array Issue
    By aussiemcgr in forum C Programming
    Replies: 5
    Last Post: 10-27-2010, 03:05 PM
  2. Help printing a multi array
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 05-05-2008, 01:35 PM
  3. help with small program. array issue
    By InvariantLoop in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 12:26 PM
  4. Printing an Array to the screen
    By simhap in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2001, 10:16 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21