I am using C++ CURL library for sending emails.
My problem is that the text in my email may vary from time to time so I need to change size or dynamically re-allocate my variable:
const char *payload_text[10]
where 10 might be 20 or even 30
how can I change the size of this variable in my program?
By the way this variable must be allocated as I am using the example from CURL library:
https://curl.haxx.se/libcurl/c/smtp-mail.html
Whenever you are faced with a data structure that may need to change size you should be thinking std::vector.
In this specific case, a std::vector<std::string>> is what you want/need.
To pass the vector to a legacy API that expects a C-style array and a size, pass it std::vector::data() and std::vector::size(). A vector is guaranteed to be using contiguous memory, just like a C-style array, so the two interoperate well.
THANKS
It actually works just some points for others with the same problem:
In the function payload_source change the line:
data = payload_text.at(upload_ctx->lines_read)
What I was afraid to do and in the body of your code where you will fill the email array do the following thing:
payload_text.push_back(char_array)
The vector itself should be:
vector <const char*> payload_text
Again thank you all very much
Related
I have a class which just contains a 1D array of set size (17): However this array needs to be for every monitor that a user has and I am unsure whether to simply set say and array of size 10 limiting the user, or whether to try a more dynamic approach.
Excerpt from ScreenArray.h:
private:
unsigned long pixelArray[17];
I would like advise to which method(s) will work best for my problem and how would I go about constructing / allocating and accessing it?
EDIT: The array (Or the 2D part of the array) preferably dynamic to the size of the monitors currently connected. This contains the 1D array of set size 17.
As UnholySheep stated out, vector is a great tool to use in this situation:
Variable size;
Out of range protection;
Automatic memory management;
...
The only drawback I see may be a loss in performance, but because your vector is so small, you won't even be able to measure it, so this doesn't count.
You can use it like so:
// You'll need this one
#include <vector>
// Create you vector
// Replace TYPE with the type of it's members.
std::vector< TYPE > pixelArray;
// Add members to it
pixelArray.push_back( MEMBER );
// Access it as usual
pixelArray[n];
Hi I am using a C compiler(GCC) where I cannot use a vector like in C++. So how can I create similar kind of data structure/dynamic array with c which will work like a vector? It might be very easy but I don't have any idea how can I do it.
thanks
Start with a struct holding a pointer to the correct type the currently used size, and the current allocation size. Allocate space with malloc. If you run out of space, use realloc to increase it.
Currently I read arrays in C++ with ifstream, read and reinterpret_cast by making a loop on values. Is it possible to load for example an unsigned int array from a binary file in one time without making a loop ?
Thank you very much
Yes, simply pass the address of the first element of the array, and the size of the array in bytes:
// Allocate, for example, 47 ints
std::vector<int> numbers(47);
// Read in as many ints as 'numbers' has room for.
inFile.read(&numbers[0], numbers.size()*sizeof(numbers[0]));
Note: I almost never use raw arrays. If I need a sequence that looks like an array, I use std::vector. If you must use an array, the syntax is very similar.
The ability to read and write binary images is non-portable. You may not be able to re-read the data on another machine, or even on the same machine with a different compiler. But, you have that problem already, with the solution that you are using now.
The constraint is that it should be contiguous memory. The reason is, this is being sent to another language legacy code, which expects it in that format as a 2D array.
so esentially i want to send
char *temp[20] = { "abc", "def"};
etc where abc, def are part of a space thats 20 byte length.
Now, i would like to dynamically create this array and add as many 20 character or less strings to it.
And then send that as an array into the different layer.
Whats the best way to do this.
std::vector< std::array<char, 20> > will do the trick
vector allows dynamically allocating more memory for more strings, the type std::array<char,20> ensures that each member of the vector is indeed 20 characters (make sure to verify boundaries on copy etc, as with any array).
This is for newer C++ standard, IIRC, so older compilers might not support it. Use boost.array instead, then.
A C function expects an array of buffers to be in scope at runtime. e.g.
char values[x][y]
The C function will populate the buffers
I would like to use a dynamic array so I don't have to hard code the dimensions
How do I use a std::vector in this situation?
Just to be clear, I am using C++. The C function is contained in a library that I cannot modify.
If you just want to pass the dynamic array encapsulated in a std::vector to a c routine you can pass a pointer to the head of the underlying array as:
std::vector<char> myvector;
// size-up myvector as needed
foo(&myvector[0]); // pass a pointer to start of myvector to function foo
The c++ standard ensures that the underlying array in std::vector is always contiguous.
Hope this helps.
EDIT: While the declaration char values[x][y] creates an "array of arrays" the memory for values will actually just be a contiguous block, essentially char linear_values[x * y].
If you size your std::vector to include a count of x * y elements it should give you the same underlying dynamically allocated array space.
The c function will access the array in row-major order, so the first row of elements will come first, followed by the second full row etc...
C doesn't have standard data structures libraries.
If you really want all the functionality of a vector, and it's not for something critical, you can probably find someone's pet implementation of a straight C vector online and just use that.
If it is critical, write your own. It's not too hard, and can be quite useful.
If you just want a dynamically growing array, it's easy to emulate that behavior of a vector using the realloc function, which extends the dimensions of a heap-allocated array. Start with a small array, and grow as needed when you reach the end. It's more efficient to grow in big chunks, but if you have some idea of what your data looks like you could grow it in a different way. A common method is doubling the array size every time you run out.
You can get the details of realloc at:
http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/
or, on a *nix system:
man realloc
You can't.
By definition, C knows nothing of any of the required components of a std::vector, including, but not limited to:
C does not have namespaces, so it can't understand the std namespace.
C does not have templates, so it can't understand the std::vector<T> type.
Essentially, you need what looks like a C function, but that is, for all intents and purposes, a C++ function.
The simplest way to achieve this is probably to write what looks like a C function, using C++, and running the whole mess through a C++ compiler rather than a C compiler.