Thread: dynamic array

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    30

    Exclamation dynamic array

    hey,
    how can i increase the size of a dynamic array even though i dont know what its end size will be.
    i try to use realloc and new but both seems didnt work on string.
    what was i trying to do is given a string "HELLO"...place it in array [0]= "HELLO".
    and the size of the array keeps on increasing if the user gives input.


    any help would be appreciated.

    cheers.

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    this is what i got so far..
    Code:
    void Stack :: push(std::string n){
    	//create new space when adding element
    	
    	if (size+1 < maxItems ){
    		maxItems *= 2;
    		char*array=new char[maxItems];
    		memcpy( newArray, array, sizeof ( char ) * size );
    		delete array;
    		array=newArray;
    
    	}
    
    // i know this is wrong but i just couldnt figure out how to place a string into ana array
    
    	array [++size]= n;
    	top++;
    	
    
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Unless you're learning/practicing, you should be using vector, which does all this for you. Since the name of your class is Stack, though, I'm going to assume you're just practicing.

    You need to allocate space for the new array, then copy the data over using a loop or the copy algorithm, then delete the old array. That's basically what you did, except you tried to use memcpy (which is a bad idea with strings) and you made a couple other minor errors.

    Note that in that code, you are allocating an array of char, but you are attempting to add a std::string to the end of the array. You should be allocating an array of std::string, not char.

    Also, take another look at your if statement, it's not quite right.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    for this practice,we are not allowed to use othe standard lib besides the string class...so what i trying to do here to create a new space for a string.

    any hints?

    thanks.

  5. #5
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    std::string::append?
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    1) Never use a 'naked new' (eg: assign the result of 'new' to a raw pointer). Always assign to an object that can deterministically free the memory from it's constructor. Incidentally, an std::vector would be perfect for the job.
    2) It isn't clear what 'size' actually does. I'm assuming it's the number of actual elements in use? In that case you probably want to resize the container if it's greater than 'maxItems' minus the string length (or just add that to 'size' and compare with 'maxItems' to prevent an underflow calculation).
    3) 'array' is declared within the scope of the block, overwites the old data and then get's deleted. I'm assuming you meant to make that 'newArray'?
    4) To copy the string, you can either index it like an ordinary char array, or even use memcpy by obtaining a pointer to it's data with the 'c_str' or 'data' member functions.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by effa View Post
    for this practice,we are not allowed to use othe standard lib besides the string class...so what i trying to do here to create a new space for a string.

    any hints?

    thanks.
    You don't have to use the standard library - just anything that will clean up the memory automatically.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    i tried the append method and i dont think it will works the way i want it to.
    Code:
    void Stack :: push(std::string n){
    	//create new space when adding element
    	const char* n2= n.c_str();
    	ans.append(n2);
    	ans+= ' ';
    	
    
    	size++;
    	
    	top++;	
    	
    
    }
    this only give me a list of string.and when i try to pop() it it will not give me the string i want.
    instead of giving me ans2[0]= "Hello" it gives ans2[0]='H'....therefore i think i really need to use new or realloc but i seems to get a lot of error when i tried to put variable to the maxItems.
    =(... what should i do? googling on this creating dynamically array didnt help me much either.im getting more confused.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    if i want to use realloc..how should i define the type..is it char? isnt this means it will only look into each character in the string but not as a whole string?

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Rather than just have us guess what you're trying to do, why don't you post a stripped-down example demonstrating the problem?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 AM
  2. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 01:59 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM