Thread: dynamic memory allocation (arrays)

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    8

    Smile dynamic memory allocation (arrays)

    Hi,

    I have recently bought a copy of "Jumping into C++" and have come to chapter 14 ( dynamic memory location) and have a question.

    On page 153-154 an example of dynamic allocation is given for array's of int. How would the code look like for strings or structs ?

    The allocation was given by:

    Code:
     
    int *growArray (int* p_values, int *size)
    {
    *size *= 2;
    int *p_new_values = new int[ *size ];
    for ( int i = 0; i < *size; ++i )
    {
    p_new_values[ i ] = p_values[ i ];
    }
    delete [] p_values;
    return p_new_values;
    }
    Sample Code
    I tried to use this for an array of structs but failed completely... please help.

    I used the following struct:

    Code:
    struct user{
        int days; 
        string name;
    };
    
    and the allocation function (which does not work):
    
    
    Code:
    struct user *growarray (struct user *p_values, int *size)
    {
    *size *= 2;
    struct user *p_new_values = new struct user[ *size ];
    for ( int i = 0; i < *size; ++i )
    {
    p_new_values[ i ].name = p_values[ i ].name;
    p_new_values[i].days = p_values[ i ].days;
    
    
    }
    
    
    delete [] p_values;
    return p_new_values;
    }

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    21
    Why don't you use vectors? Is there any specific reason for which you're sticking to arrays?

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    8
    I haven't made it to vectors yet, so don't know how they work... secondly I clearly didn't understand pointers properly.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, your mistake in the original growArray is that you loop using *size, which has already doubled by that point. In your second version, growarray should just assign user objects rather than assign individual members (and the *size bug is still present).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    8
    Hi laserligh,
    thanks for your reply!

    There is still something I don't get. You say that *size is used wrong, but why does it then work when using type "int" in the version from the book? Can you give an example what you mean by assigning user objects ?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by newHansen View Post
    There is still something I don't get. You say that *size is used wrong, but why does it then work when using type "int" in the version from the book?
    You got unlucky. The bug is there for the int version, but didn't cause a crash in your circumstance. The problem is that you are copying twice the number of elements from p_values than are there.

    The C++ standard does not define what happens when you access more array elements than an array has. With undefined behaviour, anything can happen. You just got unlucky, with your compiler/library/host system, that your code seemed to work with the int version. It could equally have crashed, or reformatted your hard drive, or fried your mouse, .....

    Quote Originally Posted by newHansen View Post
    Can you give an example what you mean by assigning user objects ?
    If you want to assign a whole struct, use

    Code:
        //  assume a_struct and b_struct are both of type struct some_type, which has members member1, member2, ....  memberN.
       a_struct = b_struct;
    rather than
    Code:
        a_struct.member1 = b_struct.member1;
        a_struct.member2 = b_struct.member2;
    
          //   assign other members
    
        a_struct.memberN = b_struct.memberN;
    The whole point of a struct is that it can be treated as a logical unit. That extends to assignment.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by newHansen View Post
    There is still something I don't get. You say that *size is used wrong, but why does it then work when using type "int" in the version from the book?
    You got unlucky. The bug is there for the int version, but didn't cause a crash in your circumstance. The problem is that you are copying twice the number of elements from p_values than are there.

    The C++ standard does not define what happens when you access more array elements than an array has. With undefined behaviour, anything can happen. You just got unlucky, with your compiler/library/host system, that your code seemed to work with the int version. It could equally have crashed, or reformatted your hard drive, or fried your mouse, .....

    And if the code you quoted is unmodified from the book, Alex deserves a gentle spanking (unless he has specified that the code is illustrating a problem).

    Quote Originally Posted by newHansen View Post
    Can you give an example what you mean by assigning user objects ?
    If you want to assign a whole struct, use

    Code:
        //  assume a_struct and b_struct are both of type struct some_type, which has members member1, member2, ....  memberN.
       a_struct = b_struct;
    rather than
    Code:
        a_struct.member1 = b_struct.member1;
        a_struct.member2 = b_struct.member2;
    
          //   assign other members
    
        a_struct.memberN = b_struct.memberN;
    The whole point of a struct is that it can be treated as a logical unit. That extends to assignment.
    Last edited by grumpy; 02-25-2014 at 03:08 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Feb 2014
    Posts
    8
    Hi grumpy,

    Thanks for your answer, it worked perfectly correcting the array length... if you come to Copenhagen I will buy you a beer!

  9. #9
    Registered User
    Join Date
    Feb 2014
    Posts
    8
    And also to you laserlight ... Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Dynamic Memory allocation for arrays
    By mrafay in forum C Programming
    Replies: 19
    Last Post: 01-09-2011, 11:44 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Help with dynamic memory allocation
    By malooch in forum C Programming
    Replies: 2
    Last Post: 12-13-2006, 01:26 PM
  4. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  5. Dynamic Memory Allocation?
    By motocross95 in forum C++ Programming
    Replies: 11
    Last Post: 12-03-2002, 08:52 PM