Jump to content
  • Advertisement
Sign in to follow this  
lougv22

When to use regular array and when array of pointers?

This topic is 3031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is a very basic question, but still I don't seem to have a good answer for it. When should I use a regular array, say something like:

char tiles1[] = {"a", "b", "c", "d"};

and when to use an array of pointers, such as:

char* tiles1[] = {"a", "b", "c", "d"};

What are the advantages and disadvantages of each?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by lougv22
What are the advantages and disadvantages of each?


As DevFred said, the first won't compile. Anything between double quotes "" is of type const char*, anything between single quotes ''' is of type char and can only be one character (not a string as opposed to double quotes).

This array is on the stack, so if you have a number of large data files, you'll want those on the heap and store the pointers to the heap (heap is when you allocate with malloc / new). Pointers also work if you want your array to be filled with different element sizes. Strings are a good example of data type that vary in size, therefore we store a pointer to them:
const char *array[] = {"123", "12345"}; // reminder: should be const

If you know this length in advance, you can make a matrix / table / 2D array:
char array[][4] = {"abc", "123"}; // reminder: strlen + 1 for string terminating 0x00

In C++, using pointers also allows you to have an heterogeneous array. This means you can store different classes (but with the same base class) in one array.

In general, storing values is easiest and most desired. But you often cannot because the elements differ in length or the data must be on the heap. In those cases you use pointers.

Share this post


Link to post
Share on other sites
Quote:
Original post by lougv22
This is a very basic question, but still I don't seem to have a good answer for it. When should I use a regular array, say something like:

char tiles1[] = {"a", "b", "c", "d"};

and when to use an array of pointers, such as:

char* tiles1[] = {"a", "b", "c", "d"};

What are the advantages and disadvantages of each?


Neither.


//char tiles1[] = {"a", "b", "c", "d"};
std::vector<char> tiles1;

//char* tiles1[] = {"a", "b", "c", "d"};
std::vector<std::string> tiles2;


In general, prefer the SC++L over arrays.
In general, use vector as your default container unless you have a reason not to.
In general, prefer containing objects over containing pointers.
Exception, if you need polymorphic behaviour, use container of pointer to base (and either remember to clean up after yourself or use something like boost.ptr_vector)

Those are general and vague guidelines. Like everything in life there are numerous and subtle exceptions to those rules. Experience will eventually teach you the exceptions. For now, stick to those rules until you can't.

Share this post


Link to post
Share on other sites
It could be he is talking about C, in which there is no std::vector of course. I too barely have those arrays in my code, unless it's small and static.

Share this post


Link to post
Share on other sites
Quote:
Original post by Decrius
It could be he is talking about C, in which there is no std::vector of course. I too barely have those arrays in my code, unless it's small and static.


In general, prefer C++ over C [grin].

Share this post


Link to post
Share on other sites
Sorry for the late reply. This should actually be single quotes like so:

char tiles1[] = {'a', 'b', 'c', 'd'};

And also, using STL is not an option, it has to be done in "straight" C++ or C. It's for a programming exercise from a job interview test.

Share this post


Link to post
Share on other sites
For chars, there is really no reason to use an array of pointers. Chars are going to be 1 byte. Pointers to chars, on a modern OS, can be as large as 8 bytes. However, lets say we are talking about larger objects. In C++, one advantage to pointers is that they allow polymorphism, as using a traditional array of objects could cause slicing. In C and C++, pointers can point to objects which are dynamically allocated (malloc in C, new in C++). Pointers also make it more efficient to swapping objects in and out of the array, because they do not need to be copied from different places in memory. Also, in C++, copying an object into an array initiates the copy constructor, which can be bad and extremely costly at times.

Share this post


Link to post
Share on other sites
Quote:
Original post by lougv22
And also, using STL is not an option, it has to be done in "straight" C++ or C. It's for a programming exercise from a job interview test.


Who said anything about the STL? The STL has basically not existed since the mid-90s.

We are talking about the SC++L, as ChaosEngine was very careful to spell it.

I'm going to break that down for you: Standard C++ Library.

You know, "the standard library". The code that comes built right into the language, that is guaranteed by the language specification to be there.

There is no reasonable argument by which this is "not 'straight'" C++.

If you do not allow yourself to use std::vector in C++ for "purity" reasons, then by the same argument you cannot allow yourself to do any I/O, since all of that functionality is also provided by the standard library. By the same token, you would not allow yourself to do any kind of dynamic memory allocation in C, since malloc() et. al. (and even alloca(), in case you thought it was just a problem with heap memory) come from the standard C library. Oh, and I hope you like calculating the length of C-strings by hand.

By the way: if a company apparently thinks that asking you to solve these sorts of problems "by hand" is somehow "interesting" or a particularly good way of testing your skill as a programmer - or even your understanding of implementation details - then I would take that as a big warning sign that this is not a place I want to work.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!