Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,845 questions

55,674 answers

573 users

How to allocate 1MB in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>

int main() {
    size_t bytes = 1024 * 1024; // 1MB = 1,048,576 bytes
    char *buffer = (char *)malloc(bytes);

    if (buffer == NULL) {
        // Check if allocation failed
        printf("Memory allocation failed!\n");
        return 1;
    }

    // Use the allocated memory (e.g., initialize it)
    for (size_t i = 0; i < bytes; i++) {
        buffer[i] = 0; // Initialize all bytes to 0
    }

    printf("Memory allocated and initialized successfully.\n");

    // Free the allocated memory
    free(buffer);

    return 0;
}



/*
run:

Memory allocated and initialized successfully.

*/

 



answered May 19, 2025 by avibootz

Related questions

2 answers 295 views
1 answer 127 views
1 answer 126 views
2 answers 290 views
2 answers 461 views
2 answers 227 views
227 views asked May 19, 2025 by avibootz
3 answers 303 views
...