Run-time error when allocating new char[]

Mar 29, 2016 at 12:01am
Hi people,
the code below describes an example of implementation for couple functions of built-in C++ string class, as the CTOR and insert().
when I run this code, a run-time error occurs when b.set(r) is executed. More precisely, when the program tries to allocate str = new char[l] (only for b, not for a as said before). It says that "ConsoleApplication.exe has triggered a breakpoint."
Why? Particularly, why a is OK and b isn't?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
using namespace std;

class String
{
private:
    char* str;

public:

    String(char* s = NULL);
    ~String();
    void set(char *s);
   
    String& insert(int index, const char*other, String* y);
};

String::String(char *s)
{
    if (s)
    {
	int l = strlen(s) + 1;
	str = new char[l];
	strcpy_s(str, 100, s);
    }
    else
	str = NULL;
}

String :: ~String()
{
    if (str)
    {
	delete[] str;
	str = NULL;
    }

}

void String::set(char*s)
{
    if (s)
    {
	int l = strlen(s) + 1;
	str = new char[l];
	strcpy_s(str, 100, s);
    }
}

String& String::insert(int index, const char* other, String* y)
{
    int n = strlen(str) + strlen(other) + 1;
    char* tmp = new char[n];//New dynamic space allocation.
    strcpy_s(tmp, 100, other);//Copy the string are sent to help.
    strcpy_s(tmp + index, 100, str);/*Copying string ptr end String helped without overwriting.*/
    strcpy_s(tmp + strlen(tmp), 100, other + index);/*Copying the end of the word string sent without overwriting data.*/
    *y = tmp;

    delete[]tmp;
    return *y;
}


int main()
{
    String a, b;
    char * t = new char[1000];
    char * r = new char[1000];
    cin >> t;
    cin >> r;

    a.set(t);
    b.set(r);

    return 0;
}
Last edited on Mar 30, 2016 at 12:08am
Mar 29, 2016 at 5:32am
> strcpy_s(str, 100, s);
¿why 100?
¿what does that parameter mean?
Mar 29, 2016 at 11:46pm
100 is to explicitly specify the size of the target buffer, so the function will not overflow. I chose it arbitrary.
Visual Studio 2013 deprecated strcpy totally, and treats it as compilation error, so I used strcpy_s instead as it says.
Last edited on Mar 30, 2016 at 12:06am
Mar 30, 2016 at 1:59am
> 100 is to explicitly specify the size of the target buffer
ah, ¿so your target has that size?


here's a hint: it doesn't.
str = new char[l];
Mar 30, 2016 at 2:21am
First, as I understand - 100 is the maximum size of the buffer.
But anyway, why a.set(t) works while b.set(r) doesn't?
Last edited on Mar 30, 2016 at 2:25am
Mar 30, 2016 at 1:36pm
Suppose that you input
hello
world

`l' would be 6, but you are telling to write 100 elements to `str'. I don't understand why you are surprised then.
Mar 30, 2016 at 2:38pm
Again, if I understand correctly - I'm telling to write at most 100 elements to str.
I'm surprised of the fact that a.set() works well but b.set() doesn't...
Mar 30, 2016 at 5:01pm
I'm telling to write at most 100 elements to str.

According to the documentation page,
https://msdn.microsoft.com/en-us/library/td1esda9.aspx
The debug versions of these functions first fill the buffer with 0xFE.

That is, the function will always write 100 bytes to the buffer, before copying the characters from the source string. The whole point of specifying a number is to tell the function the size of the buffer, rather than give it some other unrelated number. That will result in a buffer overrun, exactly the issue that the function was supposed to prevent ... resulting in undefined behaviour.
Mar 30, 2016 at 5:14pm
OK, but anyway I'm curious to know why a.set(t) works and b.set(r) doesn't?
Mar 30, 2016 at 5:19pm
Undefined behaviour is undefined - anything can happen.
Mar 30, 2016 at 5:24pm
Alright, thank you guys
Topic archived. No new replies allowed.