bizarre print statement glitch

This is a discussion on bizarre print statement glitch within the C Programming forums, part of the General Programming Boards category; Okay, this is really weird, but perhaps someone knows why this is happening... After creating an array in a separate ...

  1. #1
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    75

    bizarre print statement glitch

    Okay, this is really weird, but perhaps someone knows why this is happening...

    After creating an array in a separate function, and successfully returning the pointer
    to main, I've noticed that if I place a simple print statement *before* I use a for statement to assign the values to a new array in main, it won't pass the values. Here are two code snippets, the first is successful, the second fails:

    -----------------------------------------------------------------
    Code:
    // this is in main():
    
    {
    	double newarray[3][5] = {0.0}; //newarray created to take createarray's values
    
    	double * ptr;
    
    	int count, total;
    
    	ptr = createarray(); // createarray is separate function that creates array
    
    	for(total = 5, count = 0; count < total; count++)
    		newarray[0][count] = *((ptr) + count);
    
    // print statements after this value assignment indicate success, HOWEVER:
    -----------------------------------------------------------------
    Code:
    // same code as before:
    
    {
    	double newarray[3][5] = {0.0};
    
    	double * ptr;
    
    	int count, total;
    
    	ptr = createarray();
    
    	printf("this is a TEST print statement\n\n"); // this can say anything!
    
    	for(total = 5, count = 0; count < total; count++)
    		newarray[0][count] = *((ptr) + count);
    
    // now, the subsequent print statement to print out the values only reports zeroes!!!
    -----------------------------------------------------------------

    My question is:

    Does anyone have any idea why simply placing a print statement *after* the

    ptr = createarray();

    statement but *before* the

    for(total = 5, count = 0; count < total; count++)
    newarray[0][count] = *((ptr) + count);

    statement somehow prevents the values from being transferred?

    I can just remember to always do the value assignments right away, but it seems like there shouldn't be such a restriction.

    I'm using Microsoft's Visual C++.

    Thanks for any input!

  2. #2
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    75

    here's the complete code:

    Compiled this way, it will show the array's contents successfully transferred to the newarray in Main. But, you un-comment the print statement and recompile, you'll get zeroes for the newarray's contents!!!

    Code:
    #include <stdio.h>
    
    
    double * createarray(void);
    
    int main(void)
    {
    	double newarray[3][5] = {0.0};
    
    	double * ptr;
    
    	int count, total;
    
    	ptr = createarray();
    
    //	printf("\n\nThis is a TEST print statement!\n\n");
    
    	for(total = 5, count = 0; count < total; count++)
    		newarray[0][count] = *((ptr) + count);
    
    	printf("\nTest of Main array content transfer:\n");
    
    	for(total = 5, count = 0; count < total; count++)
    		printf("%.2lf \n", newarray[0][count]);
    
    	return 0;
    }
    
    double * createarray(void)
    {
    	int count, total;
    
    	double array[3][5];
    
    	double * createptr = 0;
    
    	printf("Each time I prompt you, please enter five floating point values.\n");
    
    	printf("Enter *only* your first row of five floating point numbers, please.\n");
    
    	for(total = 5, count = 0; count < total; count++)
    		scanf("%lf", &array[0][count]);
    
    	printf("\nTest of original array's contents [first row only]:\n");
    
    	for(total = 5, count = 0; count < total; count++)
    		printf("%.2lf \n", array[0][count]);
    
    	createptr = array;
    
    	printf("\nTest of original ptr's contents:\n");
    
    	for(total = 5, count = 0; count < total; count++)
    		printf("%.2lf \n", *((createptr) + count) );
    
    	return createptr;
    }

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    957
    You're returning a pointer to a local array. When a function returns, all objects local to that function are destroyed, meaning you should not try to use them.

    In your case, what's probably happening is that the function returns, invalidating its local objects; but they haven't yet been overwritten. It's still undefined behavior when you access them, but in this case, the undefined behavior means you got the answer you expected. However, when you call printf(), its local variables are now in the same place that createarray()'s were. When printf() futzes around with its local variables, it's scribbling over the same area that you have a pointer to.

    A quick and dirty way to fix the problem is to make your array static. This has potential downsides, but it's not universally a bad solution. You could also pass in an array and update it in the function, instead of having the function provide its own array. Or, if you wanted to, you could allocate a dynamic array with malloc() and return that, remembering that you'd have to free it, too.

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Because array is local to createarray() can't be reliably accessed after it goes out of scope, that is, after the function returns. In fact, you shouldn't be accessing it.

  5. #5
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    75

    thanks!

    Thank you for replying so quickly! As you can surely tell, I'm pretty new to this. I haven't even reached the part of the textbook that introduces malloc() yet.

    I really appreciate your responses... you guys saved me a lot of head banging, lol. I hope to one day be able to contribute in the same way for others.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. print output problem
    By chor in forum C Programming
    Replies: 1
    Last Post: 12-29-2002, 08:45 AM
  3. Replies: 1
    Last Post: 07-31-2002, 11:35 AM
  4. send data to printer and print on paper
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 07-22-2002, 05:19 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

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