Thread: Overwriting Array with New Array

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    23

    Overwriting Array with New Array

    I'm working with a program at the moment that needs to have an array of undefined length (and of type 'record', which is a struct) that is initialized in a separate function, sorta like so:

    Code:
    class MyClass {
    ...
    private:
        record knowledgeBase[];
    }
    ...
    
    void MyClass::initKnowledgeBase() {
        knowledgeBase[] = {
            ...etc...
        }
    }
    I've been looking everywhere and tried everything I could think of. Is there any way to make it so that every time initKnowledgeBase() is called, I can recreate the variable-length knowledgeBase[] array?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    yes
    you make knowledgeBase pointer to record
    Code:
    record* knowledgeBase ;
    and initialize it using new

    also - do not forget to delete it afterwards
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you considered just using a std::vector<record> as a member variable instead? Also, why not just use the constructor?
    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

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    Quote Originally Posted by laserlight View Post
    Have you considered just using a std::vector<record> as a member variable instead? Also, why not just use the constructor?
    I have, but it messed things up in ways I'm not really prepared to fix. And I can't use it in the constructor because in record, there's a string entry that needs to have a variable put in at run time... If that explanation makes any sense.

    @vart:
    Could you give me a little snippet to show what you mean? Not sure I fully understand.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I have, but it messed things up in ways I'm not really prepared to fix.
    If that is the case, then you will find that with vart's suggestion, you may mess things up in even more ways that you are probably also not prepared to fix.

    And I can't use it in the constructor because in record, there's a string entry that needs to have a variable put in at run time.
    Well, firstly you say you want an array of "undefined" length. Of course, an array must have a length, just that its length may be unknown at compile time (e.g., it is a dynamically allocated array, as per vart's suggestion). But then you want to initialise this array with an initialiser list, which implies that you know its length at compile time.

    I suggest that you use a std::vector<record>, and then have record provide a default value for strings that are not specified. Later, when these values are known, you can assign them.
    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

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    Quote Originally Posted by laserlight View Post
    If that is the case, then you will find that with vart's suggestion, you may mess things up in even more ways that you are probably also not prepared to fix.
    What I meant by "not prepared to fix" was that I would have to entirely rewrite the code, which is not something I really want to do... Not that I couldn't fix the problems.

    Quote Originally Posted by laserlight View Post
    Well, firstly you say you want an array of "undefined" length. Of course, an array must have a length, just that its length may be unknown at compile time (e.g., it is a dynamically allocated array, as per vart's suggestion).
    That's what I meant. I want an array with an undefined length.

    Quote Originally Posted by laserlight View Post
    But then you want to initialise this array with an initialiser list, which implies that you know its length at compile time.
    Not 100% sure what you mean, but if I'm taking it right, I technically DO know the length at runtime. The thing is, the length is going to change so often as I'm developing that I don't want to have to go through all my code and change the length every time I change the array around. If this is impossible, oh well. I'll deal I guess.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not impossible.
    std::vector will grow and shrink dynamically. You don't have to worry about a thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What I meant by "not prepared to fix" was that I would have to entirely rewrite the code, which is not something I really want to do... Not that I couldn't fix the problems.
    Ah, that's fair enough.

    Not 100&#37; sure what you mean, but if I'm taking it right, I technically DO know the length at runtime. The thing is, the length is going to change so often as I'm developing that I don't want to have to go through all my code and change the length every time I change the array around.
    It's my turn to be unsure of what you mean

    As I noted, you definitely know the array length at runtime. The question then is: do you know the array length at compile time? If you do and this value is prone to changes during development, the solution is to define a static constant (or an enum) that holds the fixed array length, and then use this throughout the relevant code. When you decide that the length needs to be changed, just change the value of that static const size_t member variable (or enum).

    If you do not know the array length at compile time, then you should use a std::vector. Of course, you could still use a std::vector even if you know the array length at compile time, though arguably something like std::tr1::array would be more appropriate than std::vector.
    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

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> What I meant by "not prepared to fix" was that I would have to entirely rewrite the code
    Are you sure? Vectors have a very similar interface to regular dynamic arrays. You don't have to use iterators, you can use operator[] just like with a C style array.

    In general they take less code because you don't need a destructor, copy constructor or copy assignment operator for your class either. They are also safer for those reasons.

    If you don't want to take the time to use a vector now, that is your decision, but in most cases it would probably be worth it.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    Quote Originally Posted by Daved View Post
    Are you sure? Vectors have a very similar interface to regular dynamic arrays. You don't have to use iterators, you can use operator[] just like with a C style array.
    Fairly sure. I actually tried it at one point, but even when I went through and changed everything, it just caused too many problems due to how my program is currently structured.

    As for the size issue, that's really unimportant at this point. I just need to know how I could have it redefine the array. 'vart' gave an answer, though I really don't understand it fully. Could I get someone to give me a short example?

    EDIT: I'd also like to say I'm coming over from a C background, so the whole "new" thing is a bit (pardon the pun) new to me, so if perhaps you could point me to a good explanation of it as well...

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use push_back() to add a string and erase() to delete something and the std::vector will automatically manage size for you.
    Vart means that you create an array on the heap:
    Code:
    record* p = new record[n];
    The only problem is that you must remember the size yourself and delete, reallocate and copy if it gets too big:
    Code:
    int size = 100;
    int curelement = 0;
    record* p = new record[size];
    //...
    
    if (curelement + 1 > size) // Not enough room, reallocate
    {
    	record* p2 = new record[size * 2];
    	memcpy(p, p2, size);
    	size *= 2;
    	delete [] p;
    	p = p2;
    }
    Of course, you should really use std::copy, but that's another matter.
    Last edited by Elysia; 02-25-2008 at 12:55 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As for the size issue, that's really unimportant at this point. I just need to know how I could have it redefine the array. 'vart' gave an answer, though I really don't understand it fully.
    The size issue matters as it is at the heart of the problem. vart's solution describes a dynamically allocated array, which is the raw version of std::vector as provided by the core language. std::vector is its standard library equivalent. If the size of the array is unknown at compile time, you should use a dynamically allocated array, which means that you should use std::vector unless you have a good reason to use its core language version.

    Fairly sure. I actually tried it at one point, but even when I went through and changed everything, it just caused too many problems due to how my program is currently structured.
    Since std::vector is the standard library version of a dynamically allocated array, if std::vector is causing problem due to how your program is structured, a dynamically allocated array will cause the same problems, and worse.
    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

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    Forget the size issue. Let's say for ease's sake that I just put the size in hard-coded everywhere. Let's get past that entirely.

    What I need to know is how to in essence "reinitialize" the array in that separate function. To redefine it entirely.

    Am I making any sense? I feel like I'm not. =/

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What I need to know is how to in essence "reinitialize" the array in that separate function. To redefine it entirely.
    You will have to (manually) assign the array elements to the member array in that function. The initialiser list cannot be used.
    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

  15. #15
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    Well, I decided to take everyone's advice (even if it was advice at gunpoint =P) and try again with vectors, and I don't know what I was doing wrong before, but it works just fine now. Not sure where my code or logic went wrong previously, but I'm good now I guess. *shrug*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM