1

I'm having trouble with my code and hope you could help. When I input an odd number I'm given a segmentation fault, and a bus error if it's even. I'm trying to add 00's to a data array to bring it from length Nprime to a new, larger length Ndprime that I input. I'm doing this in a function *fpad, where my paddata array contains Nprime complex numbers (i.e. 2*Nprime components), and needs to be brought up to size 2*Ndprime.

double *fpad(double *paddata, unsigned int Nprime, unsigned int Ndprime)
{   
   if (Nprime!=Ndprime)
   {
       paddata=(double*)realloc(paddata,(sizeof(double)*((2*Ndprime)-1)));

       for(i>=((2*Nprime));i<(2*Ndprime);i++) paddata[i]=0;  

       if(paddata==NULL)              /* Checks memory is reallocated */
       {
           printf("\nError reallocating memory.\n");
           free(paddata);
           exit(EXIT_FAILURE);
       } 
    }
  return(paddata);  
}

ANy help would be appreciated, I can't see what I'm doing wrong.

Sarah Wishart
  • 49
  • 1
  • 1
  • 2

2 Answers2

1

You are using an undeclared variable i (or maybe it is a global).

for(i>=((2*Nprime));i<(2*Ndprime);i++) paddata[i]=0;

Your first condition checks whether i is smaller than or larger than 2*Nprime (but does not set i). It then goes around accessing the array using this not-properly-initialized value of i that could be negative, which would lead to problems.

You only check whether the memory reallocation succeeded after the loop diagnosed as problematic above. If the memory allocation fails, you've carefully zapped the original copy of the pointer in this function. There is no point in freeing the null pointer — but since you exit on allocation failure, there isn't too much of a problem.

Put your initialization loop after the memory check, with slightly less exuberance in the number of parentheses:

for (int i = 2*Nprime; i < 2*Ndprime; i++)  // C99 (and C++)
    paddata[i] = 0.0;

If you can't use C99 notation, declare int i; in the function.

Don't create global variables called i, ever.

Do pay attention to your compiler's warnings. If it wasn't warning you about 'statement with no effect', you haven't turned on enough warnings.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I've gotten it working, embarrissingly I'd malloced my paddata array too small in a previous function. However, thank you, I've defined my i now, but is the line required? I thought realloced arrays had uninitialised elements, are they already filles with 0s? – Sarah Wishart Nov 09 '12 at 00:25
  • These things happen. Do pay attention to `i`. The question will probably be closed 'too localized'; no offense intended, but that problem won't readily help other people in the future. – Jonathan Leffler Nov 09 '12 at 00:27
  • The extra data in the array is uninitialized; you are correct to want to initialize it. However, you should only initialize when you know you have data to initialize — after you've checked the allocation. And you need to make sure you're using a loop variable that is properly initialized itself, of course. – Jonathan Leffler Nov 09 '12 at 00:28
0

I recommend the function memset function to init your dynamic array.I think the index 'i' in the 'for' statement should range from 0 to 2*Ndprime-2.

prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42