[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


char arrays...

Posted on 2009-02-24
Medium Priority
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

Infinity08 earned 500 total points
ID: 23721577
>> 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

ID: 31550557
Thanks that's what I thought.
LVL 39

Expert Comment

ID: 23721697
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

872 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