How to allocate dynamically when I don’t know how many positions I will use in C?

Asked

Viewed 462 times

11

In a part of the code I need to transform an integer into binary and store it in a character array, however, I don’t know which integer I will receive to transform into binary, so I don’t know how many positions my vector will have char.

How to treat this problem with dynamic allocation? I need to allocate memory spaces according to the need of my program, since I will receive one int and I need to turn this int in binary, however, I do not know the values.

  • trial vetor[sizeof(int)*8], I think so of.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

8

A int will have (almost) always 32 bits, so you know the required size. Even if you don’t know it, in cases like this it’s best to allocate enough for as long as possible. It’s actually something so simple that you shouldn’t allocate dynamically. Do the allocation as vector yourself and don’t worry about it.

As the comments I made a code to at least demonstrate the functioning (as information is missing in the question, it will need adaptations):

#include <stdio.h>

int main(void) {
    int inteiro = 3;
    int tamanho = sizeof(int) * 8; //aqui acha o tamanho de um int
    char binario[tamanho + 1]; //aqui aloca o vetor de char com espaço para o terminador
    int i = tamanho - 1; //inicia o contador
    while (i > 0) {
        binario[i] = inteiro % 2 + 48; //acha o binário
        inteiro /= 2;
        i--;
    }
    binario[tamanho] = '\0'; //coloca o terminador
    printf("%s", binario);
}

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

Note that he does not consider the endianness.

  • however, if the number is 3, for example, it will be 0000.... 00011. As I will need to concatenate these char vectors further ahead, it is not cool for me to have a lot of unnecessary zeros...

  • If he’s a int in binary it will have 32 bits. I imagine you will put a bit in each position of the array. The fact that the first digits are 0 does not mean that it does not have all of them. You will only fill the last 2 positions of the array with 1 and the rest with 0.

  • 2

    Well, you’re already changing the problem. When you ask a question, you have to give details. In case you didn’t say what you were going to do with it. Still, I would do it this way. At least the way you described the problem. If you have to do something completely different from what you described, then I would probably do it differently, but I have no way of knowing, I can only talk about what’s in your description. Next time you ask a question, correctly describe everything you need, post code you’ve already done, isolate the problem and specifically show where you’re having trouble.

  • If you are going to concatenate, you will most likely need these zeroes to hold the positions in the right places, otherwise you will end up forming wrong numbers. I cannot guarantee that, but as I said, it is not in the description of the question how these cases should be treated. Actually the question isn’t even about this. It’s clearly about allocation only.

  • @bigown is the following as it is in my answer I entered with a very high number with 11 digits, and show what the sizeof returns, it returned me 8, it means 8 bytes, ie the sizeof(int) always returns 4, but there arose my doubt because the whole has a limit, There I explain better

  • @Skywalker I think you’ve already understood that a very large number will need a long and not a int which is what the AP asked for. If you’re having any doubts about the operation of this, open a question.

  • ok solved already. Thank you

Show 2 more comments

0

The @Maniero solution would solve.

But I leave mine too.

Starting from the beginning where sizeof(int) -> returns the number of bytes an integer type uses in memory, in which case it would return 4, then multiply by 8 for bits terms, and thus the result would be 32.

int tamanho = sizeof(int)*8;//sizeof de int para independer da arquitetura 
 char vetor[tamanho];

Regarding the "unnecessary" zeroes, to make a way to remove them would be a time spent at all, and if it is case to save space that yes would find unnecessary.

Browser other questions tagged

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