How to see how much memory occupies such a variable in C++? And how to use the define?

Asked

Viewed 2,379 times

5

How to see how much memory occupies such a type variable int, char e long in the C++?

And how to use the #define?

  • 3

    I think you’re mixing two completely different subjects into one topic. Don’t you think you should create two different questions?

  • 2

    I agree with @Math, the ideal is to ask separate questions. I answered about the define because the best answer is p/not to use it in C++ but each subject should be in a question.

3 answers

6


The #define should not be used in C++ (actually not even in modern C), use a constant with the keyword const. Anyway I showed an example of #define.

I made an example in the C++ patterns showing the size of the variables as you requested. Note that the sizeof is an operator, he even needs the parentheses in some situations. This operator takes the size of the data used as operand, in the case of the variable, at the time of compilation, so it has no cost at runtime.

In general C++ does not use legacy C libraries.

#include <iostream>
#define tamanho 10
const int elementos = 10;
using namespace std;

int main() {
    int a = 1;
    char b = 'a';
    float c = 1.0f;
    double d = 2.0d;
    short e = 1000;
    long f = 100000;
    int *g = new int[tamanho];
    int h[elementos];
    cout << "a tem " << sizeof(a) << " bytes\n";
    cout << "b tem " << sizeof(b) << " bytes\n";
    cout << "c tem " << sizeof(c) << " bytes\n";
    cout << "d tem " << sizeof(d) << " bytes\n";
    cout << "e tem " << sizeof(e) << " bytes\n";
    cout << "f tem " << sizeof(f) << " bytes\n";
    cout << "g tem " << sizeof(g) << " bytes\n";
    cout << "h tem " << sizeof(h) << " bytes\n";
}

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

Note that dynamically allocating data can cause problems in determining the size of the allocation. You would have to have other means of finding the allocated size. But also note that in this case the size of the variable really is the size of the pointer. The result is not wrong. The variable itself occupies 4 bytes on 32-bit architectures. What the pointer points to is that it will have another extra size, shows in the variable h (static allocation) that was not allocated with a pointer as it was in g (dynamic allocation).

Learn more about the difference between allocation in stack and in the heap.

  • 1

    Here is a good example of what can occur by these confusions of using sizeof with unexpected result: http://answall.com/questions/48055/aloca%C3%A7%C3%A3o-em-c-Object-was-probably-modified-after-being-Freed/48107

  • 1

    @Augustovasques solved, one of the things I don’t like about C/C++ is that compilers practically create different languages and each uses different flags at different times, and there I have no control over it.

1

You can find out how many bytes a certain variable occupies through the function sizeof().

This function takes a variable as argument, or the reserved words that represent the variables: char, int, float etc.

Example:

printf("Char: %d bytes\n", sizeof(char));
printf("Int: %d bytes\n", sizeof(int));
printf("Float: %d bytes\n", sizeof(float));
printf("Double: %d bytes\n", sizeof(double));

Don’t forget to include the library stdio.h

Source

To use the define is quite simple:

#define CONTADOR 0

So just use the name of define in the code.

Example:

#define CONTADOR 10
int main() {
  int vetor[CONTADOR]; //Vetor com CONTADOR posições
}
  • 2

    sizeof is not a function, is an operator solved at compile time.

1

define it works like this:

#define TAMMAX 100

first comes the defines then the name and what it contains in that name, when the compilation is done all that is with the name TAMMAX in its code and replaced by 100, for example:

int vetor[TAMMAX] // vetor de 100 indices

Browser other questions tagged

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