why not always hello?

Oct 22, 2009 at 1:08am
closed account (iw0XoG1T)
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
/*

Name: strdup.cpp

Purpose: exercise.

*/





#include "error.h"

#include <cstdlib>



using namespace std;

//=============================================================================

struct strdup{
	strdup(const char* original){
		char ch=*original;
		int* size = new int(0);
		while(ch!=0){ 				//determining size
			ch=*original++;
cout<<ch;
			*size=*size+1;
		}
cout<<endl;
		char* copy=new char[*size];
		for(int i=0; i<*size+1; i++)cout<<original[i];
cout<<original<<endl;
	}

	char* get()const{return copy;}

	private:
		char* copy;
		int* size;
};
//============================================================================

int main()try{
	char stringtest[]="hello";

	strdup test(stringtest);


	

	return 0;

}

catch(Run::Error& e){

	_RUN_ERROR;

}

catch(...){

	return 1;

}
Oct 22, 2009 at 1:39am
While you do allocate space for strdup.copy, you don't actually fill it with anything.

Besides, how does this even compile? You're using cout, but you never include <iostream>.
Last edited on Oct 22, 2009 at 1:41am
Oct 22, 2009 at 2:07am
closed account (iw0XoG1T)
<iostream> was included in "error.h"; the reason I did not fill it was because I am not getting what I expected. This is just an exercise. I thought I understood pointers but I obviously do not. If I could figure out why lines 34 and 35 print junk instead of "hello" I would finish this exercise.
Oct 22, 2009 at 12:31pm
You have a number of problems.

Your constructor is re-declaring size and copy to be local variables, which means you are never
actually filling out the "size" and "copy" class/struct data members. [Not to mention that you
don't ever copy the original string to either the data member "copy" or the local variable "copy"]
This is your problem.

Couple of other things:

Add #include <iostream> anyway. It is never a good idea to rely on another header file to include
a header file you need.

Don't use the names strdup or copy in the global namespace, because both of those names are
already used.

Why are you dynamically allocating an int? ie, why is size a pointer instead of an int?

get() should return a const char*.

If you want the user to be able to call get() and use the resulting const char* as a C-style string,
then you have to make sure you \0 terminate it.

Why are you doing this anyway???

Oct 23, 2009 at 12:23am
closed account (iw0XoG1T)
The reason I am doing this is to gain an understanding of pointers and the use of heap memory, (this program has no practical use). It is a slightly modified exercise that I found in a book that I am using.

Thank you for the advise and criticism I appreciate both; I am just sorry that I did not read your response sooner--it would have saved me some time.
Topic archived. No new replies allowed.