Pointers as a function return and use malloc

I use pointers as the function returns. Below is a simple snippet of code

The main function:

void main()
{
    int a = 10, b = 20;
    int *ptr;
    ptr = add(&a, &b);
    printf("sum of a and b is %d\n", *ptr);
}

add function:

int* add(int *a, int *b)
{
    int c;
    c = *(a)+*(b);
    return &c;
}

This works correctly and gives me a yield of 30 ..

But if you add one more function printhelloworld();before adding as below

void main()
{
    int a = 10, b = 20;
    int *ptr;
    ptr = add(&a, &b);

    printhelloworld();--this just prints hello world

    printf("sum of a and b is %d\n", *ptr);
}

will not be more than 30, and this is undefined due to the release of the stack frame. I need to change my program as shown below usingmalloc()

int* add(int *a, int *b)
{    
    int* c = (int*)malloc(sizeof(int));
    *c = *(a)+*(b);
    return c;
}

It works.

But if I do not free the memory allocated in the heap, will it not remain forever? Should I use free()as below?

free(c);

If I use free()mainly, it cdoes not fall within the scope, and it will not work, if I use "free" in the add, I get the result undefined again.

ASK:
free()

free(c);

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

void printhelloworld()
{

    printf("hello world\n");
}

int* add(int *a, int *b)
{

    int* c = (int*)malloc(sizeof(int));

    *c = *(a)+*(b);
    //free(c);
    return c;
}


int main()
{

    int a = 10, b = 20;

    int *ptr;
    ptr =(int*) malloc(sizeof(int));
    ptr = add(&a, &b);

printhelloworld();
printf("sum of a and b is %d\n", *ptr);

}
+4
5
int* add(int *a, int *b)
{
    int c;
    c = *(a)+*(b);
    return &c;
}

30..

, : c - , , , . , , , , add, . , printhelloworld, , - .

, , . , . C undefined.

, malloc . , malloc, , free. free , , add: free main. malloc free - .

free - , malloc. c, c add, , free, - , add. ptr main , c free. , , .

int* add(int *a, int *b)
{    
    int* c = malloc(sizeof(int));  // no need for a cast here
    printf("The pointer is %p\n", (void*)c);
    if (c == NULL) { // Abort the program if the allocation failed
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    *c = *(a)+*(b);
    return c;
}

int main()
{
    int a = 10, b = 20;
    int *ptr;
    ptr = add(&a, &b);
    printf("The pointer is still %p\n", (void*)ptr);
    // ...
    printf("sum of a and b is %d\n", *ptr);
    free(ptr); // After this line, you aren't allowed to use the value of ptr any more
    ptr = NULL; // This is not necessary, but it good practice to ensure that you won't accidentally use the value
}
+5

, int .

, , .

add , . , add :

void add(int *a, int *b, int *c)
{    
    *c = *(a)+*(b);
}

:

int c;
add(&a, &b, &c);

, . IMO - add.

+5

free() , c , , "free" , undefined.

c add() ; malloc'ed main ptr. main():

free(ptr);

, free(), , (malloc/realloc/calloc ..) - .

+4

, c , free() , .

+1
void main()

main shpould int. , main:

int main(void)
int main(int argc, char** argv)

.

return &c;

Returns the address of a local variable undefined . Implementation allows you to do anything with this program, including making it work on the 17th of every month.

What is the correct way to use free () in my case

The right thing is not to use pointers when it is not needed, but if you use a pointer, then release it in the function mainas follows:

ptr = add(&a, &b);
...  
free(ptr);
0
source

Source: https://habr.com/ru/post/1685828/


All Articles