Issue with returning an array of type int from function

Sjoseph

I have read through a lot of posts giving ways in which you can return an array of type int from a function. I have attempted to follow the approach of dynamically allocating the memory inside of the function using the malloc() function.

In the example code, I am using a function foo which calculates peaks in an array which are greater than a specified value.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* function declaration */
int *foo(int arr[],int size);
int main()
{
int test[] = {1,2,1,4,5,2,7,8,9,1}; //array of test data.
int *p;
p = foo(test,10);
int w;
for(w=0;w<5;w++)
    {
      printf(" Peak %d\t",p[w]); // This line is giving a non sensible answer.
    }
    free(p); // free the memory

return 0;
}

int *foo(int arr[],int size) {
    int j;
    int Peak_min = 3; // Minimum peak height
    int *ret = malloc(size * sizeof(*ret));
    if(!ret)  return 1;
    for (j = 0; j < size -1; ++j)
    {
        if ( (arr[j] < arr[j+1]) && (arr[j+1] > arr[j+2]) && (arr[j+1] > Peak_min))// Peak criteria
        {
            ret[j] = arr[j+1];
            printf(" Peak_No %d",ret[j]); // This line is giving the correct output.            
        }
    }
    return ret;
}

The output printed in the function gives 5 and 9, as expected. However, the output when I call the function in int main() gives non-sensible values. I am struggling to find the error with my code, any suggestions on how I can debug/fix this?

Update

I edited the for loop in the fucntion foo to

for (j = 0; j < size -2; ++j)
    {
        if ( (arr[j] < arr[j+1]) && (arr[j+1] > arr[j+2]) && (arr[j+1] > Peak_min))// Peak criteria
        {
            ret[j] = arr[j+1];
            printf(" Peak_No %d",ret[j]); // This line is giving the correct output.
        }
        else
        {
         ret[j] = 0;
        }
    }

I am now getting the output I wanted.

Sourav Ghosh

malloc() returns unitialized memory.

Inside the function, the assignment of ret[j] is conditional. You never know for sure that which or any index element is actually initialized. After returning the pointer, you unconditionally index into the pointer any to read the values which may be very well unitialized.

In case, you are returning the pointer with the same assignment condition, you can at least use calloc() which returns 0-filled memory, so at least, you have a deterministic value. However, this will fail to differentiate between a left-over index element and an element actually having a value of 0. For better precision, you can memset() the malloc()-ed memory to some guard value which indicates that those node values are not assigned.

Also, another quickfix would be, add one else condition which basically helps to unconditionally assign a value to each element.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Type mismatch when returning an array from a function

From Dev

Issue with Returning String from Pure Function in React with Type Checking

From Dev

Returning structure containing int array from Structure Function

From Dev

Returning largest number from an array using an int function not printing value

From Dev

returning int array when return type is int *

From Java

Java "Type mismatch: cannot convert from int[][] to int" when returning copied array

From Dev

Returning an Array from a Function in C

From Dev

Returning a mutable array from a function

From Dev

Returning an array from a function in VBA

From Dev

Returning an array of pointers from a function

From Dev

Returning an array from function in nodejs

From Dev

Returning vector array from a function

From Dev

Returning an array from function in C

From Dev

Javascript returning array from the function

From Dev

VBA: returning a range from a function issue

From Dev

Issue on returning a variable from an evalScript function

From Dev

Issue with returning multiple pieces of data from a function

From Dev

Printf function who returning an array of int

From Dev

Why doesn't the Haxe compiler complain when returning a Dynamic (String) from a function with a generic (Int) return type?

From Dev

Kotlin: Returning Array<E> from function with return type Array<I> if E is enum class that implements interface I

From Dev

Correctly returning Int from Hashmap in a function

From Dev

Returning String array in a function with void return type

From Dev

returning random int from method to array

From Dev

Returning Array and assign to array from calling function

From Dev

F#: Returning a Union Type From a Function

From Dev

OpenCV: Returning Point type from a function to main

From Dev

Returning the correct child type from a function

From Dev

Problem with returning generic type from function

From Javascript

Reusable bcrypt function, returning data from function issue

Related Related

  1. 1

    Type mismatch when returning an array from a function

  2. 2

    Issue with Returning String from Pure Function in React with Type Checking

  3. 3

    Returning structure containing int array from Structure Function

  4. 4

    Returning largest number from an array using an int function not printing value

  5. 5

    returning int array when return type is int *

  6. 6

    Java "Type mismatch: cannot convert from int[][] to int" when returning copied array

  7. 7

    Returning an Array from a Function in C

  8. 8

    Returning a mutable array from a function

  9. 9

    Returning an array from a function in VBA

  10. 10

    Returning an array of pointers from a function

  11. 11

    Returning an array from function in nodejs

  12. 12

    Returning vector array from a function

  13. 13

    Returning an array from function in C

  14. 14

    Javascript returning array from the function

  15. 15

    VBA: returning a range from a function issue

  16. 16

    Issue on returning a variable from an evalScript function

  17. 17

    Issue with returning multiple pieces of data from a function

  18. 18

    Printf function who returning an array of int

  19. 19

    Why doesn't the Haxe compiler complain when returning a Dynamic (String) from a function with a generic (Int) return type?

  20. 20

    Kotlin: Returning Array<E> from function with return type Array<I> if E is enum class that implements interface I

  21. 21

    Correctly returning Int from Hashmap in a function

  22. 22

    Returning String array in a function with void return type

  23. 23

    returning random int from method to array

  24. 24

    Returning Array and assign to array from calling function

  25. 25

    F#: Returning a Union Type From a Function

  26. 26

    OpenCV: Returning Point type from a function to main

  27. 27

    Returning the correct child type from a function

  28. 28

    Problem with returning generic type from function

  29. 29

    Reusable bcrypt function, returning data from function issue

HotTag

Archive