Go to Post The robot is never 'done' there is always something that can be done to improve it. - Jonathan Norris [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 19-01-2003, 12:27
Adam Shapiro's Avatar
Adam Shapiro Adam Shapiro is offline
Registered User
FRC #0555 (Montclair Robotics)
Team Role: Alumni
 
Join Date: Jan 2002
Rookie Year: 2001
Location: Montclair, NJ
Posts: 401
Adam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud of
Send a message via AIM to Adam Shapiro
C++ Code Error?

I'm trying to use memcpy to set a vertex buffer for Direct3D but when I send the vertex array, it shuts the program. The memcpy definition asks for a const void * but I am using code that has worked on different machines with a normal void *. I have even gotten it to work before but I just added something that changes the verticies after they have been declared and now it won't work...

Sample code:
struct D3DVERTEX
{
float x,y,z,nx,ny,nz;
};
VOID *pVertex;
D3DVERTEX cvVertex[]={{1,2,3,0.0f,0.0f,0.0f,},};
cvVertex.nx=5.0;
memcpy(pVertex,cvVertex,sizeof(cvVertex));
-->CRASH!!!

I have had the same problem when sending a dynamic string to MessageBox... I think it is a problem with sending dynamic to static but other people have gotten it to work!! Any suggestions?
__________________
Mentor to Teams 555, 1929, and 2070!
Currently working in hardware design at Cisco.
Cornell University DARPA Urban Challenge - http://www.cornellracing.com
Co-Captain Team 555 - 2003,2004,2005
Trust, Love, and Magic
  #2   Spotlight this post!  
Unread 19-01-2003, 12:57
Adam Shapiro's Avatar
Adam Shapiro Adam Shapiro is offline
Registered User
FRC #0555 (Montclair Robotics)
Team Role: Alumni
 
Join Date: Jan 2002
Rookie Year: 2001
Location: Montclair, NJ
Posts: 401
Adam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud of
Send a message via AIM to Adam Shapiro
Well, I found my error... I hadn't changed the size of my vertex buffer from the previous code. I am still confused about sending dynamic text to a message box though...

Sample:
char *display;
long number=30001;
ltoa(number,display,5);
MessageBox(hWnd,display,"test",MB_OK);
-->CRASH!!
__________________
Mentor to Teams 555, 1929, and 2070!
Currently working in hardware design at Cisco.
Cornell University DARPA Urban Challenge - http://www.cornellracing.com
Co-Captain Team 555 - 2003,2004,2005
Trust, Love, and Magic

Last edited by Adam Shapiro : 19-01-2003 at 12:59.
  #3   Spotlight this post!  
Unread 19-01-2003, 13:08
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
I don't see where you get some memory that pVertex can point to. The crash is probably memcpy() trying to blast data somewhere it shouldn't because pVertex is uninitialized. This may not be the case if you're doing this outside of the code snippet that you included.

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org
  #4   Spotlight this post!  
Unread 19-01-2003, 13:25
Adam Shapiro's Avatar
Adam Shapiro Adam Shapiro is offline
Registered User
FRC #0555 (Montclair Robotics)
Team Role: Alumni
 
Join Date: Jan 2002
Rookie Year: 2001
Location: Montclair, NJ
Posts: 401
Adam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud of
Send a message via AIM to Adam Shapiro
I probably should have included the following line:
m_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0);
Direct3D allows an LPD3DVERTEXBUFFER8 variable to lock a certain amount of memory. This line goes right before the memcpy.
__________________
Mentor to Teams 555, 1929, and 2070!
Currently working in hardware design at Cisco.
Cornell University DARPA Urban Challenge - http://www.cornellracing.com
Co-Captain Team 555 - 2003,2004,2005
Trust, Love, and Magic
  #5   Spotlight this post!  
Unread 19-01-2003, 14:47
rbayer's Avatar Unsung FIRST Hero
rbayer rbayer is offline
Blood, Sweat, and Code
no team (Teamless Orphan)
 
Join Date: Mar 2002
Rookie Year: 2001
Location: Minnetonka, MN
Posts: 1,087
rbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of light
Send a message via AIM to rbayer
You still need to allocate memory, however. Just because you "lock" the memory doesn't mean that it exists.

As for your message box thing: you've got to allocate some memory for the pointer. You've got two options here:
1. change display from a char * to a char[32]. (or some other sub-script)
2. allocate some memory via new. Ie. display=new char[32]. If you do this, don't forget to delete it once you're done with it.

If you don't do this, all you've got is a pointer to, usually, 0xcdcdcdcd (or something like that), which isn't valid. When itoa tries to copy text to this location, it's going to crash because there is no memory there to copy it to.

--Rob
__________________
New C-based RoboEmu2 (code simulator) available at: http://www.robbayer.com/software.php
  #6   Spotlight this post!  
Unread 19-01-2003, 15:26
Adam Shapiro's Avatar
Adam Shapiro Adam Shapiro is offline
Registered User
FRC #0555 (Montclair Robotics)
Team Role: Alumni
 
Join Date: Jan 2002
Rookie Year: 2001
Location: Montclair, NJ
Posts: 401
Adam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud of
Send a message via AIM to Adam Shapiro
I also forgot to mention that I declared VOID *pVertices at the beginning of the function. I now have all of that working but I can't get normals to calculate correctly...
__________________
Mentor to Teams 555, 1929, and 2070!
Currently working in hardware design at Cisco.
Cornell University DARPA Urban Challenge - http://www.cornellracing.com
Co-Captain Team 555 - 2003,2004,2005
Trust, Love, and Magic
  #7   Spotlight this post!  
Unread 19-01-2003, 15:35
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Quote:
Originally posted by Adam Shapiro

Sample:
char *display;
long number=30001;
ltoa(number,display,5);
MessageBox(hWnd,display,"test",MB_OK);
-->CRASH!!
Adam,

Same problem. "char *display" allocates only enough memory to hold the pointer. "char" tells the compiler what kind of data the pointer points to (for type checking). "*" tells the compiler to allocate a 32-bit word for the pointer. "display" is a pointer to the pointer that the compiler/linker uses internally. ltoa() blindly uses the address contained in display as the destination address for the move. The problem is that display probably doesn't point to a valid chunk of memory that you can use. So at run-time, the memory managment hardware in the processor will catch this and return a trap #13 (a/k/a GP fault) to windows. This is a very common mistake in c/c++ coding (yes, I've made this mistake more than a few times ).

Replace the "char *display;" line to "char display[sizeof(long)*8+1];" and it should work.

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org
  #8   Spotlight this post!  
Unread 19-01-2003, 15:47
Zmeko Zmeko is offline
Registered User
no team
 
Join Date: Jan 2003
Location: Freehold Iowa
Posts: 15
Zmeko is an unknown quantity at this point
char *display;
long number=30001;
ltoa(number,display,5);
MessageBox(hWnd,display,"test",MB_OK);

how to fix this problem?

you can do it Kevin Watson way or you can the other way.

char *display;
long number=30001;

display = 0;
display = new char[sizeof(long)*8+1];
ltoa (number,display,10);
printf("Buffer = %s\n", display);

i think when people post a question with code, it should be required that you answer them with code.

http://www.cplusplus.com/ref/cstdlib/ltoa.html
http://www.cplusplus.com/ref/cstdlib/fcvt.html

Last edited by Zmeko : 19-01-2003 at 16:12.
  #9   Spotlight this post!  
Unread 19-01-2003, 16:20
Adam Shapiro's Avatar
Adam Shapiro Adam Shapiro is offline
Registered User
FRC #0555 (Montclair Robotics)
Team Role: Alumni
 
Join Date: Jan 2002
Rookie Year: 2001
Location: Montclair, NJ
Posts: 401
Adam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud of
Send a message via AIM to Adam Shapiro
Quote:
Originally posted by Zmeko
i think when people post a question with code, it should be required that you answer them with code.
I agree that getting replies with code is helpful but good explanations such as Kevin's/Rob's help as well. Thanks everyone for all of the help. I will be posting a lot more with questions and, once I get it to work, I will post the source/exe's for "Dasboard3D" on our team website (also under construction at http://www.mvrt.org/team555). My new problem is getting normals to calculate. I have compared my source with the sample I'm working out of and they seem to be in order but the sample works correctly and mine only calculates about 1/3 of the normals...
__________________
Mentor to Teams 555, 1929, and 2070!
Currently working in hardware design at Cisco.
Cornell University DARPA Urban Challenge - http://www.cornellracing.com
Co-Captain Team 555 - 2003,2004,2005
Trust, Love, and Magic
  #10   Spotlight this post!  
Unread 20-01-2003, 13:24
jeremy562 jeremy562 is offline
Engineer
#0562 (SPARK 562)
 
Join Date: Feb 2002
Location: Fitchburg, MA
Posts: 74
jeremy562 is an unknown quantity at this point
Send a message via AIM to jeremy562
Just a general tip for C/C++ coding: ALWAYS initialize your variables!

This includes pointers (init to NULL, or zero), integers, strings, etc.

Also, know your API, whether it's MFC, C runtime, etc. And if you can afford it, use a tool like BoundsChecker from NuMega. It's great for finding memory leaks, buffer overruns, uninitialize variables, ...
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is your most prefered programming language? Hailfire Programming 156 19-01-2005 21:42
EMERGENCY! EPROM FULL error?!? CHSguard72 Programming 2 05-03-2003 20:51
Autonomous code PBoss Programming 7 14-01-2003 15:29
Does your team use the Default code. Jeff McCune General Forum 2 09-01-2003 14:46
Error found in programming for the pump and pressure switch sjharobotics Programming 4 06-02-2002 17:46


All times are GMT -5. The time now is 14:21.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi