deividaspetraitis
deividaspetraitis

Reputation: 379

A dynamic size array in C

I have encountered with interesting situation when initializing dynamic size variables.

For example:

// getInput() is some magic method to retrieve input with dynamic length string
// some times it can be 10, some times 99 and so on
char vector[strlen(getInput())] = getInput();

In this case definitely wont work since compiler can't allocate some fixed size memory to heap, that's right?

But in this case, it works fine:

char path[] = "";
strcpy(path, getInput());

Why it doesn't work in first case and works in second? Is maybe strcpy uses malloc or something?

Upvotes: 3

Views: 2066

Answers (3)

Lundin
Lundin

Reputation: 214780

char vector[strlen(getInput())] = getInput();

Calling the getInput() function twice in the same expression doesn't make any sense. In particular, you don't copy strings with the = operator but with strcpy(). Also, you need to allocate space for the null terminator.

Assuming these are local variables (they should be), what you should do instead is this:

int main (void)
{
  const char* input = getInput();
  char vector[strlen(input) + 1];
  strcpy(vector, input);
  ...
}

But in this case, it works fine:

char path[] = "";
strcpy(path, getInput());

No, it doesn't work fine! All you did was do declare a static array of size 1 (size of the null terminator), then you copy data of longer length into that array. This causes an array out of bounds bug which is undefined behavior, anything can happen. Unfortunately, it caused your program to seem to work ok, while it actually has a latent severe bug.

Upvotes: 12

Khaled.K
Khaled.K

Reputation: 5960

char vector[strlen(getInput())] = getInput();

You are mixing a char array initialization expected by char vector[strlen(getInput())] = with the assignment of a array pointer returned by getInput().

Possible Solutions

You can either initialize the array with values

char vector[strlen(getInput())] = { 'a', 'b', .. 'z' };

Or obtain the array pointer returned by getInput

const char * vector = getInput();

Or copy the array returned by getInput into vector array

const char * input = getInput();
const size_t size = strlen(input);
char vector [size+1] = { 0 };
memset(vector , '\0', sizeof(vector));
strcpy(vector,input);

Upvotes: 1

August Karlstrom
August Karlstrom

Reputation: 11377

It seems like the function getInput returns a character pointer, so you cannot assign the result to an array. Also in

char path[] = "";

the length of path is only one character (the null character), so copying the input to this variable is only valid if the input is the empty string.

You probably want something like this:

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

#define NEW_ARRAY(ptr, n) \
    { \
        (ptr) = malloc((n) * sizeof (ptr)[0]); \
        if ((ptr) == NULL) { \
            fprintf(stderr, "error: Memory exhausted\n"); \
            exit(EXIT_FAILURE); \
        } \
    }

const char *getInput(void);

int main(void)
{
    const char *input;
    char *inputCopy;
    int inputLength;

    input = getInput();
    inputLength = strlen(input);
    NEW_ARRAY(inputCopy, inputLength + 1);
    strcpy(inputCopy, input);

    return 0;
}

Upvotes: -1

Related Questions