0

I have given the array size manually as below:

int main(int argc, char *argv[] )
{
    char buffer[1024];
    strcpy(buffer,argv[1]);
    ...
}

But if the data passed in the argument exceeds this size, it may will create problems.

Is this the correct way to allocate memory dynamically?

int main(int argc, char *argv[] )
{
    int length;
    char *buffer;
    length = sizeof(argv[1]); //or strlen(argv[1])?
    buffer = (char*)malloc(length*sizeof(char *));
    ...
}
2
  • 3
    Are you working in C or C++? If you're working in C++, using malloc() is generally misguided (and raw char * is also not a good idea). If you're working in C, you don't need the C++ tag. In fact, don't dual-tag the question — they are two distinct languages. Commented Aug 30, 2013 at 15:08
  • As to your question "use sizeof() or strlen()", using strlen(argv[1])+1 will work as you want it to (and using sizeof will generally not allocate enough memory). Bigger question, why do you need a copy of the data that argv[1] points at? Commented Aug 30, 2013 at 15:10

6 Answers 6

4

sizeof tells you the size of char*. You want strlen instead

if (argc < 2) {
    printf("Error - insufficient arguments\n");
    return 1;
}
length=strlen(argv[1]);
buffer = (char*)malloc(length+1); // cast required for C++ only

I've suggested a few other changes here

  • you need to add an extra byte to buffer for the null terminator
  • you should check that the user passed in an argv[1]
  • sizeof(char *) is incorrect when calculating storage required for a string. A C string is an array of chars so you need sizeof(char), which is guaranteed to be 1 so you don't need to multiply by it

Alternatively, if you're running on a Posix-compatible system, you could simplify things and use strdup instead:

buffer = strdup(argv[1]);

Finally, make sure to free this memory when you're finished with it

free(buffer);
Sign up to request clarification or add additional context in comments.

Comments

1

The correct way is to use std::string and let C++ do the work for you

#include <string>

int main()
{
    std::string buffer = argv[1];
}

but if you want to do it the hard way then this is correct

int main()
{
    int length = strlen(argv[1]);
    char* buffer = (char*)malloc(length + 1);
}

Don't forget to +1 for the null terminator used in C style strings.

Comments

1

In C++, you can do this to get your arguements in a nice data structure.

const std::vector<std::string>(argv, argv + argc)

Comments

1
length= strlen(argv[1]) //not sizeof(argv[1]);  

and

//extra byte of space is to store Null character.    
buffer = (char*)malloc((length+1) * sizeof(char));

Since sizeof(char) is always one, you can also use this:

  buffer = (char*)malloc(length+1);                       

2 Comments

If C: a cast of malloc is not needed and the sizeof(char) is also redundant, because it is 1.
@pzaenger yes agreed , written in general way.
0

Firstly, if you use C++ I think it's better to use new instead of malloc.

Secondly, you're malloc size is false : buffer = malloc(sizeof(char) * length); because you allocate a char buffer not a char* buffer.

thirdly, you must allocate 1 byte more for the end of your string and store '\0'.

Finally, sizeof get only the size of the type not a string, you must use strlen for getting string size.

2 Comments

In C++, it's even better not to use new but to use std::string.
I think it depends of what you want to do with it, if it's for storing a string, of course std::string do the job, but for an array which is not necessary a string, I prefere manage it myself.
-1

You need to add an extra byte to hold the terminating null byte of the string:

length=sizeof(argv[1]) + 1;

Then it should be OK.

2 Comments

sizeof is not OK in this context
@David Elliman The type of argv[1] is char*. sizeof(char*) tells you the size of a pointer rather than the length of the string it points to.

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.