0

I would like to create a new array of values, and I am not sure how to do this efficiently. Since to create the new array I have to call a function, passing the old array as a parameter. Right now my code looks something like:

float *newMeasurements1;
newMeasurements1 = malloc(sizeof(calcNewArray(oldArray)));
newMeasurements1 = calcNewArray(oldArray);


float *calcNewArray(float *oldArray) {
    float *newArray;

    int new_size = sizeof(oldArray) - outliers;
    newArray = malloc((new_size) * sizeof(float));

    for (i = 0; i < new_size; i++) {
        newArray[i] = oldArray[i];
    }

    return newArray;
}

I am not sure if this is the correct way to do this because I have to call the function once to know the size of the new array. And then call the function a second time to actually assign the array to the allocated memory. How best can I do this?

Hdot
  • 543
  • 1
  • 4
  • 15
  • 1
    is oldArray mentioned here is a array? or pointer? what is the declaration of the function calcNewArray? malloc(sizeof(calcNewArray(oldArray))); will only give the size of the data type it is returning. so i think this is not what you need. second one can work fine if you are allocating the proper size inside and returning a float * – Deepak Feb 15 '19 at 10:32
  • Does my edit help? – Hdot Feb 15 '19 at 10:36
  • @Hdot is helps somewhat. But we need to know how you deal with `newArray` in the `calcNewArray` function – Jabberwocky Feb 15 '19 at 10:37
  • 1
    I have edited above. it seems second one is suitable. if you allocate the proper memory to newArray with malloc() – Deepak Feb 15 '19 at 10:38
  • @Hdot If `calcNewArray` is OK, then you probably can juste remove the line `newMeasurements1 = malloc(sizeof(calcNewArray(oldArray)));`. But to be sure you need us to show how you deal with `newArray` in `calcNewArray`. – Jabberwocky Feb 15 '19 at 10:40
  • @Jabberwocky what does calcNewArray have to do to be ok? – Hdot Feb 15 '19 at 10:44
  • @Hdot what exactly is `oldArray` here: `newMeasurements1 = calcNewArray(oldArray);`? – Jabberwocky Feb 15 '19 at 10:52
  • 1
    what is outliers? also this will not work sizeof (oldArray) will not give the array size. how you are declaring the oldArray outside where you calling this function? – Deepak Feb 15 '19 at 10:52
  • @Hdot read this SO article: https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array. You probably also should read about `malloc` and dynamic memory allocation in your C text book. – Jabberwocky Feb 15 '19 at 10:53

1 Answers1

0

This line is useless:

newMeasurements1 = malloc(sizeof(calcNewArray(oldArray)));

just write this:

newMeasurements1 = calcNewArray(oldArray);

The malloc is already done in calcNewArray.

But there is another problem in calcNewArray, arrays decay to pointers to their first element when you pass them to a function, therefore sizeof(oldArray) is no the sizte bof the array you passed to calcNewArray but it is the size of a pointer. You need to pass the size of oldArray explicitely as a second parameter:

float *calcNewArray(float *oldArray, int oldsize) {
    float *newArray;

    int new_size = oldsize - outliers;
    newArray = malloc((new_size) * sizeof(float));

    for (i = 0; i < new_size; i++) {
        newArray[i] = oldArray[i];
    }

    return newArray;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • one thing not sure, what is outliers here? if this is greater than oldsize then it will be a problem as oldsize - outliers will be a -ve value. as this means this will be a huge memory allocation. – Deepak Feb 15 '19 at 10:55
  • 1
    @Deepak this is not really part of the question beeing asked here, but you are right, the code is still somewhat fishy, probably `outliers` should be passed as an argument to `calcNewArray` – Jabberwocky Feb 15 '19 at 10:57