-2

I need an array to store char arrays of variable size. I could use vectors or anything else, but unfortunately this is for a MPI project and I am forced to use an array so I can send it using MPI::COMM_WORLD.Send(...) function.

My idea comes from this link.

This is a simplified example of the problem I have:

char* arrayStorage[3]; //I want to store 3 char arrays of variable size!
int index = 0;

char array_1[RANDOM_SIZE] = {.....};
char array_2[RANDOM_SIZE] = {.....};
char array_3[RANDOM_SIZE] = {.....};

arraySorage[index] = array_1;
index++;    
arraySorage[index] = array_2;
index++;
arraySorage[index] = array_3;
index++;

I have also seen people talking about malloc and stuff like that, but I don't know much about pointers. I do malloc, I have to call free and I don't know where, so I am avoiding that for now.

This code obviously doesn't work, array_1, array_2, array_3 are all OK, but when I try to access them I get garbage. The problem seems to be inside the index variable. Maybe I shouldn't be doing index++, perhaps I should be doing index += RANDOM_SIZE, but that also fails.

How can I store variable size char arrays in an array?

Community
  • 1
  • 1
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
  • 4
    Nononononooooo. You should simply get yourself at learning how to use malloc and free, it's not that hard. –  Oct 05 '12 at 16:06
  • @BleepBloop: depends. With MPI in the mix, he might actually need `MPI_Alloc_mem`, which is sometimes exactly equivalent to `malloc`. – willglynn Oct 05 '12 at 16:10
  • 2
    I don't know about MPI, but the contents of a std::vector _is_ an array, right? Whatever send function you're passing the data to, you can pass it the std::vector<>::data() pointer (with the vector's size), and that's clearly equivalent to allocating an array yourself and passing that. – Nicholas Wilson Oct 05 '12 at 16:12
  • when you initialize arrays like this - `char array_1[RANDOM_SIZE] = {.....};` make sure there's a null-terminating char at the end ('\0') - that's the proper way of dealing with strings in c/c++ – noobed Oct 05 '12 at 16:16
  • I would like to avoid malloc at all costs, mainly because I don't see why I am forced to use it. The lighter the code the better imo. As for vector, I know about it, I wanted to use it, but I simply cant send objects that heavy and complex via MPI, even if they represent something as simple as an array. The '\0' char at the end is not a problem, thanks for reminding me though. – Flame_Phoenix Oct 05 '12 at 16:30
  • 1
    You are not forced to do anything. You should just choose the best tool for the job. And `malloc` is perfectly "light". Remember that understandability is also a factor in "light" code. – tenfour Oct 05 '12 at 17:15
  • try with a stl::vector. http://www.cplusplus.com/reference/vector/vector/ – ANjaNA Jul 20 '15 at 05:15

1 Answers1

2

Use malloc and free (or new and delete in C++). You can do it with vectors too - as vectors can be treated as arrays.

 char *str = "hello world";
 // need the +1 for null character
 arraySorage[0] = (char *)malloc (strlen(str) + 1); 
 strcpy(arraySorage[0], str);
 ...
 free(arraySorage[0]);

with new/delete

arraySorage[0] = new char[strlen(str)+1];

strcpy(arraySorage[0], str);
...
delete arraySorage[0];

Using vector and std::string is the correct C++ way, for lots of reasons, including not leaking memory and proper handling of exceptions.

Julian
  • 852
  • 4
  • 9
  • I can't use vectors, I know of them, I thought of them, but I can't send objects that heavy and complex via MPI, even if they represent a concept as simple as an array. As for the answer, it made me realize that my question was poorly formulated, I thank you for that. I also don't get why I "have" to use malloc or new. Can someone explain? – Flame_Phoenix Oct 05 '12 at 16:28
  • 2
    @Flame_Phoenix: Why do you think you can't use vectors? The MPI::COMM_WORLD.Send function takes a pointer and a length as parameters. You can get both from the vector class. – ScottTx Oct 05 '12 at 16:28
  • Yes you can. As Nicholas Wilson stated above, you can call the std::vector::data method to get a pointer to the underlying array if using C++11. If you are using an older compiler (pre C++11), you can get the pointer by taking the address of the first member of the vector, like this: char* arrayPtr(&myVector[0]); – Shaun Marko Oct 05 '12 at 16:29
  • Using vector like that is a bad idea, as explained here:http://www.thinkingparallel.com/2007/02/08/a-smart-way-to-send-a-stdvector-with-mpi-and-why-it-fails/ . The other only way is to use MPI Boost, which I also can't :S – Flame_Phoenix Oct 05 '12 at 17:00
  • Do you need to send a 1D or 2D array using the MPI function call? – Shaun Marko Oct 05 '12 at 17:01
  • 1
    Using vectors in the way described is not a bad idea -- the C++ standard guarantees that the a std::vector stores its data in contiguous memory. – Shaun Marko Oct 05 '12 at 17:32