Thread: Need help transferring contents of one character array to another in function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by blackfox_1109
    Is freeing the original function entirely unnecessary because I am just sending it right back to the exact same addresses?
    In your original implementation, free(list->array) is necessary because you use malloc to allocate a new dynamic array. Using realloc, this is no longer necessary and would in fact be a mistake. The loop that loops to free(list->array[i]) is a mistake because you copied those pointers over to the new dynamic array, thus freeing them here means that the new dynamic array contains pointers that are no longer valid.

    Quote Originally Posted by blackfox_1109
    And what is the format for realloc so that I could use it in the future with my other functions?
    Your learning materials should contain a declaration of realloc that you can refer to. Or, if you are using a system with man pages, running man realloc might work. Or, you can search the Web.

    EDIT:
    Oh, actually, the loop that "copy the contents of list->array into newarray" is also wrong because you failed to allocate space for the pointers to point to. With strcpy, you did not copy pointers, but rather copied the contents of what the pointers pointed to.

    I suggest that you work on something simpler: try implementing an ArrayList struct for a 1D dynamic array of ints, or maybe a string struct. Doing 2D without having done 1D is harder than doing 1D and then 2D.
    Last edited by laserlight; 02-01-2014 at 11:39 AM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Transferring vector data to an array
    By bijan311 in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2012, 12:30 PM
  2. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 01:32 PM
  3. Passing a character array to a function
    By Qchem in forum C Programming
    Replies: 3
    Last Post: 03-07-2005, 07:18 AM
  4. Reading Contents of Edit Control up to a Certain Character
    By maththeorylvr in forum Windows Programming
    Replies: 1
    Last Post: 02-19-2005, 06:26 AM
  5. passing a character array through a function
    By volk in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2003, 05:47 PM

Tags for this Thread