VC++ 6.0 -> VS Express 2005.

aCynic2

Senior member
Apr 28, 2007
710
0
0
I've decided to make a thread devoted to this, since it looks like I'm going to have a hard time adapting to VS Express's API and I'm going to have lots O' questions.

Here is the latest trouble I'm having difficulty with:

in teh calling function, the data is declared as such:

char *tokenptr[9]; // This is intended to hold the 3 coordinates of the 3 points that define a triangle.


The call:

set_points( txtline, tokenptr );

The receiver:

void set_points (char *lpszLine, char *tokenptr[9])
{
unsigned int i = 1;

// Establish the token procedure
strtok_s( lpszLine, " ", &tokenptr[0] );
while (strtok_s (NULL, " ", &tokenptr )) <<===Failure.
i++;


}


I get an exception/assertion failure on the marked line. What am I doing wrong in VS Express's eyes?
 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
First off you should be checking the return value on the first strtok_s and only if its not NULL be going into the while loop. Also, in the while loop you shouldnt be using tokenptr you need another variable intialized to null to hold the pointer infomation between calls. As it is now your passing in your output array which isnt what the last parameter is for. You should be storing the result of the call in tokenptr. Lastly, you need to check your while loop for overflows. The code as written is a buffer overflow just waiting to happen.
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
Ok, but keep in mind, this program is entirely internal and has no outward facing.

If anyone unauthorized gains access to it, they're already inside.

I also clear the files going in.

Each line is three point of three coordinates...let me see...

void set_points (char *lpszLine, char *tokenptr[9])
{
unsigned int i = 1;
char *token;

// Establish the token procedure
tokenptr[0] = strtok_s( lpszLine, " ", &token );
while ( tokenptr != NULL && i < 9)
{
tokenptr\[\i\] = strtok_s( NULL, " ", &token );
i++;
}

Is that what I need to do? NOTE: remove the slashes from the \[\i\] snippet. The forum is interpreting it as a italic tag.

I need to save the tokens in the char ptr array. They are used in the calling function.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
My recommendations:

1) Pass in the number 9 as a seperate value - That will make the function more generic
2) Use of pointers may be more efficient

Recommend changes are bolded


void set_points (char *lpszLine, char*tokenptr[], int nTokens)
{
char *ptr = tokenptr;

unsigned int i = 1;
char *token;

// Establish the token procedure
*ptr = strtok_s( lpszLine, " ", &token );

while ( *ptr != NULL && i < nTokens)
{
ptr++
*ptr = strtok_s( NULL, " ", &token );
i++
}
 

StormRider

Diamond Member
Mar 12, 2000
8,324
2
0
The problem is that you never allocated any space to hold the 3 coordinates of the triangle. All you did was declare pointers to them.
 

StormRider

Diamond Member
Mar 12, 2000
8,324
2
0
char *tokenptr[9];

The line above just declares 9 character pointers. Did you set them to point to the appropriate coordinates before making the call?
 

StormRider

Diamond Member
Mar 12, 2000
8,324
2
0
Just out of curiosity, I am assuming you are trying to extract a set of 9 numbers (that represent the 3 coordinates from 3 triangles) from a line of text?

12 23 45 6 81 9 65 32 45

Are you allowed to use the .NET Framework to help with this? Or are you limited to using ANSI C++ stuff?
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
Originally posted by: StormRider

Are you allowed to use the .NET Framework to help with this? Or are you limited to using ANSI C++ stuff?

I choose not to use it.

Don't worry about not being able to read my code. It is internally bound and was never meant to be seen. This is not an employer's or buyers request. It is a program I'm developing in support of a larger project I'm planning.

Here is a typical line from a triangle file:

-0.726059 -15.3173 -4.1 1.03711 -15.3173 -3.3 -1.03721 -15.3173 -3.3 0xFFFFFF

Think of the numbers in groups of three, each being the x, y and z coordinates that represent the vertices of a triangle. The last is the color of the surface, but this program is not concerned with rendering, only computing physical values.

The function I posted is intended to take a line as above, break it down into nine pointers to character arrays that go to an atof function for conversion (the color is disregarded). It actually reads very fast, though I do eventually want to improve some things such as using block allocates and tracking memory allocation (to prevent memory leaks).
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
Originally posted by: EagleKeeper
My recommendations:

1) Pass in the number 9 as a seperate value - That will make the function more generic
2) Use of pointers may be more efficient

Recommend changes are bolded


void set_points (char *lpszLine, char*tokenptr[], int nTokens)

Forgive my ignorance, I didn't think you could pass an undimensioned array. I think I tried it once and it wouldn't work under VC++ 6.0. I had two choices, multi-dimensional dynamic (I use that elsewhere) or define the number of pointers. For expediency, I chose to define the number of pointers.

And FWIW, I did check the end of array limit using a for loop. This is the original (unver VC++ 6.0):

// Sets each string pointer to the start of each token delimited by ch. Each ch position is set to \0
void set_points (char *lpszLine, char *lpszToken[9])
{
unsigned int i;

// Establish the token procedure
lpszToken[0] = strtok( lpszLine, " " );
for ( i = 1; i < 9; i++ )
lpszToken = strtok (NULL, " " );


}

I had to get that in for my defense. I'm trying to rewrite my stuff for VS Express and I'm not getting it right. They've changed the calling structure. Returns are now part of the parameter list and the functions return error codes instead.
 

StormRider

Diamond Member
Mar 12, 2000
8,324
2
0
Originally posted by: aCynic2
Originally posted by: StormRider

Are you allowed to use the .NET Framework to help with this? Or are you limited to using ANSI C++ stuff?

I choose not to use it.

Don't worry about not being able to read my code. It is internally bound and was never meant to be seen. This is not an employer's or buyers request. It is a program I'm developing in support of a larger project I'm planning.

Here is a typical line from a triangle file:

-0.726059 -15.3173 -4.1 1.03711 -15.3173 -3.3 -1.03721 -15.3173 -3.3 0xFFFFFF

Think of the numbers in groups of three, each being the x, y and z coordinates that represent the vertices of a triangle. The last is the color of the surface, but this program is not concerned with rendering, only computing physical values.

The function I posted is intended to take a line as above, break it down into nine pointers to character arrays that go to an atof function for conversion (the color is disregarded). It actually reads very fast, though I do eventually want to improve some things such as using block allocates and tracking memory allocation (to prevent memory leaks).

Have you considered using something like sscanf() or fscanf() to do this?
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
Question:

I've got the program running now, but it's giving me an error on exit saying:

The instruction at "0x7c910de3" referenced memory at "0xfffffff8". The memory could not be "read".

This is after main exits. It runs fine to the return statement and when it leaves my main it gets this error. It did try to load the source, but I'm not sure MS included source with the Express edition.

What can I do about this?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Take a close look at the handling of the last iteration of the loop.

More than likely, you are writing outside the memory bounds (C/C++ will allow you to do so).
That is corrupting the stack(s) and as you return from the function calls, the final return (exit from main) is not properly setup.
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
Are you certain about the stack? I do a lot of allocation off the heap, but the stack has a huge list as well: floats, ints, some structures...could I increase the stack to avoid this?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: aCynic2
Are you certain about the stack? I do a lot of allocation off the heap, but the stack has a huge list as well: floats, ints, some structures...could I increase the stack to avoid this?
The OS places return addresses and paramters in a FILO type sequence. If anything gets screwed up, this sequence will still unwind until something has become so corrupt that the OS chokes it off.

It is not a memory or stack allocation problem, it is a problem that something is going where it should not.

If you desire, E-Mail a zip file containing the source code, data file and instructions (if needed).

I will takea quick look at it on my VC 6 system and see if I can get a clue. I may not get to it until this week-end though.

 

aCynic2

Senior member
Apr 28, 2007
710
0
0
Originally posted by: Common Courtesy
Originally posted by: aCynic2
Are you certain about the stack? I do a lot of allocation off the heap, but the stack has a huge list as well: floats, ints, some structures...could I increase the stack to avoid this?
The OS places return addresses and paramters in a FILO type sequence. If anything gets screwed up, this sequence will still unwind until something has become so corrupt that the OS chokes it off.

It is not a memory or stack allocation problem, it is a problem that something is going where it should not.

If you desire, E-Mail a zip file containing the source code, data file and instructions (if needed).

I will takea quick look at it on my VC 6 system and see if I can get a clue. I may not get to it until this week-end though.

Not necessary. Via a series of #if 0/#endifs, I've got it isolated down to an array insert function. However, it may not be the last iteration that does it. It'll take some time, but I'll figure out a way to resolved this issue.

Thanks for the error interpretation. That tells me HOW to go about troubleshooting.

nothing like a ten year break to re-ignite your sense of newbiedom.

I remember about the stack, but I think I've over written the stack maybe 1 or 2 other timesin my life. It's rare that I commit this sin. Usually it's string buffer overflows.



 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
One advantage of learning how assembly works when running raw (no OS)
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
CC, you have a very insightful brain. You've discovered a shortcoming I've been meaning to take care of, but haven't.

Basically, it's this...

I'm using a dynamic array type structure that keeps track of the number of items in the list and the end of allocated memory.

In pseudo code:
typedef struct
{
<type> *list;
int num_items;
int end_of_list;
} LIST;

I defined the end_of_list (in this particular case) as model.num_surfaces / 4. Now, because the 3D object being read in was small, a triangulated cube for 12 polys, 8 vertices, the end_of_list was 3 and the span algorithm stores hits on a poly, discarding non-hits, for a potential four per poly, that meant up to eight items in the list (the original test model has 1366 polys).

I've been meaning to write out a better array subsystem (among other things), but I've been so focused on getting the span algorithm done, I've ignored it. Now, it came back to bite me.

When !(num_items < end_of_list) I should double space, or some other amount that will give me elbow room. But, again, I haven't taken the time.

Thanks for your help, sincerely.

And bbosel and Eagle, thank you as well. I didn't mean to ignore you, but I'm P&Ming over VS Express's changing things. Since this won't render, I wnat something I can run anywhere, including on *nix. I'm thainking of devising some form of compile type typing..