Difference betweeen char[] and char* = new char[]

Posted on 2009-02-18
Last Modified: 2012-05-06
I come from java background so this is confusing for me. When allocating dynamic memory in cplusplus what is the difference between allocating memory for char array using char var[40] and char* var = new char[40]?

How do I delete/free the allocated memory of char arrays in both forms?

thanks in advance.
Question by:zoollu
    LVL 53

    Accepted Solution

    >> char var[40]

    The memory is statically allocated (usually on the stack). Room for 40 characters is reserved. The size of the buffer is fixed at compile time, and cannot be modified at run time.

    The memory is free'd as soon as the buffer goes out of scope (at the closing } for the block it was defined in).

    >> char* var = new char[40]

    The memory is dynamically allocated (usually on the heap). Room for 40 characters is reserved. The size of the buffer can be decided at run time.

    The memory has to be explicitly deleted by the programmer using delete [] :

           delete [] var;

    This is done as soon as you don't need the memory block any more.

    Author Comment

    So can i allocate memory like

    char var[sizeof(newitem)+30] and not care about deleting it since its gone when out of scope?
    LVL 53

    Expert Comment

    >> char var[sizeof(newitem)+30]

    That's ok.

    >> and not care about deleting it since its gone when out of scope?


    But you won't be able to change the size of the buffer at run time. It's fixed at compile time to sizeof(newitem)+30 chars.

    Author Closing Comment


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now