1

When going through a C++ code I came across a line where the developer is trying to add a new cell to a char** variable. While doing so, following is the line of code he is using to allocate memory for the new cell.

*(plantValue + (plantCount-1)) = (char *) malloc(sizeof(char) * SAPPlantStr.length()); 

where plantValue is char** and plantCount is an integer.

Could someone please explain me the above line of code as I am unable to understand it.

Thanks in advance.

4
  • 9
    For any pointer or array p and index i, the expression *(p + i) is equal to p[i]. In your specific case it means that *(plantValue + (plantCount-1)) is equal to plantValue[plantCount-1]. Hopefully that should make it clearer for you. Commented Jul 3, 2017 at 10:57
  • 4
    Also, you should probably throw that code away if you use it to learn C++. "Proper" C++ would be to use std::string for strings, and std::vector for a dynamic "array". Commented Jul 3, 2017 at 10:58
  • 4
    Oh and there's probably a bug in the code you show, as the allocation doesn't include space for a terminating '\0' character. Commented Jul 3, 2017 at 10:59
  • Thank you for response. I have got my doubt clarified. Commented Jul 5, 2017 at 13:23

2 Answers 2

2

It looks like plantValue in your code is a pointer to an array of pointers to raw C-style strings, i.e.:

 +----------+ plantValue --> | char * | --> String #1 (char **) +----------+ | char * | --> String #2 +----------+ | ... | +----------+ | char * | --> String #N +----------+ 

So, that code is basically allocating a new C-style string using malloc(), and storing a pointer to it in a slot in the char* vector represented above.

In particular, plantCount-1 is the index of the first available slot in the pre-allocated pointer array; plantValue + (plantCount-1) points to that slot, and with *(plantValue + (plantCount-1)) = ... you write the string pointer returned by malloc() in that slot.

Please note that the above code is more C than C++; for example, in C++ you would use new[] instead of malloc() for explicit dynamic memory allocations; but, even better, in C++ you should use standard container classes likes std::vector, and std::string instead of raw char* owning pointers. These C++ classes automatically manage their own memory, and simplify your code a lot.

P.S. Note also that C-style strings are NUL-terminated, so when you allocate memory for a new string with malloc(), you should consider also the terminating NUL when you compute the total length (in other words, you should have SAPPlantStr.length() + 1 in your code).

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

1 Comment

Thank you for response. I have got my doubt clarified.
0

plantValue[plantCount-1] contains a pointer to allocated memory with length of SAPPlantStr

1 Comment

And that's how he should have written it. The pointer dereferencing syntax is confusing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.