Problem with either something pure C++ or something SDL_net

I'm on the edge of a mental breakdown right now, so I can't explain much, but basically commenting out where the following function was called stops my program from segfaulting in a completely different location. The stuff that's already commented out in this function stops it from segfaulting between a single ending curly bracket (didn't even know that was possible)


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
int getStickFigures() {
  //int theStickId, rint[19];
  for (int i=0; i<64; i++) {
		if (SDLNet_UDP_Recv(sd, p))
		{
			printf("UDP Packet incoming\n");
			printf("\tChan:    %d\n", p->channel);
			printf("\tData:    %s\n", (char *)p->data);
			printf("\tLen:     %d\n", p->len);
			printf("\tMaxlen:  %d\n", p->maxlen);
			printf("\tStatus:  %d\n", p->status);
			printf("\tAddress: %d %d\n", p->address.host, p->address.port);
			
			char *test[sizeof(strtok((char *)p->data, " "))];
			test[0] = strtok((char *)p->data, " ");
			if (strcmp(test[0], "stickman")==0) {
			  test[1] = strtok (NULL, " ");
			  test[2] = strtok (NULL, " ");
			  test[3] = strtok (NULL, " ");
			  //test[4] = strtok (NULL, " "); rint[4]=atoi(test[4]);
			  //test[5] = strtok (NULL, " "); rint[5]=atoi(test[5]);
			  //test[6] = strtok (NULL, " "); rint[6]=atoi(test[6]);
			  //test[7] = strtok (NULL, " "); rint[7]=atoi(test[7]);
			  //test[8] = strtok (NULL, " "); rint[8]=atoi(test[8]);
			  //test[9] = strtok (NULL, " "); rint[9]=atoi(test[9]);
			  //test[10] = strtok (NULL, " "); rint[10]=atoi(test[10]);
			  //test[11] = strtok (NULL, " "); rint[11]=atoi(test[11]);
			  //test[12] = strtok (NULL, " "); rint[12]=atoi(test[12]);
			  //test[13] = strtok (NULL, " "); rint[13]=atoi(test[13]);
			  //test[14] = strtok (NULL, " "); rint[14]=atoi(test[14]);
			  //test[15] = strtok (NULL, " "); rint[15]=atoi(test[15]);
			  //test[16] = strtok (NULL, " "); rint[16]=atoi(test[16]);
			  //test[17] = strtok (NULL, " "); rint[17]=atoi(test[17]);
			  //test[18] = strtok (NULL, " "); rint[18]=atoi(test[18]);
			  //test[19] = strtok (NULL, " "); rint[19]=atoi(test[19]);
			  
			  //for (int i=4; i < 20; i++) {
			  //  printf("rint[%d]=%d",i,rint[i]);
			  //}
			  
			  //theStickId=atoi(test[1]);
			  stickman[atoi(test[1])].position.x=atoi(test[2]);
			  stickman[atoi(test[1])].position.y=atoi(test[3]);
			  std::cout << "Right before the input functions...\n";
			  //stickman[theStickId].left_arm.setPoints( rint[4],rint[5],rint[6],rint[7] );
			  //stickman[theStickId].right_arm.setPoints( rint[8],rint[9],rint[10],rint[11] );
			  //stickman[theStickId].left_leg.setPoints( rint[12],rint[13],rint[14],rint[15] );
			  //stickman[theStickId].right_leg.setPoints( rint[16],rint[17],rint[18],rint[19] );
			  std::cout << "asdfasdfasdf\n";
			}
			std::cout << "lastpartofif\n";
		}
		std::cout << "beforeendofor\n";
	}
	std::cout << "absoluteendoffunction\n";
}
Last edited on
ok first off why am I using c techniques for a c++ lib?

This function would be much simpler if you used a vector.

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

int getStickFigures() 
{
   //int theStickId, rint[19];
   for (int i=0; i<64; i++) 
   {
	if (SDLNet_UDP_Recv(sd, p))
	{
	        printf("UDP Packet incoming\n");
		printf("\tChan:    %d\n", p->channel);
		printf("\tData:    %s\n", (char *)p->data);
		printf("\tLen:     %d\n", p->len);
		printf("\tMaxlen:  %d\n", p->maxlen);
		printf("\tStatus:  %d\n", p->status);
		printf("\tAddress: %d %d\n", p->address.host, p->address.port);
			
                // first dump the string someplace useful.
                std::string strIncoming = (char *)p->data;

                std::string strPiece;

                std::vector<std::string> tmpVector;
                
                int nIndex = 0;
                int nLast = 0;
                while (nIndex < strIncoming.length())
                {
                      if(strIncoming[nIndex] == ' ')
                      {
                           strPiece = strIncoming.substr(nLast, nIndex - nLast);
                           tmpVector.push_back(strPiece);
                           strPiece.clear();
                           nLast = nIndex;
                      }
                      nIndex++;        
                }
                
                // now that we have it tokenized to something useful.
                // we can use the vector as an array and we didn't care how much we got.
               
                if(tmpVector[0] == "stickman")
                {
                         // yada yada yada...
                }
       } // main if
       
return 0;
} // end of funciton.


the main culprit of your fault is this line...
 
char *test[sizeof(strtok((char *)p->data, " "))];


this is getting the size of the function not the data it returns so you over run the size of your array very quickly. So you have to find another method to parse your data.
char *test[sizeof(strtok((char *)p->data, " "))];
_ ¿why do you need the casting?
_ Your array is not big enough. It's of size 4 in your machine, because that is how big your pointers are.
(to clarify, you are doing char *test[ sizeof(char*) ]; //equivalent to char *test[4] in your machine )
_stickman[theStickId] make sure that it is a valid range.

If you are going to output error messages, then use std::cerr instead. The output will be flushed in every step, so you can be sure that the line was reached.
Or you could make an step by step run trough a debugger.
Topic archived. No new replies allowed.