dynamically allocating space for array of c strings

I have 2 classes, class CreateData and class SaveData.

CreateData does just that - it creates and uses a linked list with data std::string str_val;.
SaveData saves the current values in the CreateData list to a file in binary mode. AFAIK, to read/write in binary, it is necessary to use char*
as the fundamental type (
1
2
outf.open(file_, std::ios_base::out | std::ios_base::binary);
    outf.write((char *)&variable, sizeof variable);
)

class SaveData has a nested structure struct save_data that holds all the necessary info to be saved. The number of nodes in the CreateData list varies over the course of the program, so the corresponding pointer-to-array of c-string variable in SaveData::save_data struct has to be dynamically allocated. I'm at a loss for how to do this. Any help would be greatly appreciated.
Sample code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class CreateData
{
  private:
  enum {MAX_SIZE=50};
  struct Sequence { std::string value; Sequence * next; };
  Sequence * first_;
  Sequence * last_;
  int NodeCount_;
  const int MaxNodeCount;

  public:
  CreateData( int sz=MAX_SIZE);
  int getNodeCount() const;
  void addValue(const std::string& str);
  std::string processValue();
  //...
  friend int SaveData::getCount(CreateData * cd);
  friend std::string SaveData::getValues(CreateData * cd);
};

std::string CreateData::processValue()
{
  std::string str = first_->value;
  NodeCount_--;
  Sequence * temp = first_;
  first_ = first_->next;
  delete temp;
  return str;
}

class SaveData
{
  private:
  std::ofstream outf;
  const char * file_;
  struct save_data
  {
    int ID_;
    int totalNodes_;
    const char ** NodeValues_; //this is what I'm unsure of
  };

  public:
  SaveData();
  save_data sd;
  //...
  int getCount(CreateData * cd) { return (*cd).getNodeCount(); }
  void getValues(CreateData * cd);
};

void SaveData::getValues(CreateData * cd)
{
  //sd.totalNodes_ = getCount(CreateData * cd);
  const int ROW = sd.total_cards_;
  const int COL = 5;
  char ** temp = 0;
  *temp = new char[ROW];
  for(int i=0; i < limit; ++i)
  {
    temp[i] = new char[COL];
    temp[i] = (*cd).processNode().c_str();
  }
    sd.cards_in_deck_ = temp;
}


I've tried many ways to implement this; however, I end up with errors every time (they always originate from the SaveData::getValues function.
invalid conversion from char** to const char**
invalid conversion from char* to char
error: assignment of read-only location '*(temp + ((unsigned int)i))


EDIT: Updated function.
Last edited on
1
2
3
4
5
6
7
8
const int ROWS = 8;
const int COLS = 12;
int **tmp = 0;
tmp = new int[ROWS];
for(int i = 0; i < ROWS; ++i)
{
  tmp[i] = new int[COLS];
}
Also
1
2
3
4
5
6
7
8
const int a = 5;
int b = 10;
char (*str)[a];
str = new char[b][a];

...

delete [] str;


You can make them different, but I was having memory leaks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

int main(void)
{
	// not const is the purpose here
	int WORDSL = 2;
	int STR1L = 10;
	int STR2L = 15;
		
	char **str;

	*str = new char[WORDSL];
	str[0] = new char[STR1L];
	str[1] = new char[STR2L];

	for (int i = 0; i < STR1L; i++) str[0][i] = '1';
	for (int i = 0; i < STR2L; i++) str[1][i] = '2';

	str[0][STR1L - 1] = '\0';
	str[1][STR2L - 1] = '\0';

	std::cout << str[0] << std::endl;
	std::cout << str[1] << std::endl;
	
	for (int i = 0; i < WORDSL; i++) delete [] *(str + i);
	// Still has a memory leak	

	std::cin.get();
	return 0;
}

Last edited on
thanks to both of you for your replies!

However, one persistent problem remains: (*p).processNode().c_str() returns const char *, but
temp[ROW][COL] is type char *.

I tried adding const_cast<char *> to the c_str() part, and the program came to a screeching halt (the program froze and the message box that says "such-and-such program encountered an error and needs to close. You can choose the send a technical report to Microsoft..." popped up).

I can't make temp const char, as I would be unable to assign values.

or is there some other way to do it that I'm completely overlooking?
PS - I also need to be able to store this array in a structure and then write the contents of said structure to a file in binary mode, but the structure variable char ** NodeValues_; (line 40) should take care of that part, right?

Topic archived. No new replies allowed.