1

I am writing an algorithm in C which takes pointer to an array of unsigned integers as input does some sort of filtering and returns the results back to the same the pointer array. I did this:

static void Filtering(Inst_t *psStruct, uint8 *arraypointer)
{
    uint8 *arrayFilter; 
    uint32 i, n = 0, uCount;
    uCount = psStruct->uCount;   

    for (i = 0; i < uCount; i++)
    {        
       arraypointer[i]  = (arraypointer[i] + arraypointer[i - 1] +  arraypointer[i + 1])/3;     
    }
}

Doing so overwrites the data. But I dont want that to happen. So want to make a local copy of the array data the pointer is pointing to and then use that to compute the average and pass the computed value back.

I am thinking of allocating an array of the size of *arraypointer and initiliazing it with the pointer array values and use this local copy instead. theoritically, this makes sense to me but not sure if this is the best way to do it.

Any pointers/ code to address this problem is highly appreciated. Please help.

Thanks in advance.

2
  • Go ahead, your approach sounds ok. Commented Feb 27, 2013 at 7:30
  • You don't have a "pointer to an array of uint". You just have a "pointer to a uint" Commented Feb 27, 2013 at 19:22

3 Answers 3

1

If you don't want to modify the data, then why have the function doing that? Instead of trying to invent ways to dodge your own program design, change the definition of the function and implement it differently.

static void Filtering (Inst_t *psStruct, 
                       const uint8_t* source, 
                       uint8_t* result)

then in the caller:

uint8_t* filtered_buf = malloc (sizeof(buf));

Filtering(some_struct, buf, filtered_buf);

free(filtered_buf);
Sign up to request clarification or add additional context in comments.

2 Comments

Also, the caller doesn't have to use malloc, he can just use uint8_t buf[N], filtered_buf[N];. This is a much better interface because it doesn't require deallocation to happen at a different level to the corresponding allocation.
@CharlesBailey Exactly. The tight coupling between the buffer allocation and the filtering algorithm is removed with this approach.
1

Sure. That's a completely legitimate way to implement a function that returns a new array.

You can do this to copy the elements quickly:

uint8 *newarray = malloc(uCount * sizeof(uint8));
memcpy(newarray, arraypointer, uCount * sizeof(uint8));

/* do work on newarray */
return newarray;

Be sure to document that your function returns a dynamically-allocated array, and be sure to free it when you are done.

2 Comments

I don't think this is good advice. A better idea would be to have the caller past two buffers, one with read-only access and one that will contain the result. Then let the caller worry about memory allocation and memcpy, something this algorithm shouldn't concern itself about.
@Lundin: Depends entirely on what you want to do with the function. If it is used exclusively in a context where it will need to create a new array, I see no reason to avoid malloc inside the function (naming it e.g. NewArrayByFiltering to make the intent clear).
0

Since you seem to have the number of elements available in the uCount field, you can certainly create a local copy.

If all you want to do is compute the average (a read-only operation) though, I don't see why a local copy would be needed.

To create the copy, you'd do something like:

const size_t num_bytes = psStruct->uCount * sizeof *local;
uint8 *local = malloc(num_bytes);
memcpy(local, arraypointer, num_bytes);

You should of course check that local is non-NULL before use, and free() it before returning from the function (unless you want to return the new array as a result, in which case it becomes the caller's responsibility to free() it when done).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.