Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is there a way to allocate memory, where you can increase the size, without losing information?

Is there a way to allocate memory, where you can increase the size, without losing information?

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    I'm doing a project where i have to keep reading a struct from the disk and i have to keep doing this until i come to a special set of bits. I don't know how many structs are on the disk so i need to be able to do something like this:

    unsigned int num_structs = 0;
    struct_name *the_struct;

    do
    {
    num_structs++;
    the_struct = realloc(sizeof(the_struct)*num_structs);
    read_struct(&the_struct[num_structs-1]);
    } while (nextbits() == 'whatever')

    Where the function realloc will preserve the data already written in the memory area. Is this possible? thanks!

    M M 2 Replies Last reply
    0
    • A Anonymous

      I'm doing a project where i have to keep reading a struct from the disk and i have to keep doing this until i come to a special set of bits. I don't know how many structs are on the disk so i need to be able to do something like this:

      unsigned int num_structs = 0;
      struct_name *the_struct;

      do
      {
      num_structs++;
      the_struct = realloc(sizeof(the_struct)*num_structs);
      read_struct(&the_struct[num_structs-1]);
      } while (nextbits() == 'whatever')

      Where the function realloc will preserve the data already written in the memory area. Is this possible? thanks!

      M Offline
      M Offline
      mwilliamson
      wrote on last edited by
      #2

      No. But you can do this: unsigned int size; int* array; for( int i = 0;i < 10;i++ ) { int* temp = array; size++; array = new int[size]; memcpy( array, temp, sizeof(int) * (size-1) ); delete[] temp; }

      1 Reply Last reply
      0
      • A Anonymous

        I'm doing a project where i have to keep reading a struct from the disk and i have to keep doing this until i come to a special set of bits. I don't know how many structs are on the disk so i need to be able to do something like this:

        unsigned int num_structs = 0;
        struct_name *the_struct;

        do
        {
        num_structs++;
        the_struct = realloc(sizeof(the_struct)*num_structs);
        read_struct(&the_struct[num_structs-1]);
        } while (nextbits() == 'whatever')

        Where the function realloc will preserve the data already written in the memory area. Is this possible? thanks!

        M Offline
        M Offline
        Moak
        wrote on last edited by
        #3

        have a look on std::vector http://www.msoe.edu/eecs/ce/courseinfo/stl/vector.htm

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups