Click here to Skip to main content
14,773,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this may sound like a stupid question but I'm not entirely sure as to how to ask it properly and this is my first time asking a programming related question (normally I can find what I'm looking for by search but hey).
So I'm using allegro and I've got quite a few lines of code with a lot of trig math in and I need to debug it so I'm trying to send the mid calculation int(floar)s (properly converted to strings don't worry). my problem is that I always run into issues trying to output variable values with allegro because the textout function requires a 'const char *' type and I don't really know how to use it. I'm pretty sure its a pointer to a /0 terminating character array (whatever that is, I'm assuming something like [0]='H',[1]="i",[2]="!",[3]="/0") and is a constant so I can't change it after it's declared (which is annoying, for this situation).
my main question being: how do I use this type properly without causing crashes?
and perhaps I've done something else wrong.. if so, please let me know.
this subroutine sometimes works and sometimes crashes before outputing anything to the screen but I'm pretty sure I can only use it once. Do I have to delete the const char * before I use it again? I'm not really sure what to do, I've tried deleting it but I'm pretty sure I must've used the wrong syntax or something. I just need some teaching on this, I run into this as a limitation all the time using allegro, I may have done something right before but I can't remember.. If I get a good answer I'll definitely write it down somewhere safe :)

#include <allegro.h>
#include <string>

void debugOut(std::string outstr)
{
     const char * c = outstr.c_str();
     clear_to_color(screen, 0x32324B);
     textout_centre_ex(screen, font, c, resolution.x/2, resolution.y/2, 0xFFFFFF, 0x333333);
     while(!key[KEY_ENTER]){}
     rest(500);
}
Posted
Comments
Fredrik Bornander 5-Nov-13 7:43am
   
How is it crashing? What error message are you getting?
James kingswell 5-Nov-13 9:14am
   
there is no error message the program simply closes once I call this function, sometimes it runs through the first time, sometimes it crashes strait away.. :/
I could give examples of the strings I would pass to it if that helps but I doubt that has anything to do with the actual issue.
James kingswell 5-Nov-13 9:15am
   
I'm assuming based on the low predictability it has something to do with memory
Fredrik Bornander 5-Nov-13 9:18am
   
I mean run it in the debugger at see what the error is.
Is is an access violation?
Is it trying to read a null pointer or a bad pointer?
What does the debugger say when then application crashes?
James kingswell 5-Nov-13 14:07pm
   
well here's the weird thing.. using the debugger stops the crashes.. :/ I don't know why but it does.. it's also stopped some other issues I've had with fstream.. thanks.. I guess.. I've never used the debugger before because I use my own tools in each program.. cheers though.
James kingswell 5-Nov-13 7:45am
   
So I looked up null-terminating strings and am a bit more clued in now and understand why I had to use c_str() but that doesn't solve my problem. :c

Yes, you are right - a it's a pointer to a null terminated array of characters.
The const part just means that you can't modify it via the pointer - not that it can't be modified. You can either pass a genuine cost value:
const char * c = "hi!";
Or you can pass it a non-const array:
char data[6];
const char * c;
strcpy(data, "hi!");
c = data;
printf(c);

and either will work. The important thing is that once it is assigned to the const pointer, you can't change it via that pointer - so you can safely pass a genuinely constant string and it'll work fine.
   
Comments
James kingswell 5-Nov-13 9:26am
   
thanks, you truly are a very helpful person.. I'm just curious as an off topic question, what draws people like you to even answer people in such places? Do you enjoy teaching or 'solving puzzles'? just curious, it seems everyone I know in real life would just have a lot of other things they would consider higher priority and never bother helping people out on line. :/
OriginalGriff 5-Nov-13 11:02am
   
Difficult question really.
I enjoy solving puzzles, but (to be honest) most of the questions I answer aren't that much of a puzzle! :laugh:
It helps me as well as others - because nothing improves your understanding of a subject than trying to explain it to someone else - and it also takes my mind off what I am trying to do, which improves my productivity by de-focussing me from a problem.

But mostly I've been in this game for a long time, and it is frustrating to see people being taught as badly as most question setters appear to be. And it's a "nice feeling" to see a metaphorical light bulb light up over someones head! :laugh:
James kingswell 5-Nov-13 14:53pm
   
nice to know there are people like you around :) revives my faith in the human race. Keep up the good work sir, I salute you! :D
When you see a const char* somewhere, it means that the code wants to see a char string, and will not be modifying it. In the code example you give here, the memory management for the string is handled in the std::string. Your code gets a const char* look at the std::string and uses it accordingly. Your code should just drop value 'c' at scope exit (as you have done here). For this code segment, do not attempt to delete 'c' when you are done with it; the memory is owned by 'outstr', not by 'c'.
   
Comments
James kingswell 5-Nov-13 9:21am
   
Ok :) thanks for the clarification.. perhaps this is not the subroutine causing the crash after all.. :/ I'd just assumed it was due to my lack of understanding.. :/ I'll have a browse. Note that your time was not wasted if this is the case, I know now what this type does and can use it properly. knowing this isn't causing an issue is a step forward :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900