Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Buffer too small error

Status
Not open for further replies.

electroRF

Member
Hi,
I wrote the following C++ constructor, and I get an error - BUFFER too small on strcpy_s

Code:
Trace::Trace(const char *str)
{
    if (str)
    {
        int len = strlen(str);
        this->m_name = new char[len+1]; // asking for 'len+1' memory elements of char
        strcpy_s(m_name, len, str); // **** I get here an error "BUFFER TOO SMALL" ****
        cout << "start of " << m_name << endl;
    }
    else
    {
        m_name = new char[1];
        m_name[0] = '\0';
    }   
}

m_name is a private data member of type char* .

You have an idea why I get this error?
 
Hi Ian,

Please see my answers to your questions:

How big is is the buffer m-name? It seems as if its smaller than len!!
m_name is:
Code:
char *m_name;
is is a pointer and i dynamically allocated 'len+1' char elements to it.

strcpy is for ( dest, suorce)
strncpy is for ... ( dest, source, len)
I used strcpy_s which receives (dest, len, source)
 
Hi Ian,
Please look at the code i posted in the first post.
I did initialize the pointer m_name:
Code:
 this->m_name = new char[len+1]; // asking for 'len+1' memory elements of char
 
tried it out before, i don't remember exactly what error it generated, but there as an error still..

The following worked instead:
Code:
memcpy(m_name, str, len+1)

But I still don't get it, why strcpy_s did not work?
 
tried it out before, i don't remember exactly what error it generated, but there as an error still..

The following worked instead:
Code:
memcpy(m_name, str, len+1)

But I still don't get it, why strcpy_s did not work?

It will work ("The destination string must be large enough to hold the source string, including the terminating null character.") but strcpy_s is non-standard and MS specific. Memcpy will work with PODs in C++.

https://en.wikipedia.org/wiki/Plain_old_data_structure
 
If you read the description of strcpy_s(), you'll see that the second argument the size of the buffer. By passing len you say:

Dear strcpy_s() function, I want to copy the string from src to dst, but my dst is only len bytes long. Make sure not to overrun it.

strcpy_s() tries to copy and finds out that copying would require to put len+1 characters into the dst buffer. So it says:

Dear electroRF, I would be glad to copy for you. I would need len+1 characters, but you told me that only len is available. So, I better return the "Buffer too small" error.

This would work:

C:
strcpy_s(m_name, len+1, str);

However, using memcpy() would be better because you already know the length.
 
oh got you NorthGuy!
Thank you!!! :)

I thought that the size_t len was the length of the string, as in C string (i.e. excluding the NULL character), and not the size of dst.

Thank you for clearing it up!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top