Dynamic memory allocation gives error while accessing

Asked

Viewed 59 times

-1

I have a problem with my code:

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

void a(void *po, size_t t)
{
    void *r = realloc(po, t);
}


int main()
{
    uint8_t *array = NULL;

    a(array, sizeof(uint8_t) * 2);

    array[0] = 1;
    array[1] = 3;

    return 0;
}

When trying to insert data into the array this error appears:

 segmentation fault
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

3

The first problem is that you are relocating the object and playing on the variable r who dies there. It seems he wants to play in po even to reflect on array in the calling function.

But then we find another problem, you don’t want to pass the object but the pointer to the object, so you must pass &array to grab a pointer to array, which happens to be a pointer. And of course, the parameter should receive a pointer to what it will receive, so it should be a pointer to another pointer. And the right guy over there is uint8_t.

Fixing these problems you pass a memory address to a function, it will be changed to another address and as it went by reference it is changed in the variable that served as argument, which is what you want.

I hope this is just to demonstrate the mechanism because in real code it doesn’t make much sense to do this.

The way it was nothing was changed into array that kept pointing to null.

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

void a(uint8_t **po, size_t t) {
    *po = realloc(*po, t);
}

int main() {
    uint8_t *array = NULL;
    a(&array, sizeof(uint8_t) * 2);
    array[0] = 1;
    array[1] = 3;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • I think that line *po = realloc(*po, t) can give conversion error, can confirm?

  • What a mistake it can make?

  • i tried here https://ideone.com/JiFIpz, here http://cpp.sh/ and here https://www.mycompiler.io/new/cpp and gives the error invalid conversion from 'void*' to 'uint8_t* {aka unsigned char*}' [-fpermissive], if you test the code you should be able to replicate this build error

  • https://ideone.com/OiEJjG (the issue is about C and not C++) https://answall.com/q/285379/101 and https://stackoverflow.com/q/605845/221800

  • humm already seen what the difference Maniero, the two code are equal in the 2 ideone links, but one was compiled with "language: C99 (gcc 8.3)", its link that works, and the other was compiled with "language:C++ (gcc 8.3)", it seems that does not work with c++ or some other compiler feature that I can’t say

  • 2

    C and C++ are different languages, contrary to popular belief.

  • yes, even in a seemingly simple code the +1 difference is evident

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.