1

I don't know any C++ at all but I am trying to make a very small update to a C++ library that my application is using. Before I start hacking away at this, I am hoping someone can tell me the proper syntax for the following:

I have the following lines of code:

#define A_NUMBER 100
#define ANOTHER_NUMBER 150

enum {
   type1,
   type2,
 };

static int someMethod(int type)

{
     char command[A_NUMBER];
     //...more code
}

What I need to be able to do is based on the type argument (type1 or type2) I need to be able to set the size of the array to be either A_NUMBER or ANOTHER_NUMBER.

In pseudo code it would be something like:

if (type == type1) {
    char command [A_NUMBER]
}
else if (type == type2) {
    char command [ANOTHER_NUMBER]
} 

Is there a way to dynamically define the size?

5 Answers 5

5

Yes, you can use an std::vector<char>:

if (type == type1) {
    std::vector<char> x(A_NUMBER);
} else if (type == type2) {
    std::vector<char> x(ANOTHER_NUMBER);
} 

Remember to include the header with:

#include <vector>

While your example code matches the "pseudo code" in the question, I think part of the question is how to decide the size via type and then use the resulting storage unconditionally, i.e. outside the conditional blocks.

Then it gets as simple as:

std::vector<char> x;
if (type == type1) {
    x.resize(A_NUMBER);
} else if (type == type2) {
    x.resize(ANOTHER_NUMBER);
} 
Sign up to request clarification or add additional context in comments.

3 Comments

While your example code matches the "pseudo code" in the question, I think part of the question is how to decide the size via type and then use the resulting storage unconditionally, i.e. outside the conditional blocks.
@delnan, can you please see if the edit match what you meant?
It does, except that you probably mean .resize(), not .reserve().
1

I believe this is what you want

std::vector<char> x; // x is empty, with size 0

if (type == type1) {
    x.resize(A_NUMBER); // change size to A_NUMBER
} else if (type == type2) {
    x.resize(ANOTHER_NUMBER); // change size to ANOTHER_NUMBER
} 

Comments

0

Yes and no. In standard C++, you cannot keep the array on the stack and have its size determined in runtime.

However, you can turn the array into a dynamically-allocated one (i.e. on the heap). In C++, the standard way to do this is to use std::vector:

std::vector<char> command(A_NUMBER); // or ANOTHER_NUMBER

Indexing will work just as before: command[5]

However, if you need to pass the array to something which expects a C-style array (i.e. a char *), you'll have to use one of these:

command.data();  // if your compiler supports C++11

&command[0];  // if it does not

And of course, to use std::vector, you'll have to #include <vector>.

Comments

0

Here's an example that works in C and C++:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char *argv[])
{
    char *opt;
    if(argc == 2) {
        opt = *++argv;
    }
    else {
        printf("Usage: %s [small|large]\n", *argv);
        return EXIT_FAILURE;
    }

    int *arr;
    int arrlen = 0;
    if (strcmp(opt, "small") == 0) {
        arrlen = 3;
        arr = (int *) malloc(arrlen*sizeof(int));
        int i;
        for(i = 0; i < arrlen; i++)
            arr[i] = i+1; 
    }
    else if (strcmp(opt, "large") == 0) {
        arrlen = 5;
        arr = (int *) malloc(arrlen*sizeof(int));
        int i;
        for(i = 0; i < arrlen; i++)
            arr[i] = i+1; 
    }
    if (arrlen > 0) {
        int i;
        for(i = 0; arr[i]; i++)
            printf("%i, ", arr[i]);
        printf("\n");
        free(arr);
    }
    return EXIT_SUCCESS;
}

Example:

[gyeh@gyeh stackoverflow]$ ./dynarr
Usage: ./dynarr [small|large]
[gyeh@gyeh stackoverflow]$ ./dynarr small
1, 2, 3, 
[gyeh@gyeh stackoverflow]$ ./dynarr large
1, 2, 3, 4, 5, 

Comments

-1

The raw C++ way is new and delete

char * command = new char[A_NUMBER];
// and later delete it like this
delete[] command;

Of course you'll have to manage the memory, and it is not recommended to use this approach because of many reasons you should be able to find online. So in conclusion... don't use this method if vector is an option

If using a big array the best way would be to use C++ vector, you could even consider other data structures like list based on your needs (for example a lot of insert, deletions operations on your array).

21 Comments

Please, sir, do not ever suggest using naked pointers. Especially to a person who is not experienced with C++.
You're not technically wrong, due to the "raw" adjective, this is still a terrible way and shouldn't even be discussed at length, let alone recommended.
I do see both your points... but this answers the basic question "Is there a way to dynamically define the size?" To bash on me like I did a sin is just denying knowledge... Naked pointers are still an important subject to learn
It's a bad decision, bad to do, and you're bad for suggesting it. It's not correct.
The goal is not simply to answer questions, but to answer them with best practices and modern approaches in mind. If someone Google's this question and sees this answer, it will perpetuate raw pointer style, which some would go so far as to call deprecated in modern C++.
|

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.