Declaring a pointer to a struct in C ++ automatically allocates memory for its members. Am I mistaken?

I wrote the following code snippet, and I thought it would work if I tried to access structural elements for which I did not even allocate memory. But I was completely surprised that C ++ automatically allocated memory for the structure. Is this normal behavior? For comparison, if you declare a pointer to an object, and then try to access any members without actually creating the object using the "new" operator, the program will fail. I'm just wondering why this works when I think it shouldn't be.

This is my program:

#include <stdio.h> struct Produto { int codigo; float preco; }; int main() { struct Produto* sabonete; sabonete->codigo = 654321; sabonete->preco = 0.85; printf( "Codigo = %i\n", sabonete->codigo ); printf( "Preco = R$ %.2f\n", sabonete->preco ); return 0; } 

OS: Windows 7
Compiler: MinGW GCC 4.6.1

+4
source share
5 answers

C ++ does not automatically allocate memory; the pointer contains an arbitrary value that just turned out to be a valid address in the memory of your program memory, so you did not get segfault. Your program exhibits undefined behavior, and it may not work the next time it starts.

Undefined behavior does not guarantee failure, so it is called undefined.

+11
source

You Have Undefined Behavior

on line 2 it is undefined what sabonet points to. It may indicate a memory that will not break anything if you change it, or it may indicate a memory that launches a nuclear weapon when you change it.

C ++ does not allocate memory for you at all, you just stomp on a memory that does not interrupt anything.

+4
source

No, the exact opposite. This pointer is not initialized, and you call UB by dereferencing it. If you want the pointer to be initialized, either using the existing pointer, or by calling new (or, even better, since this is C ++, some object that works as a container for handling allocation / destruction, i.e. RAII).

By definition, undefined behavior means that anything can happen. Of course, it can work sometimes because of the memory layout, no matter what, but there is no guarantee that it will always be, and you should never write such code.

+2
source

Since the g ++ compiler does not initialize an unallocated pointer to NULL, it may happen that it points to some part in memory that contains data that looks right at first glance.

To answer the question, yes, this is normal undefined behavior when you don't want to allocate memory for your objects :)

0
source

If you are interested in what could have happened under the hood, consider that sabonete on your platform is 4 bytes wide (as a pointer), and since your main object did not declare parameters (that the OS should match anyway), it may take one and the same memory space, which is usually in the reverse order of parameters.

That's right, it overwrites char ** argv, which points to an array of pointers (it must contain at least 16 of them), which points to lines containing parts of the command line.

In fact, you assign your values ​​by writing them to this array, and since it belongs to your program, the OS does not complain. And since you never refer to original values, and no one changes them, everything looks fine.

But this is not so, because it is normal. This is because you are lucky that you are using the right place for something else that you are not interested in.

0
source

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


All Articles