Thread: Array changes value after external function that does not call the array

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    59

    Array changes value after external function that does not call the array

    My code looks like this at the moment;

    Code:
    	for (j=0; j<bfun_lines; j++)
    	{
    		printf("Attention! BEFORE main split array %i is %lf\n", j, split_array[j]);
    	}
    
    	
    	encrypt_array = padme(encrypt_array, &n_value);
    
    	for (j=0; j<bfun_lines; j++)
    	{
    		printf("Attention! AFTER main split array %i is %lf\n", j, split_array[j]);
    	}
    In the first printf I have the array I originally assigned to split_array and after the function call which doesn't call split_array, it prints a different array.

    I am pretty stuck as to what to look for as I have no idea how this could happen? Any ideas or would you need to see more code?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I have no idea what you think you mean by "call split_array". Is split_array global? If so, it can be changed by any function that feels like it. Are either of these arrays "fake" arrays (i.e., pointers to double instead of double[])? If so, they might be pointing to the same place. Does your function do something crazy, like write past an array? If so, it will probably end up writing on some other piece of memory, which may very well be split_array.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    It isn't global. Both arrays are pointers to double but they have been individually malloc'd (if that makes a difference).

    By 'call split_array' I meant that I am not passing it through an external function.

    The function reallocs memory to the array passing through it (which is not split_array) with

    Code:
    pad_array = realloc(array, (new_size)*sizeof(double));
    with pad_array a pointer to a double in the external function.

    Is there any way to avoid the memory being overwritten?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Are you saying split_array is being changed when you call padme() even though there are no references to it made? Then we'd need to see the code in padme() to spot any array/pointer out-of-bounds stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM