return error

This program fails with a message of:

Run Time Check Failure #2 - Stack around the variable 'holdarea' was corrupted

on line: 'return holdarea'?

Any help?

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

#include <iostream>
#include <cstring>

using namespace std;


char* parse_words(char* pstr, int strp);


int main()
{
	char* strin("This is the string to be parsed");
	cout << " String is: " << strin << " Length is: " << strlen(strin) << "\n\nParsing...\n";

	/* cout << "Please enter a string to parse word-by-word: \n";
	cin >> strin;
	cout << "This is the string entered: " << strin << " \n.";
	*/
	//char * p(nullptr);
	char* p = parse_words(strin, strlen(strin));
	//char* p = parse(s1);
	while (p)
	{
		std::cout << p << "\n.";
		delete [] p;
		p = parse_words(nullptr, strlen(strin));
	}
	return 0;


}
char* parse_words(char* pstr, int strp)
{
	static char* pStr(nullptr);
	static int start(0);
	static int len(strp);
	cout << "Value of len: " << len << '\n';
	int lenpm(len);
	size_t pos(0);
	char* pReturn(nullptr);
	int i(0);
	int n(0);
	int x(0);
	int holdlen = 20;
	char holdarea[20];

	for (start; start <= len; start++)
	{
		for (x = 0; x <= holdlen; x++)
		{ 
			holdarea[x] = ' ';
		}
		if (*pstr != ' ')
			while (*pstr != ' ')
			{
				holdarea[x] = *pstr;
				x++;
				*pstr++;
				start++;
			}
		else
		{
			cout << "parse word: " << holdarea;
			return holdarea;
		}

	}

	return pReturn;

}
I don't have the answer but line 64 will just output the memory address of the array. I doubt that's what you intended.
closed account (Dy7SLyTq)
why not use std::string? using char*'s like that is very dangerous
holdarea is a local variable in function parse_words().
When the function ends, the local variables fall out of scope and no longer exist - that means the memory where they were stored may be reused for some other purpose. Returning a pointer to a local variable like this is not valid.
Topic archived. No new replies allowed.