1

I am having a difficult time obtaining the correct size of a string in order to satisfy strcpy_s. For example if I specify

char buffer = {0};
char *str1 = (char*)&buffer;    
strcpy_s(str1,sizeof("This is a string\n"),"This is a string\n");

Then it will work as expected. If however I declare the following:

char buffer = {0};
char *str1 = (char*)&buffer;
const char* string1 = "This is a string.....";
strcpy_s(str1, ?????,string1);

If I use anything other than a literal in place of ????? it will fail with a memory exception, for example if I use std:strlen(str1), etc. Any size literal for ???? will work. Of course using a fixed literal is not acceptable.

This is a major re-edit of the original question and I apologise to the people who have answered to date. However none of the the answers below have worked.

  • The second argument should be the size of the destination, that is the size of str1. In other words, how much space is available. – zdf Dec 10 '15 at 14:38
  • [Learn the difference between arrays and pointers](https://stackoverflow.com/questions/3959705/arrays-are-pointers). – Simple Dec 10 '15 at 14:40
  • Your first attempt is not correct, even if it “works” (at some point it will crash). It is not correct for you have allocated a buffer that can hold **one character, only** and you are copying more than one character. The correct call would be `strcpy_s(str1, sizeof(buffer),"This is a string\n");`. However, you will not get the expected result since the buffer is only 1 character long. To see something you should allocate a large enough buffer: `char buffer[128]`, for instance. This buffer will hold a 127 characters long string (plus terminating null). – zdf Dec 10 '15 at 15:51

3 Answers3

2

"This is a string" is a character array. When you say sizeof(Array)/sizeof(type) it will give the size of the array

When you define the string as const char* then the sizeof(pointer) gives the size allocated for the pointer no the array size

const char* ptr = "This is a string\n";
std::cout<<sizeof("This is a string\n")<<std::endl; //==>18
std::cout<<sizeof(ptr)<<std::endl; //==>4
Praveen
  • 8,945
  • 4
  • 31
  • 49
2

First of all, the second parameter is the size of the destination buffer, not the size of the source buffer.

so the correct way is:

char str1[100];
strcpy_s(str1, sizeof str1, "Whatever string");

or

int n = 100;
char *str1 = new char[n];
strcpy_s(str1, n, "whatever string");

For an array (first example) sizeof returns the size of the array.

For a pointer (second example) sizeof returns the size of the pointer (which is not what you want)

bolov
  • 72,283
  • 15
  • 145
  • 224
1

In your second example, string1 is of type const char*. sizeof will return the size of the pointer, rather than the length of the string literal you are pointing to.

The first example works because a string literal is a const char[], and sizeof will correctly return the length of the string (but with the null terminating character as well). It's only coincidental that this works because char is 1 byte. Do not use sizeof to get string lengths.

To make your second example work, try using std::strlen.

Aiden Deom
  • 916
  • 5
  • 11