1

I'm writing a function in C that takes as input an array and gives as output two arrays. The function will write to an array that is passed in as an argument. As a "fail safe" to prevent memory errors, I want to check whether or not the pointer passed into the function is a valid memory address or not and if it is, free it. Here's the code I have so far, with the part that I'm still unsure about stubbed out:

int fft( double* signal, double* real, double* imag, int size ) {
    double retVal;

    /*initialize output arrays*/
    if( /*real is a valid memory address */ ) {
        free( real );
    }

How do I check that real is a valid memory address that will not seg fault when I try to free it?

rurouniwallace
  • 2,027
  • 6
  • 25
  • 47
  • 1
    I guess if you have to check that, you're doing something the wrong way. – gronostaj Mar 24 '13 at 17:11
  • @gronostaj Well the reason I'm doing it is to make sure nothing goes wrong. It's to prevent the user from inadvertently passing in a bad memory address without the entire program falling apart. – rurouniwallace Mar 24 '13 at 18:14
  • If you're letting the user to enter arbitrary memory addresses, then you're doing something very wrong. – gronostaj Mar 24 '13 at 21:58
  • @gronostaj I don't have any control over what the user enters. I want the program to handle it gracefully in the case that the user does enter something wrong. – rurouniwallace Mar 25 '13 at 00:13
  • But why would you ask user for memory addresses? How could he even know what to enter? Shouldn't your program handle such things itself? – gronostaj Mar 25 '13 at 05:17
  • @gronostaj It's an array, hence a memory address, that will be populated with the output values. Hence I'm looking for a way to given the user valid output regardless of how they choose to pass in the pointers. – rurouniwallace Mar 31 '13 at 16:51
  • So you know the array's size and its starting address, why not just calculate boundaries? – gronostaj Mar 31 '13 at 17:26

2 Answers2

2

There's only one answer to that: You can't.

If it's a valid pointer it points to a valid address. There is no standard way of telling if a block of memory has been allocated on the heap or not.

The only solution is that you need an argument that the pointer is pointing to a heap-allocated block of memory. Or even better, let the caller of your function free memory that the caller allocated.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Sometimes you can check whether you have write access to some pointer. For instance, in Windows you can use IsBadWritePtr function.

Note that this function is obsolete, not thread-safe and not recommended to use. You can read it as 'do not use it'.

Also, you can not free some arbitrary address. You can not be sure that memory allocation you use is the same OS uses. You can not free addresses other than you get with malloc/realloc call.

In your case it would be good to follow WinAPI guidelines: require two pointers with sizes, if there's no room for result, return an error code. Alternatively, you can require two pointers to pointers, then allocate memory yourself, return data and specify that callers must free the memory.

int fft( double* signal, double** real, double** imag, int size ) {
   int retval_size = 256;
   *real = (double*)malloc(sizeof(double) * retval_size);
   *imag = (double*)malloc(sizeof(double) * retval_size);
   ...
}
Aneri
  • 1,342
  • 8
  • 21
  • [IsBadXxxPtr should really be called CrashProgramRandomly](http://blogs.msdn.com/b/oldnewthing/archive/2006/09/27/773741.aspx). – Alexey Frunze Mar 24 '13 at 23:06