Thread: Dynamic array?

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    3

    Dynamic array?

    Hi,

    I'm working on a telnet client for BBSes. I have most everything I need in place, except for the phonebook part of it. I need to be able to read in an external array, which is an external file (array.txt), and have it "load" the telnet addresses from that array, so I can pass the values as an argument to the telnet() function I have.
    As it is now, I have to recompile the source code _every_ damn time I add a new element to the array. Which is annoying to say the least.

    I'm wondering, would a dynamic character array work best for this?

    Thanks for your time, any help would be greatly appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,070
    Yes, e.g., with malloc, realloc and free.
    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

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    3
    Ok, and how do I go about declaring a dynamic array?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,070
    Telnet addresses are just IPv4 addresses, right? I'd go with a struct to hold an IPv4 address, then you could write:
    Code:
    struct addr *addresses = malloc(sizeof(addresses[0]) * N);
    where N is the initial number of elements. You can figure out how to realloc and free by searching online.
    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
    Mar 2019
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Telnet addresses are just IPv4 addresses, right? I'd go with a struct to hold an IPv4 address, then you could write:
    Code:
    struct addr *addresses = malloc(sizeof(addresses[0]) * N);
    where N is the initial number of elements. You can figure out how to realloc and free by searching online.
    Yes. Ipv4 addresses. That little bit of code is all that is necessary?
    How exactly do I incorporate this code into my project?

    I should have also mentioned that, "array.txt" is actually an #include file in my code. So, would I put your code into that file? Forgive me, i'm very new to C.

    Thanks.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,070
    Quote Originally Posted by ignatius333
    I should have also mentioned that, "array.txt" is actually an #include file in my code.
    Yes, you should have mentioned this from the start. I assumed that by "external array" you meant the content of the array was loaded from an external file, whereas what you seem to have done was to literally define a C fixed size array in an external file, and then name it "array.txt" when in reality it is a C header file, not a generic text file.

    Unfortunately, you have to redo that part of your code because your approach is simply wrong: with your current approach, you must always recompile whenever you add a new element to the array. There is no way around it short of a code redo.

    So, to redo:
    • Remove the #include of array.txt
    • Change the format of array.txt to something easier to parse, e.g., one IP address on each line, nothing else.
    • Adapt what I showed you to create a dynamic array.
    • Write code to read from array.txt and populate the dynamic array. There are a few ways to do this, but one way is to have two size_t variables: size and capacity. size would track the number of elements of the dynamic array that are in use, and capacity would track the number of elements of the dyanamic array that have been allocated. You start with size == 0 and capacity == 5 (or some other suitably small non-zero number), then whenever the size is about to exceed capacity (i.e., you have read the next IP address, but size == capacity), you realloc the capacity by some factor (e.g., doubling it with a factor of 2).


    This way, when you change array.txt (I'd suggest renaming it to something more meaningful), you just need to run your program and it will load the current content to the dynamic array.
    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. why can't a static array copy values from a dynamic array
    By c++noob145 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2013, 10:25 AM
  2. Replies: 10
    Last Post: 12-03-2011, 02:26 PM
  3. Copy array into a Dynamic array
    By pantherman34 in forum C Programming
    Replies: 15
    Last Post: 05-01-2010, 10:58 PM
  4. dynamic array
    By Antimodes in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2009, 06:06 AM
  5. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM

Tags for this Thread