-3

When creating a pointer using new keyword or <stdlib.h>'s malloc, and put its size 0; what ever is the index number it doesnt give an error and works. ( i am not sure if this works if the size bigger then 0, because some times when i allocate a memory block and place an element outside its range program crashes).

My question : is this a c++ thing? Or just from my compiler? Is it safe to use it for arrays i don't want to limit it with specific size. Because i am thinking to use this in my game to make unlimited world generation and save it ( Not all terrain just the breakable and placable objects)

#include <iostream>

using namespace std;

int main() {
    
    int* x = new int[0];
    
    x[100] = 0;
    
    cout << x[100];
    
    return 0;
}
Star Dev
  • 59
  • 4
  • No, this is not safe. You have found yourself in undefined behavior land and that is a scary place. Unless you really want to learn about pointers, use a `std::vector` if you want an array in which you don't know how many elements you want to put in it. I would recommend you do a readthrough of a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Apr 20 '22 at 19:30
  • C++ assumes that programmers mean what they say. If they tell the computer to do something stupid, most of the time the compiler will go "Yes, boss!" and generate code that does it. – user4581301 Apr 20 '22 at 19:31
  • "I used a rope that is rated to hold 500 pounds to hold up a 600 pound item. The rope didn't break. Is this safe?" – PaulMcKenzie Apr 20 '22 at 19:31
  • 1
    *Because i am thinking to use this in my game to make unlimited world generation and save it* -- Use `std::vector` instead of `int *` and `new[whatever]`. – PaulMcKenzie Apr 20 '22 at 19:33
  • Added bonus to `vector`: It has a method `at` that will test and throw an exception if you access out of range. Why doesn't C++ always check? Because it would have to also check all of the accesses that weren't out of range and slow the program down with all those unnecessary checks. General Rule of Thumb: If an action has a cost, C++ won't do it automatically unless the documentation states that it does. – user4581301 Apr 20 '22 at 19:35
  • But if i use std::vector i must resize the array every time i generate a new chunk this will be very laggy, it will be laggier when teleporting because i need to resize more space in my vector – Star Dev Apr 20 '22 at 19:37
  • 2
    @StarDev `vector` grows geometrically. Generally once it runs out of space, it will allocate a new buffer that is 1.5-2 times the size of the old buffer. This gives you an amortized constant growth. You could also stop quite a few reallocations if you start off the vector with enough space to hold the typical amount of objects the game will produce. – NathanOliver Apr 20 '22 at 19:39
  • of course you need to resize when you add an element, but you do not need to reallocate each time you add an element. – 463035818_is_not_an_ai Apr 20 '22 at 19:40
  • Also note that to get `vector` to automatically manage its storage, you need to use `vector`'s insertion methods, and not the `[]` operator or `at`. – user4581301 Apr 20 '22 at 19:42

1 Answers1

2

is this a c++ thing? Or just from my compiler?

It's a bug in your program. The behaviour of the program is undefined.

Is it safe to use it

It is not safe. The program is broken.

eerorika
  • 232,697
  • 12
  • 197
  • 326