tags:

views:

121

answers:

4

CODE:

struct Stringdata
{
  // Length of data in buffer.
  size_t len;
  // Allocated size of buffer.
  size_t alc;
  // Buffer.
  char data[1];
};

typedef std::list<Stringdata*> Stringdata_list;
Stringdata_list strings_;

Stringdata *psd = this->strings_.front();

//...
if (len > psd->alc - psd->len)
alc = sizeof(Stringdata) + buffer_size;
else
{
  char* ret = psd->data + psd->len;
  memcpy(ret, s, len - sizeof(Stringpool_char));
  memset(ret + len - sizeof(Stringpool_char), 0,
     sizeof(Stringpool_char));

  psd->len += len;

  return reinterpret_cast<const Stringpool_char*>(ret);
}

In the code sample above, I have confused about the operations in the else

branch.

Does it create a new element and insert it after the front element or just place a new element after within the first element of list?

+1  A: 

Your code appears to do neither. The code in the else branch does not modify the strings_ structure at all. The code is only modifying the element return from the front of the list. This should have no affect on the actual list structure.

JaredPar
It's a list of pointers, so it is modifying the thing pointed to by the front list element.
anon
@Neil: If iterating the new list,does the modified list have to take care of the first element or iterate as usual?
Jinx
@Neil, yes but this should not affect the structure of the list with respect to ordering. That appears to be what the OP is asking.
JaredPar
A: 

It doesn't create a new element -- just appends data from s to the data that's already in the front element, if there's space. Very confusingly written code, though.

Alex Martelli
A: 

The fact that you are confused by what your own code does is an indicator that it is wrong, if only because it is incomprehensible. The use of reinterpret_cast is a lso a sign of deeply suspect code. Could you explain in english what you are trying to do?

anon
I'm reading others' code ,and wonder if that's right to manipulate a list.
Jinx
The standard library provides all sorts of ways of manipulating lists - you don't have to write (bad) code yourself to do this.
anon
A: 

As far as I can tell (some important code is missing from your excerpt), you have a block of data, which is essentially an array of Stringdata object, and a list<> of pointers into that block. The else block is expanding that array.

You probably would be better off with a vector<Stringdata> rather than a list<Stringdata*>

James Curran