fill buffer

Pages: 12
i have this code but i don t understand why my code crashhh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <vector>
#include <string>
#include <inttypes.h>
#include <cstring>
#include<iostream>
#include <iostream>
#include<ostream>

int main()
{
    std::vector<char> data(520);
    std::string testValue="##############__HEADER__\nprotocol-version:0.1\ncontent-length:24\n\n";
    std::string tmp(data.data());
    int dt ;
    int rt=0 ;
    int counter{1};

    dt =data.size();

   std:: cout << tmp << std::endl;

    std::string str4= tmp.substr(65,4);// find the position of 24.

    std::cout<<"\n\n"<< str4 << std::endl ;

    std::string::size_type love;

    int i_str4= std::stoi(str4,&love);// convert in integer.

    std::cout << i_str4<< std::endl;

for (int i=0; i<dt ;i++)
{
        memccpy(data.data()+rt, str4.data(),sizeof(char),i_str4);
         rt+= str4.size();
}

    for (auto lc:data)
    {
        std::cout<<"\"buffer\"element"<< counter << "."<<lc<<'\n';
       counter ++;
    }

	return 0;
}

a small resume: my code take 24 and fill in buffer , then return 0 when my buffer is full.
thank'ss!!!!!!
Last edited on
std::string str4= tmp.substr(65,4);// find the position of 24. this appears to be incorrect. double check it.

it works if you hard code str4 to a value there so the tmp statement is wrong.
Last edited on
When vector "data" is initialized it is 520 chars long and they are all set to zero. When you try to initialize string "tmp" from data.data(), the constructor will just see an empty string (first byte is zero) so it creates an empty string. Then you look for your substring in tmp, which is empty. That substr call will crash. You should presumably look in the string testValue for the substring.

It is very strange to pass sizeof(char) (i.e., 1) as the third parameter to memccpy. The third parameter is supposed to be the character to stop at.

It looks like there may be some logic errors, too. What exactly is this code supposed to do?
my code take 24 and fill in buffer , then return 0 when my buffer is full.
thank'ss!!!!!!
Not even close.

Line 22: tmp is a string filled with 0. So you have a string with 4 0's.
Line 28: This will certainly throw an exception.
Line 32: I have no idea what this is supposed to do.

sizeof(char) is wrong there.
i_str4? Your intention was the value 24. But how does it make sense?
The loop goes until dt but since you add 4 to rt it will go out of bounds soon... (i.e. crash)
@coder777, tmp isn't a string filled with zeroes. It's an empty string (size 0).
Last edited on
@jonnin it's work and tmp it isn't empty.

@tpb Do you have another solution?

@coder777 i use size of (char )because i use memccpy and i_str4 i convert string in interger.
how i add 4 to rt ?



"buffer"element1.2
"buffer"element2.4
"buffer"element3.

"buffer"element4.

"buffer"element5.2
"buffer"element6.4
"buffer"element7.

"buffer"element8.

"buffer"element9.2
"buffer"element10.4
"buffer"element11.

"buffer"element12.

"buffer"element13.2
"buffer"element14.4
"buffer"element15.

"buffer"element16.

"buffer"element17.2
"buffer"element18.4
"buffer"element19.

"buffer"element20.
....
"buffer"element505.2
"buffer"element506.4
"buffer"element507.

"buffer"element508.

"buffer"element509.2
"buffer"element510.4
"buffer"element511.

"buffer"element512.

*** Error in `/home/


1
2
  std::string tmp(data.data());
  cout << "tmp size: " << tmp.size() << "\n";

Output:
tmp size: 0

The string will consider the first 0 in data as the end of data().

Why do you use a vector<char> instead of a string ?
Your code looks too complicated.
What do you actually want to do ?
i rewrite the code , I realize my errors
i come back to you later.
thank you
i use size of (char )because i use memccpy and i_str4 i convert string in interger.
how i add 4 to rt ?
You do not understand how memccpy works. See:

http://man7.org/linux/man-pages/man3/memccpy.3.html

The third paramenter (where you currently put sizeof(char)) must be a character (like 'A') which determines where to stop copying
The fourth parameter determines the maximal length to copy.

With the code you posted it is impossible to produce such an output.

@tpb
You are right.

See this:
1
2
3
4
5
for (int i=0; i<i_str4 ;i++)
{
        std::copy(str4.begin(), str4.end(), data.begin() + rt);
         rt+= str4.size();
}
if str4 contains "24" it would copy "24" 24 times to data.
@Thomas I would like to make a code which got a data eg : "##############__HEADER__\nprotocol:1\ncontent-length:24\n\n"

My code has to take the value of the content-length which is 24 and stores him in the buffer in this first case and after when we enter a new value ("##############__HEADER__\nprotocol:1\ncontent-length:30\n\n") it recupures still the content-length which will be 30 and this stores him in the buffer following 24 if 24 set place 0 1 then 30 will take 2 3. one time the full buffer it sends back I am full and we can nothing more store.
To get the content length you can do like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int get_content_length(const string& s)
{
  string pattern("content-length:");
  auto pos = s.find(pattern);
  auto pos2 = s.find("\n", pos + pattern.length());

  if (pos != string::npos && pos2 != string::npos)
  {
    string tmp = s.substr(pos+pattern.length(), pos2 - pos);
    int retval = stoi(tmp);
    return retval;
  }

  return -1;
}

int main()
{
  
  std::string testValue = "##############__HEADER__\nprotocol-version:0.1\ncontent-length:24\n\n";

  cout << "Length: " << get_content_length(testValue);
 
  return 0;
}

Easier might be with regular expressions, not sure if you know them.
Would storing the old and new content length in a vector<int> and option ?
@Thomas1965 i'm a beginner in c++:
When I test your code he does not work, the error message mark undefined reference
1
2
3
4
5
6
7
8
9
#include <vector>
#include <string>
#include <inttypes.h>
#include <cstring>
#include<iostream>
#include <iostream>
#include<ostream>
#include <memory>
#include <algorithm> 

to store is possible to use vector<int>
What compiler do you use ?
It works on VS 2017.
What error messages do you get ?
i use qt creator
the error messages is undefined reference
Undefined reference to what?

The linker doesn't output things because it thinks your screen looks prettier with lots of text. It outputs things because those things are useful information in helping to fix the problem. So pass that help onto us.
@ Thomas1965 it's work when the block is together , but non with class.
Do you have a proposition to store
it's work when the block is together , but non with class.
Not really sure what you mean.

I guess this code is only part of some project.
If you tell us the whole story we might be able to find a solution.
are you messing up memcpy? Your use of it seems questionable at best, but generally I take it that anyone using that function is an expert and knows what they are doing. Memcpy and classes don't mix well, you have to be very careful, and that includes built in classes like vector/string/etc.

consider replacing it with a dumb copy loop to debug, and if that solves the problem, revisit your use of memcpy to see what you did wrong or if you shouldn't even be using it.
Last edited on
@Thomas1965
The purpose of the program is to provide to feign a parser. I request a tram of data, I recuperate its data (where from the size 24) then I store this value in my buffer, then a new data arrives, I copy in the buffer the size and so on until my buffer is full and my program sends back full memory.
@Thomas1965 and coder777
I combined your two proposals and I obtained this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
int parser::putData(std::vector<char> data)
{
   

    std::string tmp(data.data());
    int dt ;
    int rt=0 ;
    int counter{1};
    string bonj;
    int bons;
    cout << tmp << endl;
string allo ("content-length:");
auto pos =tmp.find(allo);
auto pos1 =tmp.find_first_of("\n",pos+allo.length());
if (pos != string::npos && pos1!= string::npos)
{
  bonj = tmp.substr(pos+allo.length(),pos1-pos);
  cout << bonj<< endl;
  bons= stoi(bonj);
}
   for (int i=0; i<128;i++)
{
        std::copy(bonj.begin(),bonj.end(),data.begin()+rt);

        rt+= bonj.size();
}

    for (auto lc:data)
    {
        cout<<"\"buffer\"element"<< counter << "."<<lc<<'\n';
        counter ++;
    }
   return 0;

}
#include "tcp_parser.h"

#include <vector>
#include <string>
#include <inttypes.h>
#include <cstring>
using std::vector;
using namespace std;

int main()
{
    std::vector<char> testData(512);


    std::string testValue ="##############_HEADER__\nprotocol:0.1\ncontent-length:24\n\n";
 memccpy(testData.data(), testValue.data(), sizeof(char), testValue.size());



    if (parser.putData(testData) == 0)

    {

        cout << "\n success!\n ";
    }
    else
    {
        cout << "No success!" << endl;
    }



    return 0;
}




however when I change the size of my data (214) it's bug
Last edited on
Pages: 12