Why doesn’t array declaration guarantee proper memory allocation whereas malloc does? [closed]

In C++,
when I declare an array like int array[10];,
it sometimes uses memory assigned to other variable where as this problem is non-existent when I use an integer pointer and malloc to allocate memory to it. Why does this happen?

4

So I think you’re working from a false premise.

I believe you assume that if there are non-zero values in the memory space that was just allocated that the memory was somehow not properly allocated. And that’s a horrific assumption that’s going to burn you badly later on as you build more complex programs. Suffice it to say that the memory is being properly allocated, but you’re misinterpreting what you got back.

Neither C nor C++ guarantee that allocated memory will be initialized. Initialized memory (and variables) is memory or variables that are set to a specific value (typically 0, “”, or false) when the variable is declared or the memory is allocated.

So this is where I think you have a false premise. I believe you’re looking at the contents of the recently allocated variable; seeing non-zero values; and assuming the allocation went wrong.

Complicating your observations is the two approaches you are using to allocate memory.

int myArray[10];  // allocates on stack

int *myPointer;
myPointer = malloc(10*sizeof(int));  // allocates on heap

As pointed out in the comments, one approach allocates on the stack and the other allocates from the heap.

Allocating from the stack will be more likely to reuse memory space that’s been used and released! by another area so the memory won’t show up as initialized.

Allocating from the heap is probably less likely to reuse space that’s already been used and released as it’s a larger area of memory to pull from. And there’s a distinctly possibility that you’re forgetting to free your memory after allocating it, so you wouldn’t run into any reuse cases.

Regardless of all that, the correct thing to do in C and C++ is to initialize all memory and variables prior to using it. Get into the habit of immediately calling memset after you allocate your memory.

4

This looks like a Stack Overflow (hehe).

When you allocate on the stack, there are other entities present surrounding the allocated array (in the memory with an address immediately larger than the array). Since you said in your comment “after few iterations the value of n suddenly changes to something garbage” I assume that n is declared in your code after the array:

int array[10];
...
int n;

Not before the array:

int n;
...
int array[10];

This doesn’t look like a “proper memory allocation” issue, but accessing the array with an invalid index, and writing in overflow. I think your memory corruption is present when you use malloc as well, but since you do not have memory allocated right after the array (when using malloc), the memory corruption affects areas of unallocated memory (and this is not visible to you).

Here’s an example of overflow (and a wild guess on my part):

You say you allocate array of n*n elements and iterate from 0 to n*n - 1. Are you sure? If you use two imbricated for loops, do not do this:

for(int i = 0; i <=n; ++i) // this is incorrect
    for(int j = 0; j < n; ++j)
        // access at i, j

This doesn’t iterate n*n, but [(n+1) * n] - 1 (and would cause an overflow).

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật