Another exercise i am stuck with

Hello friends,
i have a new problem with another exercise. It states like this:

Imagine that we have a program that requires the informations of the user. for this purpose write a function Insert, that will look after the users informations. Because the function has to be general, its parameter should be a string, that has to be printed on the screen. If the user didn't write any informations the function returns NULL (char *Insert( char* )). The usage of the function is the following:

char *name;
name = Insert("Write your name . . .");
// . . .
delete name

(TIP: In the function use a static field (200 index). When the user writes the text, first of all discover the number of index. Reserve enough space, copy the string and return the pointer on the reserved space).

My code looks like this:

//POGL8_vaja2.cpp
#include <iostream>
using namespace std;
char clear_array(char *info)
{
int i;
for (i==0; i < 200; i++)
{
info[i] = 0;
}
}

char *feelIn(char *info)
{
int count_char;
char *point = info;
char *memory = new char[200];
while (*point != '\0')
{
point++;
count_char++;
}
memory[count_char];
memory = info;
info = memory;
*point = *memory;
if (count_char > 0) return memory;
else return '\0';
}

int main()
{
char *info = new char[200];
clear_array(info);
cout << "Write down your name!" << endl;
cin >> info;
char *space = feelIn(info);
cout << "Your name is " << space << endl;
delete info;
}

I have two errors. If I just press the button enter the application still waits for the informations of the users.
the second error is that if i write a name for instance Mark
the program returns MarkM.

I don't know what i did wrong thats why i am asking for your help.

thanks a lot
best regards,
BoSCHoW.
[2196 byte] By [BoSCHoW] at [2007-11-20 11:40:24]
# 1 Re: Another exercise i am stuck with
Hello friends,
i have a new problem with another exercise. It states like this:

Imagine that we have a program that requires the informations of the user. for this purpose write a function Insert, that will look after the users informations. Because the function has to be general, its parameter should be a string, that has to be printed on the screen. If the user didn't write any informations the function returns NULL (char *Insert( char* )). The usage of the function is the following:

char *name;
name = Insert("Write your name . . .");
// . . .
delete name

Who is teaching this C++ course? Already, this is not right, as you're supposed to be using delete[], not delete. When you use new[], you use delete[], not delete to free the memory.

Second, no one really writes C++ code the way you were instructed -- there is no indication that "Insert" returns a pointer to dynamically allocated memory that the client must be responsible for. Seriously, this is a terrible exercise, and just leads you down the path of writing very bad code. C++ has the std::string class for a good reason, and you're witnessing yourself why this is the case

Third, your code has memory leaks. You're calling new[] with no corresponding call to delete[].

char *memory = new char[200];
//...
if (count_char > 0) return memory;
else return '\0';
What happens if count_char is <= 0? You allocated memory already, but then you never deleted it before the return.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 1:26:01 >
# 2 Re: Another exercise i am stuck with
int main()
{
char *info = new char[200];

There is no need to create an array dynamically here.

char info[200];
clear_array(info);
cout << "Write down your name!" << endl;
cin >> info;
char *space = feelIn(info);
cout << "Your name is " << space << endl;

// what about "space"? It was never deleted
}

Excessive and unnecessary usage of "new" in a C++ program, similar to what you did in main() is an indication that you are not being taught C++ properly, or you're not understanding completely when to use it and when not to use it. A simple char array is all you needed for info.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 1:26:58 >
# 3 Re: Another exercise i am stuck with
In the book it says that new is used for space "reserve" and with operator delete we free this space. And this operators operated over the pointers. This two operators are used for the space that we "reserve" so must free it after the usage. When we use this is not said ... i have examples in the book but in them all i could use normal arrays . . .

Meanwhile in the exercise i did like you said. I also have commented the row

// *point = *memory;

Now the function brings out the right name. But still i don't know what is wrong with this part.

Thanks for all the help,
best regards,
BoSCHoW.
BoSCHoW at 2007-11-9 1:27:57 >
# 4 Re: Another exercise i am stuck with
In the book it says that new is used for space "reserve" and with operator delete we free this space.I don't know what this book is, but what I'm telling you is correct.

When you use new[], you use delete[], not delete. If you are told to use delete when you use new[], the book, teacher, or whoever is telling you these things is incorrect.

Second, the space that you are supposed to allocate, if I understand your exercise, is the space created for the new data within the function that creates the copy of the name. There is no need for you to be using new[] all over the place on everywhere and everything. That, hopefully, was *not* the point of the exercise.
And this operators operated over the pointers. This two operators are used for the space that we "reserve" so must free it after the usage. When we use this is not said ... i have examples in the book but in them all i could use normal arrays . . .Did you fix the problems I pointed out, i.e. the memory leaks? If you did, please post your current code.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 1:29:00 >
# 5 Re: Another exercise i am stuck with
Sometimes you will allocate arrays with new rather than using string or vector but these are rare, although the teacher may wish to show you how it's done.

However, a main concept in C++ is management of resource and memory, so it is rare that you will have a function that returns a pointer and leaves it to the responsibility of the caller to do the deletion.

The normal workaround therefore when a function has to allocate something to return to the user is to return an object, which will release the resource (memory or otherwise) when it is deleted.

This itself can be a difficult thing to do in C++ as when you return an object, the object is actually copied (albeit that the copy is sometimes optimised away) and the original copy is actually deleted as the object is returned (although once again this is likely to be optimised away). So writing a string class can be tricky.

There are workarounds to this issue, one of which is using reference-counting and smart-pointers which hold these reference counts. One such is shared_ptr which was created by boost and will be part of the new standard.

Another workaround which will be brought into the new standard is making objects "movable" thus you will be able to use this to return the objects from functions.

Note that std::auto_ptr will work for you in many situations, for example, you can return one from a function because the copy "steals" the pointer. You cannot us auto_ptr for arrays because it uses the wrong form of delete, however you can usually get by with arrays by using std::vector.
NMTop40 at 2007-11-9 1:29:59 >