So I've got "m_starts" as a pointer to an int (int * m_starts;).
And a const char * of "m_str".

m_str = "bad,cold,new".

How can I allocate "m_starts" to store the starting index of each word in m_str???

For loop?

Dani AI

Generated

A compact, robust way to collect the start index of each comma-separated token is to treat the input as an std::string and push each token start into an std::vector<size_t> while scanning once. This applies 's idea (find token boundaries) but avoids a separate count-and-then-allocate step, and it removes manual memory-management concerns that hinted at. Note that dynamic allocation works for any type (as clarified to ); prefer standard containers to avoid mistakes with new/delete[].

Example approach (records empty tokens for consecutive or trailing commas; adjust if empty tokens should be ignored):

std::string s = m_str;                // copy if m_str points to a literal
std::vector<size_t> starts;
if (!s.empty()) {
    size_t start = 0;
    while (true) {
        starts.push_back(start);
        size_t comma = s.find(',', start);
        if (comma == std::string::npos) break;
        start = comma + 1;
    }
    // optional: if trailing comma should not count as an extra token
    if (!starts.empty() && starts.back() == s.size()) starts.pop_back();
}

Important notes and edge cases:

  • If m_str is a string literal (const char*), do not attempt to modify it (strtok or manual writes). Copy it into std::string before any tokenizing that mutates the buffer.
  • Decide whether empty tokens (consecutive commas or trailing comma) should be included; the snippet above includes them by default.
  • Use size_t for indices to avoid signed/unsigned mismatches; if raw pointers are used, compute an index as ptr - m_str.
  • If manual allocation is chosen instead of std::vector, free arrays with delete[] (not plain delete).

Preferred final note: std::vector/std::string is simpler, safer, and clear to future readers compared with raw arrays and manual new/delete.

Recommended Answers

All 10 Replies

First you have to find out how many comma-separated words are in the string. Iterate through the string and count the commans. Then allocate the int array large enough to hold that many integers. After that, go through the string agsin, this time when you find a comma set one of the integers to be the numeric value of where the comma was found.

For example in the string you posted you will need three integers because the string contains three comma-separated words. The first integer will be 0, the second integer 4 and the 3d integer is 9.

Understood, but what I'm asking is how do i allocate it to the size i want (in this case 3)??

I mean I can set "m_starts[3];" or even run through a for loop so i leave it open to any number of sizes, but what I DON'T understand is how to then use it to store the starting index of each word in m_str. How do i go about that process??

>>how do i allocate it to the size i want
This is c++ so use the new operator to allocate the array m_starts = new int[NumberOfIntegers];

dont forget to use delete to deallocate the memory. :)

But I though DMA is for integers contain in an array only? Characters won't work for DMA....I've tried it before

But I though DMA is for integers contain in an array only? Characters won't work for DMA....I've tried it before

Huh? I assume DMA means Direct Memory Address. How does that relate to the discussion of this thread ? BTW DMA is not possible with any 32-bit compiler unless writing a kernel-mode device driver.

Huh? I assume DMA means Direct Memory Address. How does that relate to the discussion of this thread ? BTW DMA is not possible with any 32-bit compiler unless writing a kernel-mode device driver.

I think he means 'Dynamic Memory Allocation' - I could be wrong though. Either way, it works fine with both char and int

My mistake...DMA - Dynamic Memory Allocation.

But I though DMA is for integers contain in an array only? Characters won't work for DMA....I've tried it before

Now that I know what you mean by DMA -- it works with any data types, either simple or complex. If you couldn't make it work then its your fault not that of the language.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.