-1

when I want to use C lib function like strtok, I found it provided a point of heap, I want to manage this heap pointer with CPP RAII. but I found I cannot free it, is it someone managing this part of memory? or how could I confirm its lifecycle?

enter image description here

0___________
  • 60,014
  • 4
  • 34
  • 74
Jesse
  • 517
  • 5
  • 24
  • 4
    Please [edit] and show your code as text, not as a picture, and preferably as a [mcve]. That being said, `strtok` does not manage any memory, _you_ do. Also is this C or C++? Use only one tag. Your code you didn't show certainly calls `free` with a pointer that has not been allocated via `malloc`. – Jabberwocky Mar 08 '23 at 10:21
  • You are managing the memory of C lib like strtok, as strtok modifies it's first argument. – KamilCuk Mar 08 '23 at 10:22
  • 3
    `strtok` does not allocate memory. It returns a pointer to somewhere within the string you pass to it (or `NULL`). So it is wrong to `free` the pointer it returns. – Weather Vane Mar 08 '23 at 10:24
  • Your code is not C. Do not tag it as C (check if it will compile as C) – 0___________ Mar 08 '23 at 10:24
  • 1
    Why do you think `strtok` allocates on the heap? In any case, I wouldn't use this function at all, since it relies on a global variable and breaks when used concurrently ([explanation](https://stackoverflow.com/q/5999418/2752075)). – HolyBlackCat Mar 08 '23 at 10:26
  • Apparently you like to parse something. You should describe this and do not use `strtok` or at least switch to `strtok_s` and [read docs](https://en.cppreference.com/w/c/string/byte/strtok). – Marek R Mar 08 '23 at 11:07
  • @MarekR: or better: `strtok_r`. – chqrlie Mar 10 '23 at 09:17

1 Answers1

6

strtok does not have anything to do with the heap. It does not allocate memory, it returns a pointer inside the array of char passed as the first argument to the last call with a non null first argument. This array is modified as a side-effect.

This C function has confusing and error prone semantics, it is non reentrant and not thread safe on many targets.

I recommend you do not use this function at all, especially in c++ code where you want to implement RAII.

There should be c++ alternatives available as methods of the std::string type.

If you want to program in C, consider alternatives such as strtok_r, strsep or analyzing the string using strspn and strcspn, which to not modify the source string, and allocating the tokens from the heap with strndup.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Thank you for your answer, I checked the source code, `strtok` donot request new memory, just divide the` char *` with `end` token `\0`. so if we got `int foo()`, when it consider foo is a token it will insert the `char *` like `int foo\0)`, and return the position of foo. so it should be memory safe because the memory is managed by CPP (actually a string type). – Jesse Mar 10 '23 at 02:04
  • @Jesse: Studying the source code is a good approach. You are correct: `strtok` modifies the `char` array it received a pointer to and returns a pointer inside this array or `NULL` if no more tokens can be found. Yet *the memory is managed by CPP* is a misconception: `strtok` is an archaic C function, it comes from a world where memory is managed by the programmer. Using this function with memory managed by the `std::string` type is a recipe for failure because it will be the programmer's responsibility to track the life cycle of the pointers and the internal string data: the opposite of RAII. – chqrlie Mar 10 '23 at 06:53
  • yep, different lifecycle is key point, I will not mixture them:) – Jesse Mar 13 '23 at 10:07