CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: malloc problem?

  1. #1
    Join Date
    Apr 2002
    Location
    Mumbai,India
    Posts
    567

    Question malloc problem?

    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

  2. #2
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    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:

    Code:
    char* strNameUser;
    strNameUser = &#091;b&#093;new&#091;/b&#093; char&#091;20&#093;;
    cout<<"Enter your name\n";
    &#091;b&#093;cin.width( 19 );&#091;/b&#093;
    cin>>strNameUser;
    or preferably:

    Code:
    char strNameUser&#091;20&#093;;
    cout<<"Enter your name\n";
    &#091;b&#093;cin.width( 19 );&#091;/b&#093;
    cin>>strNameUser;
    Last edited by mwilliamson; April 21st, 2003 at 08:40 AM.

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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...
    Code:
    #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...

  4. #4
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    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()

  5. #5
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647

    Re: malloc problem?

    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.
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: malloc problem?

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured