itoa() question - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

itoa() question

Allegro_DevAllegro_Dev Posts: 23Member
[b][red]This message was edited by Allegro_Dev at 2006-12-16 17:32:57[/red][/b][hr]
Ok I am making a game and i want to display the score [italic]font[/italic] by:

Create function to convert:
Score - integer
str - char*

This is what i got:
[code]
void post_score(BITMAP* SCR, int X, int Y){
int x = 0;
char* str;
// string G; <---- THE PROBLEM! if i were to acctually use this
// line the program would crash...
// Segmentation Fault

itoa(score[0], str, 10); // if i were to comment this line and
// uncomment string G; the program would
// not crash.. tho i need itoa();

// string G; <--- THE PROBLEM! if i were to put it here
// same problem: Segfault

G = str;
int i = 0;
while(G[i] != ''){
Q = G[i]; // Needed string Q; because G[i].c_str() would
// cause Compile-time error [c_str() not defined]
// that is another question i would like to ask!

draw_sprite(SCR,bScore[atoi(Q.c_str())],X+x,Y);
// SCR is the output BMP of the sprite
// bScore[10] is a bitmap array 0 stores the image of the
// number 0... 1 stores the image of 1.. etc...

x += bScore[atoi(Q.c_str())]->w; // find width of the number and
// move cursor that many pixels
i++;
}
}
[/code]

Ok read those comments in the code.. after trial and error
trying to declare string G; /* didn't even used to be called G
but i tried everything including name change (in case predefined) */
The program would throw a segmentation fault and crash
any combination of string G; and itoa() in the SAME SCOPE
AH HA!!! that's IT... the solution was make string G; GLOBAL
then it worked... until string Q; .. then i made that global
and now everything works...


why can't i declare strings in the same scope as using itoa() function???
Why does that matter if it was declared in the same scope as itoa is used???
someone please explain this science to me!!

Thank you.. if your confused im sorry..


Comments

  • MT2002MT2002 Posts: 1,444Member
    [b][red]This message was edited by MT2002 at 2006-12-16 17:58:28[/red][/b][hr]
    [blue]
    Please use code tags when psting code...[/blue][code]
    [red]char* str; //w; // find width of the number and
    // move cursor that many pixels
    i++;
    }
    }
    [/code][blue]

    Arrays are pointers in desquise. [b]str[/b] in your code is equivanelt
    to [b]&str[0][/b], so this code should be fine.

    [b]G[/b] is declared in a different scope, trying to access
    some random memory location from within a different local data
    segment (The uninitialized local ptr) -- which was causing the
    segmentation fault. (Im referring to the [b]G = str;[/b] line.)

    If a ptr (for example, [b]str[/b]) is not initialized, it
    will have whatever garbage within that memory location. So,
    when we derefrence the ptr, we would access some random location
    in memory. This is why we either need to allocate via the heap, or
    array.

    Hope this helps;

    ~mt2002[/blue]


  • CytCyt Posts: 557Member
    First of all, I don't understand why you need strings in the first place. You seem to convert a number to a string and then convert the string back to a number. Why not just stick to numbers.

    Secondly, the reason why it crashed is purely because of your use of itoa. The string (str) that you pass has not been allocated. You should alocate it on the stack.

    E.g.

    [code]
    char str[11];
    [/code]

    Doing so should fix all your problems and you should no longer see crashes.

    As for the c_str:
    c_str is a function in the string class which allows you to retrieve the string as a const char. Wherever possible, this is done automatically (through an overload of the (const char*) operator - don't ask...), but it is good practice to always call the function yourself when you want to access your string class as a string - this way you are in control and the readers of your program ought to be less confused about what's going on.

  • Allegro_DevAllegro_Dev Posts: 23Member
    [b][red]This message was edited by Allegro_Dev at 2006-12-16 18:10:58[/red][/b][hr]
    thank you for your explanations that seems more logical

    and i am converting back and forth because i cant think of an easier way right now...
    i did before using just numbers but it almost seemed harder than this way..

    and i dont know about declaring char str[..]; because (correct me if im wrong) it is less dynamic... in my mind ... the way i learned it declareing this way: char* str; let me make a string anysize (up to sizeof(char); ) or am i wasting memory?...

    and lastly... this was the first time i have used itoa(); and atoi(); and i chose this way mostly for learning exp. and that i did thank you everyone.




  • MT2002MT2002 Posts: 1,444Member
    : [b][red]This message was edited by Allegro_Dev at 2006-12-16 18:10:58[/red][/b][hr]
    : thank you for your explanations that seems more logical
    :
    : and i am converting back and forth because i cant think of an easier way right now...
    : i did before using just numbers but it almost seemed harder than this way..
    :
    : and i dont know about declaring char str[..]; because (correct me if im wrong) it is less dynamic... in my mind ... the way i learned it declareing this way: char* str; let me make a string anysize (up to sizeof(char); ) or am i wasting memory?...
    :
    : and lastly... this was the first time i have used itoa(); and atoi(); and i chose this way mostly for learning exp. and that i did thank you everyone.
    :
    [blue]
    If you want to use char* (for dynamic memory),
    you need to know the number of chars in the string.

    That is...[/blue][code]
    const unsigned int strlen = 256;

    char* str = new char [strlen];
    if (!str)
    // Error

    // use str here...

    delete [] str;[/code][blue]

    strlen is the number of shars in the string (256 for this example).
    ANSI strings are 1 byte per char, so their is no need for
    sizeof (char).

    Unless you are planning on localization with other languages
    and envirements, the above should be what you want.

    I highley recommend looking into std::string here though...

    ~mt2002[/blue]

  • DonotaloDonotalo Posts: 715Member
    : [code]
    : [red]const unsigned int strlen = 256;[/red]
    : [/code]
    [purple]
    a slight mistake: [red]strlen[/red] is a standard C string function, so if the header file containing declaration of [red]strlen()[/red] is included somehow, [red]strlen[/red] cannot be used as an identifier name.
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • LundinLundin Posts: 3,711Member
    : [b][red]This message was edited by Allegro_Dev at 2006-12-16 18:10:58[/red][/b][hr]
    : thank you for your explanations that seems more logical
    :
    : and i am converting back and forth because i cant think of an easier way right now...
    : i did before using just numbers but it almost seemed harder than this way..
    :
    : and i dont know about declaring char str[..]; because (correct me if im wrong) it is less dynamic... in my mind ... the way i learned it declareing this way: char* str; let me make a string anysize (up to sizeof(char); ) or am i wasting memory?...
    :
    : and lastly... this was the first time i have used itoa(); and atoi(); and i chose this way mostly for learning exp. and that i did thank you everyone.
    :
    :


    (assuming that you are programming on a PC)

    Static allocation takes up more stack space, yes. But it is also a whole lot faster than dynamic allocation. The amount of static data you are allowed to allocate for your program is very much platform-specific though. Therefore it is good to use dynamic allocation when dealing with large amounts of data (like files for example).

    If you are using C++ strings you don't need to bother about dynamic allocation since it will take care of it for you. It is good practice to know how to do it, though.

    You should also be aware of the fact that itoa() is not standard ANSI C/C++ while atoi() is.
Sign In or Register to comment.