The OpenNET Project / Index page

[ новости /+++ | форум | теги | ]




Версия для распечатки Пред. тема | След. тема
Новые ответы [ Отслеживать ]
Динамический массив, !*! sandy, 23-Сен-05, 21:03  [смотреть все]
Есть массив

char** array;
array=new char*[10];

Могу ли я его увеличить, не повреждая хранящихся в нем данных?

  • Динамический массив, !*! DeadMustdie, 21:48 , 23-Сен-05 (1)
    >Есть массив
    >
    >char** array;
    >array=new char*[10];
    >
    >Могу ли я его увеличить, не повреждая хранящихся в нем данных?

    Можно скопировать в новый массив. Увеличить размер существующего нельзя.
    И вообще подобная конструкция дурно пахнет - утечками памяти, например.
    STL forever и всё такое.

  • Динамический массив, !*! chip, 09:46 , 24-Сен-05 (2)
    >Есть массив
    >
    >char** array;
    >array=new char*[10];
    >
    >Могу ли я его увеличить, не повреждая хранящихся в нем данных?

    можно использовать realloc():

    The realloc() function changes the size of the previously allocated mem-
    ory referenced by ptr to size bytes.  The contents of the memory are
    unchanged up to the lesser of the new and old sizes.  If the new size is
    larger, the value of the newly allocated portion of the memory is unde-
    fined.  If the requested memory cannot be allocated, NULL is returned and
    the memory referenced by ptr is valid and unchanged.  If memory can be
    allocated, the memory referenced by ptr is freed and a pointer to the
    newly allocated memory is returned.  Note that realloc() and reallocf()
    may move the memory allocation resulting in a different return value than
    ptr.  If ptr is NULL, the realloc() function behaves identically to
    malloc() for the specified size.

    Однако, для CPP более красивое решение указал DeadMustdie


    • Динамический массив, !*! sas, 11:18 , 24-Сен-05 (3)
      >>Есть массив
      >>
      >>char** array;
      >>array=new char*[10];
      >>
      >>Могу ли я его увеличить, не повреждая хранящихся в нем данных?
      >
      >можно использовать realloc():
      >
      >The realloc() function changes the size of the previously allocated mem-
      >ory referenced by ptr to size bytes.  The contents of the
      >memory are
      >unchanged up to the lesser of the new and old sizes.  
      >If the new size is
      >larger, the value of the newly allocated portion of the memory is
      >unde-
      >fined.  If the requested memory cannot be allocated, NULL is returned
      >and
      >the memory referenced by ptr is valid and unchanged.  If memory
      >can be
      >allocated, the memory referenced by ptr is freed and a pointer to
      >the
      >newly allocated memory is returned.  Note that realloc() and reallocf()
      >may move the memory allocation resulting in a different return value than
      >
      >ptr.  If ptr is NULL, the realloc() function behaves identically to
      >
      >malloc() for the specified size.
      >
      >Однако, для CPP более красивое решение указал DeadMustdie

      Malen'koe utochnenie: Reshenie s realloc trebuet ne ispol'zovat' operatori new/delete, a funkcii C dlya videlenia pamjati.

      Udachi
      --- sas

      • Динамический массив, !*! sandy, 11:24 , 26-Сен-05 (4)
        >Malen'koe utochnenie: Reshenie s realloc trebuet ne ispol'zovat' operatori new/delete, a funkcii
        >C dlya videlenia pamjati.

        В том то и дело, что в программе еже задействованы операторы new/delete, и лень, знаете ли, переписывать это на С.
        Кроме того, этот массив - член структуры, а структура - член вектора. Была мысль воспользоваться вектором. Но получается что-то вроде этого:

        struct STR {
        /*  */
        std::vector<char*> v;
        // char** v; // альтернативный вариант
        };

        std::vector<STR> sv(10);

        В случае с массивом (определить его размер) просто:
        sv[0].v=new char*[10];

        А вот sv[0].v.resize(10); - не получается пока

  • Динамический массив, !*! sinus, 01:16 , 28-Сен-05 (7)
    >Есть массив
    >
    >char** array;
    >array=new char*[10];
    >
    >Могу ли я его увеличить, не повреждая хранящихся в нем данных?

    НОРМАЛЬНЫХ решиний только два:
    1 односвязный\многосвязный список
    2 STL (vector,list...)




Партнёры:
PostgresPro
Inferno Solutions
Hosting by Hoster.ru
Хостинг:

Закладки на сайте
Проследить за страницей
Created 1996-2024 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру