-2

This is task;

Define a function that accepts two integer parameters. The function returns the pointer to the integer value that it will dynamically allocate. Implement a function to dynamically allocate memory for a integer variable if the arguments values are the same. The function when allocating should initialize the variable to 0. If arguments do not have the same values, the memory is not allocated and the function returns NULL.

I create my code but i think that it's not good:

#include <stdio.h>
#include <stdlib.h>
    
int* funk(int a, int b ){
    int *p;
    
    if(a == b){
    
        p = (int*)calloc(a,sizeof(int));
    
        return p;
    
    }
    else{
        return NULL;
    }
    
}
    
int main(void){
    
    int *p = NULL;
    
    p = funk(4 , 4);
    
    printf("%d", *p);
    
    free(p);
    
    return 0;
    
}

So, can somebody check this code and help me?

Murgalha
  • 368
  • 3
  • 7
  • You're reserving space for **`a`** integers, but the assignment just tells to allocate space for **one**. – Antti Haapala -- Слава Україні Jul 05 '20 at 17:27
  • i need code, and a is one integer, right? – Domagoj 954 Jul 05 '20 at 17:31
  • No, you're allocating space for `a`, where `a` is the number of, in this case **4**, integers but the assignment asks to allocate space for **one**. – Antti Haapala -- Слава Україні Jul 05 '20 at 17:32
  • Read the documentation for `calloc` and also indent your code properly before posting – klutt Jul 05 '20 at 17:32
  • It should be `p = calloc(1, sizeof(int));` . To allocate space for only 1 integer. – isrnick Jul 05 '20 at 17:32
  • i do this but: Segmentation fault (core dumped) – Domagoj 954 Jul 05 '20 at 17:35
  • when i have for exmple 4 and 4 they return 0 – Domagoj 954 Jul 05 '20 at 17:37
  • but if i have for example 4 and 2 they return: Segmentation fault (core dumped) – Domagoj 954 Jul 05 '20 at 17:37
  • 2
    That is because you are trying to access a value that doesn't exist. When `a` and `b` are different (such as 4 and 2) `funk` returns NULL, which is assigned to `p`, and then you try to print an integer using this NULL pointer which results in segmentation fault. – isrnick Jul 05 '20 at 17:41
  • So this is OK or it's not, sorry but im new in this – Domagoj 954 Jul 05 '20 at 17:43
  • You can't do `*p` when `p == NULL`, because in this case it doesn't point anywhere, you should first test that `p` is not equal to `NULL` and only call `printf("%d", *p);` in that case. – isrnick Jul 05 '20 at 17:46
  • i need full code – Domagoj 954 Jul 05 '20 at 17:47
  • because then i will understand – Domagoj 954 Jul 05 '20 at 17:49
  • "i need full code because then i will understand". I doubt that. To understand you need to experiment yourself. Try https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and https://ericlippert.com/2014/03/21/find-a-simpler-problem/ For getting help on StackOverflow you need [tour], [ask] and [mre]. Many people here are convinced that giving you working code for your assignment does not help you at all. They would however gladly help you according to the compromise described here https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – Yunnosch Jul 05 '20 at 17:54
  • This, as most questions which give a strong impression to ask for code to hand in for a homework (baited with some non-working code), lacks focus on the specific programming problem you encountered. "i think that it's not good" is not such a specific problem. Please elaborate what makes you doubt your code. – Yunnosch Jul 05 '20 at 17:56

1 Answers1

0

So you know, this is a very contrived exercise. You normally don't have to allocate memory for a single integer. But functions that sometimes return a pointer and sometimes returns NULL are a real problem.

In C, you often need to be guarding that a function failed to return a valid value. In this case, you need to check that funk didn't return NULL before trying to print and free it. Trying to use a NULL pointer is undefined behavior and you risk a memory violation such as a segmentation fault.

    int *p = funk(4 , 4);
    if( p != NULL ) {
        printf("p = %d\n", *p);
        free(p);
    }
    else {
        puts("Give us the funk");
    }

Additionally, you only want to allocate a single integer, so pass 1 as the number of units to calloc. And don't cast calloc nor malloc, it's unnecessary clutter. C will do the cast for you.

    // Allocate space for 1 integer, initialize it to 0, and assign it to p.
    p = calloc(1, sizeof(int));
Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Gentlemans, i need full code of my task, i don't understand dynamic allocation, i learn about this but i don't know solve this – Domagoj 954 Jul 05 '20 at 18:10
  • But if somebody want, put your full code here that i can compare – Domagoj 954 Jul 05 '20 at 18:18
  • @Domagoj954 As this appears to be homework, I am not posting a complete program to ensure you understand the answer and aren't simply copying it. If there's something you don't understand, ask about it, and we'll see if we can help. – Schwern Jul 05 '20 at 18:35