1

As someone indicated, it looks using vectors of arrays is usually more reasonable than using array of pointers; So here I have an array of pointers which I'd like to convert to vectors of arrays:

char ** ptr;    
char * ptrContiguous;

ptr = new char*[numChannels];
ptrContiguous = new char[x*y*byteSize*nC*nM*nZ*nT];
char * p = ptrContiguous;

for(int i = 0; i < numChannels; i++)
{
    ptr[i] = p;
    p += x*y*byteSize;                          

}

My questions are: only ptr needs to be converted to vector right? and someone can write some simple code illustrating the array to vector conversion? Thanks.

6
  • 6
    You'd be even better off converting this to a vector of strings - std::vector<std::string>. Commented Nov 22, 2013 at 19:47
  • @DanielKamilKozar: can you write some simple code that will really help. Thanks. Commented Nov 22, 2013 at 19:49
  • @NickXTsui std::vector<std::string> myvec( ptr, ptr+numChannels); Commented Nov 22, 2013 at 19:53
  • @KitsuneYMG then the rest is the same? How do I do the for loop to assign ptr[i] in the sense of myvec? Commented Nov 22, 2013 at 19:59
  • And ptr should be still kept as a char**? I thought the point is to use a vector to replace a char*[] Commented Nov 22, 2013 at 20:02

2 Answers 2

2

This is your actual code :

char ** ptr;    
char * ptrContiguous;

ptr = new char*[numChannels];
ptrContiguous = new char[x*y*byteSize*nC*nM*nZ*nT];
char * p = ptrContiguous;

for(int i = 0; i < numChannels; i++)
{
    ptr[i] = p;
    p += x*y*byteSize;                          

}

Now, if you use vector from the STL, your code becomes this one :

std::vector<std::string> ptr;
ptr.resize(numChannels);

std::string ptrContiguous;
ptrContiguous.resize(x*y*byteSize*nC*nM*nZ*nT);

const int part_size = x*y*byteSize;
for(int i = 0; i < numChannels; i++)
{
    ptr[i] = std::string(ptrContiguous.begin() + i * part_size, ptrContiguous.begin() + (i+1) * part_size);                          
}

Also, this link about vectors and about strings should help you. This is the code I suggest you without knowing what's the purpose of ptrContiguous.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot. But the function I used to use like delete ptr[i], or return ptr[i], all these return errors, seems string to char* has problems?
@NickXTsui with vectors and strings, you don't need delete and new. If you want to return ptr[i], then your function definition should be this form : std::string getPtrAtPosi(/*something here maybe*/) { ... }. If you want to return ptr, the function should be like this form : std::vector<std::string> getPtrAtPosi(/*something here maybe*/) { ... }
My pointers pointing to char*. basically a piece of buffer representing images. So std::string can represent images?
@NickXTsui if you really want a vector of bytes (values between 0 and 255), in that case I suggest you to use std::vector<unsigned char> instead of std::string.
1

Try this (renamed some variables for code clarity, using C-style memory management since this is essentially C code, although let me know if you are unfamiliar with malloc and free):

char **short_strings; // short_strings[i] is the ith "short string"
char *long_string;

ptr = malloc(sizeof(char*) * num_short_strings);
long_string = malloc(sizeof(char) * num_short_strings * short_string_length);

char *p = long_string;

for(int i = 0; i < num_short_strings; i++)
{
    short_strings[i] = p;
    p += sizeof(char) * short_string_length;
}

Note that neither C++ new/delete nor C-style malloc and free will allow you to deallocate the memory for short_strings[i] (for example by calling free(short_strings[i]) or delete[] short_strings[i]. This is because these dynamic memory allocators hand out memory in chunks, and free and delete only allow you to delete the entire chunk you were handed. If you want to be able to delete the short strings individually you will need to reallocate memory per short string, and strcpy, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.