0

I want to initialize array of c-strings with zero pointers in MSVC2010

// Foo.h
#pragma once
class Foo {
  int sz_;
  char **arr_; 
public:
  Foo();
  ~Foo();
  // ... some other functions
};

// Foo.cpp
#include "Foo.h"
#define INITIAL_SZ 20

Foo::Foo() : sz_(INITIAL_SZ) {
  // there I have to initialize arr_ (dynamic array and can be enlarged later)
  arr_ = (char **)calloc(INITIAL_SZ * sizeof (char *)); // ??? 
  // or maybe arr_ = new ...
}

How to correct initialize arr_? I was not allowed to use of STL, MFC, etc.

3
  • Just curious. Why no STL? And also what have you tried so far? Actually what you have there calloc() will initialize everything to zeros (NULL). Commented May 26, 2011 at 17:38
  • 4
    If not for the class bit, I would have tagged this C... Commented May 26, 2011 at 17:39
  • @yasouser - It was a requirement of the customer Commented May 28, 2011 at 15:35

5 Answers 5

5

arr = new char*[INITIAL_SZ](); will do - you can even put it in an initialization list.

Sign up to request clarification or add additional context in comments.

Comments

4

If you really want to avoid STL, etc., then why not:

arr_ = new char*[INITIAL_SZ]();

You could even put this in the initializer list.

Remember to invoke delete [] arr_ in your destructor. (As @Nawaz points out below, you should probably also follow the Rule of Three, and define a suitable copy-constructor and assignment operator as well.)

2 Comments

I don't think it initializes them to zero, does it? (I'm not perfectly sure of the initialization rules, but I think the extra parentheses are needed)
And also define copy-ctor and assignment operator : rule of three!
2

1. Build a proper string class

2. Build a proper array class

3. Use the array on strings

Happy chasing memory leaks, double frees and memory corruption.

3 Comments

Yes, except you didn't answer his question. "I was not allowed to use of STL, MFC, etc." Clearly he's following some specific guidelines, so your answer is completely unhelpful, if not a little stuck-up.
@leetNightshade: it's definitely stuck up, and meant as a taunt. However not using STL or MFC does not mean that you cannot create your own classes for memory management. My answer merely suggest to decompose the issue (String + Array) instead of trying to solve it all at once. And I know that the classes will be imperfect, experts get them wrong.
+1 a little extra work up front to avoid hours of painful string management and debugging by hand.
1
arr_ = (char **)calloc(INITIAL_SZ * sizeof (char *));

should be

arr_ = (char **)calloc(INITIAL_SZ, sizeof (char *));

Comments

1

The correct way is to redefine arr_ as std::vector<std::string> and to use vector::reserve() to hint at the number of strings you expect to have. Let C++ take care of the memory for you.

But if you must use raw C strings, you probably want:

arr_ = new char *[sz_];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.