dynamic memory alloc. for array elements

This is a discussion on dynamic memory alloc. for array elements within the C++ Programming forums, part of the General Programming Boards category; Hello, I want to create a dynamic array(one dimension) but amount of elements(member count)will be increase while working, of course ...

  1. #1
    Unregistered
    Guest

    dynamic memory alloc. for array elements

    Hello,
    I want to create a dynamic array(one dimension) but amount of elements(member count)will be increase while working, of course if necessary. And the members of array would keep for later use. Example;
    {
    search 1. number > if found > store it > increase array index > search next ...
    }

    {
    Get 1. stored number > use it > get 2. number > ..
    }
    delete _array_

    ...so I can not guess amount of array size before also I have to store what found while search.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    1. store the value in a temporary variable.
    2. create a new array that is arraysize+1.
    3. copy the contents of the old array to the new array.
    4. add the value in the temp variable to the new array.
    5. delete the old array.

    I'm assuming you already know about the new and delete tokens. if not it goes like this:

    Code:
    char* myarray = new char[arraysize]; /*create a pointer to new array.  access elements normally. */
    delete []myarray;  //free memory

  3. #3
    Unregistered
    Guest
    Thank you,

    Liked this trick, sensible. I will try. Maybe ask again.

  4. #4
    Unregistered
    Guest
    I suppose failed on copying arrays. If I wouldn't appointment old array adress to newest, how can I copy them. In fact it would be too better if could see an example. If possible. Or I will more study and/or search about this.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    317
    You have to copy the arrays by value, that is go through a for statement that 'physically' copies each element from the old array to the new array. Then add the new value to the end of the old array.

    Code:
    for(int i=0;i<arraysize;i++)
               newarray[i] = oldarray[i];
    
    newarray[newarraysize]=temp;
    The best way to do what you want is create a function that returns the new array. You have to get into operator overloading however. Its getting too late for me to write the entire code tonight, so if you want I'll post it tomorrow.

  6. #6
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,
    maybe a vector would work better,

    M.R.
    I don't like you very much. Please post a lot less.
    Cheez
    *and then*
    No, I know you were joking. My point still stands.

  7. #7
    Unregistered
    Guest
    OK. Thank you too much. I will try again. You are too kind.

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    317
    good point itld, I always forget about those templates. I guess I'm just too stuck in my old ways

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    317
    btw here is a link to this sites tutorial on the vector class:

    http://www.cprogramming.com/tutorial/stl/vector.html

  10. #10
    Unregistered
    Guest
    Thank you for your interest again!

    I suppose did it but I think not very successfull. Because this is slower than what I expected. Anyway, here is my code;

    int main(int argc, char **argv)
    {
    TMemoryStream *orgf,*modf; // This is a part of project. Only compares two files
    char *porjb,*pfrkb; // then writes different bytes "Relative Virtual Offset"s
    char orjb,frkb; // to a file as Hex. Only test purpose demo. (Yes, C++Builder 1.0)
    long position; // warning : works too slow
    long changes; // vOLKi - 4 June 2002
    FILE *f;
    long *pPosition;
    long *ptPosition;

    orgf = new TMemoryStream();
    modf = new TMemoryStream();

    orgf->LoadFromFile(".\\test_files\\testfile.vol"); // argv[1]
    modf->LoadFromFile(".\\test_files\\testfile.bak"); // argv[2]

    porjb =(char *) orgf->Memory;
    pfrkb =(char *) modf->Memory;
    changes = 0;

    for (position = 0 ; position < orgf->Size ; position++)
    {
    orjb = *porjb;
    frkb = *pfrkb;
    if (orjb != frkb)
    {
    changes++;

    pPosition = new long [changes];
    if(changes!=1)
    {
    for(int i=0;i< changes-1;i++)
    pPosition[i]=ptPosition[i];
    }

    ptPosition = new long [changes];
    if(changes!=1)
    {
    for(int i=0;i< changes-1;i++)
    ptPosition[i]=pPosition[i];
    }
    if (changes==1) ptPosition[0]= position;
    else
    ptPosition[changes-1]=position;
    }
    porjb++;
    pfrkb++;
    }
    f=fopen("results.txt","w");
    if(!f)
    {
    cout<<"File creating failure"<<endl;
    orgf->Free();
    modf->Free();
    delete[] ptPosition;
    delete[] pPosition;

    return -1;
    }
    for (int i = 0 ; i < changes ; i++)
    fputs((IntToHex(ptPosition[i],1)+"\t").c_str(),f);

    orgf->Free();
    modf->Free();
    delete[] pPosition;
    delete[] ptPosition;
    fclose(f);
    cout<<"File compare finished. Result(s) writed to \"results.txt\" "<<endl;
    system("PAUSE");
    return 0;
    }
    //---------------------------------------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. POSIX Threads and dynamic memory allocation
    By PING in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2009, 10:28 AM
  2. Copy .dat file to dynamic memory...
    By IndioDoido in forum C Programming
    Replies: 5
    Last Post: 05-28-2007, 04:36 PM
  3. dynamic memory allocation problem
    By firyace in forum C Programming
    Replies: 4
    Last Post: 05-23-2007, 09:57 PM
  4. Issues with free and dynamic memory
    By dan s in forum C Programming
    Replies: 3
    Last Post: 01-14-2007, 02:44 AM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21