Click to See Complete Forum and Search --> : malloc problem?


VishalNikiSharma
April 21st, 2003, 08:20 AM
Hi
I need your help in the following problem
suppose i have to store a name of a person which i take at runtime from the user...
i am using the following code..

.
.
.
.
char* strNameUser;
strNameUser = (char*)malloc(sizeof(char*));
cout<<"Enter your name\n";
cin>>strNameUser;
.
.
.
.
my friends says that this will cause the error sometimes...but i do not think so,
please help me in understanding this..
if this code causes problem then suggests me the correct code...
Thanks
Vishal

mwilliamson
April 21st, 2003, 08:37 AM
sizeof(char*) will always return 4, since you are using a 32bit operating system all pointers are 32bits in size. So if you want to create a 4 byte string, just put in 4.

You code should be:

char* strNameUser;
strNameUser = [b]new[/b] char[20];
cout<<"Enter your name\n";
[b]cin.width( 19 );[/b]
cin>>strNameUser;

or preferably:


char strNameUser[20];
cout<<"Enter your name\n";
[b]cin.width( 19 );[/b]
cin>>strNameUser;

Andreas Masur
April 21st, 2003, 08:38 AM
Your friend is basically right. You are allocating memory based on the size of a pointer which is in most operating systems 4 bytes. If you type in more than 3 characters you will write over the boundaries of your array.

Use the string class from the standard template library to avoid such cases...

#include <string>
#include <iostream>

int main()
{
std::string strNameUser;

std::cout << "Enter your name" << std::endl;
std::cin >> strNameUser;

return 0;
}

As you can see there is no need any longer to do any memory allocation or deallocation since it is done by the string class internally. The sample is of course only a simple one but it should give you the right direction...

mwilliamson
April 21st, 2003, 08:42 AM
Originally posted by Andreas Masur
Your friend is basically right. You are allocating memory based on the size of a pointer which is in most operating systems 4 bytes. If you type in more than 3 characters you will write over the boundaries of your array.

Use the string class from the standard template library to avoid such cases...

or set cin.width() ;)

dimm_coder
April 21st, 2003, 08:43 AM
Originally posted by VishalNikiSharma
char* strNameUser;
strNameUser = (char*)malloc(sizeof(char*));
cout<<"Enter your name\n";
cin>>strNameUser;
Thanks
Vishal

Your friend is right.
sizeof(char*) == 4 == sizeof(any pointer on int32 platform)
So , your strNameUser allways points to a memory region == 4 bytes.

U need for a ex a fixed mem region ,
strNameUser = (char*)malloc(MAX_NAME_LEN);
where MAX_NAME_LEN - is a maximum lenght of a possible user name, but U must correctly perform a situation when user write more than this num of symbols.

Paul McKenzie
April 21st, 2003, 10:52 AM
Originally posted by VishalNikiSharma
Hi
I need your help in the following problem
suppose i have to store a name of a person which i take at runtime from the user...
i am using the following code..

char* strNameUser;
strNameUser = (char*)malloc(sizeof(char*));

a) Usage of malloc() is to be avoided in a C++ program. If anything, you use new char [].

b) You are only allocating sizeof(char *) bytes, which is usually 4 on many operating systems. Therefore you overwrite memory when you store the characters.

c) Use std::string (as Andreas Masur pointed out). There is no need to be coding so much complexity (and 'C' style coding) for such a simple thing.my friends says that this will cause the error sometimes...but i do not think so, please help me in understanding this..Why do you believe it won't cause problems and think your friend is wrong? ;)

Regards,

Paul McKenzie