char arrays...

Posted on 2009-02-24
Last Modified: 2012-05-06

I'm just wondering, at function level if I do the below, do I need to destroy the char array sString at the end?

void SomeVoid( )


	char sString[500] = "";

	Q_snprintf( sString, sizeof( sString ), "%s", "Some String" );


Open in new window

Question by:c0ldfyr3
    LVL 53

    Accepted Solution

    >> do I need to destroy the char array sString at the end?

    When the function ends, the (local) char array is also removed. You don't have to remove it explicitly.
    LVL 10

    Author Closing Comment

    Thanks that's what I thought.
    LVL 39

    Expert Comment

    To add to above:

    You only have to destroy (delete) if you allocated with new operator.

    You should try to have new/delete always with in the same scope.

    void someFunc()
        char* psz = new char[1000];
        // do somewhat with psz

        delete []psz;

    If you do the new in the constructor or member function of a class and the pointer should be valid beyond the current scope, store it in a pointer member of the class and delete it in the destructor.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    728 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