help on debugging

This is a discussion on help on debugging within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by matsp In theory, that would work. I'd probably use char oo[13] instead [13 is long enough for ...

  1. #16
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by matsp View Post
    In theory, that would work. I'd probably use char oo[13] instead [13 is long enough for a 32-bit integer].

    However, you return 'o', which is a pointer to the oo variable (yes, itoa() returns the string it got as buffer, which is oo), so you will still return a pointer to a local variable, which is bad. You either should pass a character array to the function, or call malloc() to get a permanent piece of memory (just make sure you free it later).

    --
    Mats
    tanx dear Mats, but when i did
    Code:
                  char  oo[13];
                            char * o;
                            o = (char*) malloc (13);
                            if (o==NULL) exit (1);
    
                            o = itoa(temp,oo,8);    //itoa(number,a buffer to hold the string,the base you want the number to be saved in buffer)
                            offsetptr = o;//itoa(temp,ptr,8);    //itoa(number,a buffer to hold the string,the base you want the number to be saved in buffer)
                            free (o);
    it didnt work. it just even corrupts the output! i used to get before changing this !
    Quote Originally Posted by rags_to_riches View Post
    You know, this code is full of char * (C-style strings) usage. Sorry, but if you don't know how to use them, you're destined to fail. This looks like a complicated project to undertake if you don't understand such a rudimentary part of C & C++ programming. If you're used to using the C++ string class and its relatives, then I would use those instead.
    yeah , that why i asked for your help!
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Why are you refusing to use std::string and standard containers? Read Stroustrup's answer to the FAQ: How do I deal with memory leaks?

    Oh, and what's with o and oo? Read Stroustrup's answer to the FAQ: How do you name variables?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by laserlight View Post
    Why are you refusing to use std::string and standard containers? Read Stroustrup's answer to the FAQ: How do I deal with memory leaks?

    Oh, and what's with o and oo? Read Stroustrup's answer to the FAQ: How do you name variables?
    tanx, i am using standard container!! and im using strings! too, i cant use std::string because of the conversions i have to deal with . atoi() only accepts C style strings , or and also itoa() . thats my worst luck ever!

    and about o and oo, i used them temporarily just to make that itoa() function to work, i was just testing , and just it happend and it worked, after that , i didnt change them !! ok , ill change their name,

    and after all , do i get these errors because i do have a memory leak or sth in my program !! ?
    by the way , when i used
    Code:
    cstr = new char [str.size()+1]; 
      strcpy (cstr, str.c_str());
    in my program , ( more than 20 times)
    i didnt write "delete[] cstr;" after using them , is it importatn to do so ? i mean could it be the cause to crash ? !!?

    and why do i get that error in malloc? what did i do wrong?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. Is there a reason to use itoa and atoi? If you're using strings, why not use stringstreams?

    2. If you're not seeing the warnings, why not use -Wall -pedantic flags when you compile?

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by Masterx
    tanx, i am using standard container!! and im using strings! too, i cant use std::string because of the conversions i have to deal with . atoi() only accepts C style strings , or and also itoa() . thats my worst luck ever!
    Look at it from another perspective: you can't use atoi() because it only accepts C style strings (but then you can get an unmodifiable C style string from a std::string, so that is not entirely true), and you can't use itoa() because it produces a C style string (aside from the fact that it is non-standard).

    I believe we have mentioned this before, but the solution is to use a stringstream, or if you are willing to use a non-standard component, then you can use boost::lexical_cast.

    Quote Originally Posted by Masterx
    and after all , do i get these errors because i do have a memory leak or sth in my program !! ?
    They are probably more likely due to an attempt to access memory that you do not own.

    Quote Originally Posted by Masterx
    i didnt write "delete[] cstr;" after using them , is it importatn to do so ? i mean could it be the cause to crash ? !!?
    It is good practice to do so, but it is even better practice to use a class that manages memory for you.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    im going to use string stream instead of itoa(). wish me luck .
    .please sb tell me if im wrong .


    Code:
    #include <iostream>
    #include <sstream>
    
    int main() {
      int number = 123;
    
      std::stringstream ss;
      ss << number;
    
      std::cout << ss.str() << endl;
    }
    we have an integer number. and we want to convert it to a string ! so we use ss<<number; to copy the number value to ss , . now whats the ss.str()? can i simply write
    std:ffset = std::ss now?
    will it work?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    Quote Originally Posted by Masterx View Post
    tanx, i am using standard container!! and im using strings! too, i cant use std::string because of the conversions i have to deal with . atoi() only accepts C style strings , or and also itoa() . thats my worst luck ever!
    atoi DOES work with std::string:
    But it accepts C-style strings, so use the c_str() member function to get a C-style string from a std::string object.
    itoa is non-standard, so it should not be used anyway.
    But better yet, get rid of both atoi and itoa and use boost::lexical_cast. It will serve the purpose of both - and more!

    and after all , do i get these errors because i do have a memory leak or sth in my program !! ?
    by the way , when i used
    Code:
    cstr = new char [str.size()+1]; 
      strcpy (cstr, str.c_str());
    in my program , ( more than 20 times)
    i didnt write "delete[] cstr;" after using them , is it importatn to do so ? i mean could it be the cause to crash ? !!?
    Of course you must! Otherwise there will be a memory leak!
    But you should not do it in the first place (using dynamic memory with new).

    and why do i get that error in malloc? what did i do wrong?
    malloc should not be used in C++, either. If you need to use dynamic memory, use std::vector. In rare cases where it will not do, use new.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by Elysia View Post
    atoi DOES work with std::string:
    But it accepts C-style strings, so use the c_str() member function to get a C-style string from a std::string object.
    itoa is non-standard, so it should not be used anyway.
    But better yet, get rid of both atoi and itoa and use boost::lexical_cast. It will serve the purpose of both - and more!


    Of course you must! Otherwise there will be a memory leak!
    But you should not do it in the first place (using dynamic memory with new).


    malloc should not be used in C++, either. If you need to use dynamic memory, use std::vector. In rare cases where it will not do, use new.
    first of all tanx . but second .im really dieing !!!im owerhelmed ! i learn one thing , another just pops up!
    well, when trying to use string stream , i encountered a problem !
    Code:
    |80|error: ambiguous overload for 'operator=' in 'offsetptr = ss'|
    edited : i used offsetptr = ss.str(); and voila it works ! i successfully took a step ahead .! thank GOD. and thank you guys too, !
    when i tried to std:ffsetptr = ss;!
    any reason!

    and third : boost::lexical_cast, how should i know if i have it ! if i dont where can i get it ! ?

    and at last ,im trying to remove any C style string that i used, let me see if i can do such a thing at my current stage !!
    Last edited by Masterx; 01-28-2009 at 01:21 PM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You use >> to get something out of a stringstream.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    Boost.org
    whatever_type_I_want var = boost::lexical_cast<whatever_type_I_want>(source);
    Just remember that it throws.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by tabstop View Post
    You use >> to get something out of a stringstream.
    tanx .dear tabstop. and Oh , GOD i cant believe it , it just wiped out one of the most recent annoying errors i used to get!! i mean this one( #8) is solved now .
    im going to change all of the C based stuff now. to see if the errors will be gone !
    Last edited by Masterx; 01-28-2009 at 01:42 PM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
                  char  oo[13];
                            char * o;
                            o = (char*) malloc (13);
                            if (o==NULL) exit (1);
    
                            o = itoa(temp,oo,8);    //itoa(number,a buffer to hold the string,the base you want the number to be saved in buffer)
                            offsetptr = o;//itoa(temp,ptr,8);    //itoa(number,a buffer to hold the string,the base you want the number to be saved in buffer)
                            free (o);
    There are so many things wrong with this code that I hardly know where to start.
    First of all, itoa() returns a pointer to the same address as the input buffer (oo). So, you must not malloc() o, and then assign it with the return from itoa() - it's the same as assigning o = oo after malloc. Then you set offsetptr to o, followed by free(o) - so what memory [assuming the other stuff wasn't all wrong] is o pointing to? Who owns it?

    Yes, you must free memory allocated by malloc, but NOT UNTIL YOU HAVE FINISHED USING IT!

    Now, it looks like you are planning to use C++ style strings, so it's a moot point anyways, but you it really seems like you need to learn a bit more about the basics of memory and how you deal with it before you start on a complex programming project.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #28
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    tanx dear mats, in this case i would appreciate a proper and yet sample demosntrating how to use malloc() just for learning purposes, im not gona use it anymore.

    and the rest :
    ive disabled all of the C string variables so far , and used their equivalent C++ counter parts ,(str::string) .
    i even managed to replace the atoi() with the new string stream based Atoi() function i modified :
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <sstream>
    using namespace std;
    
    int Atoi(string input)
    {
            int temp;
        	stringstream ss( input);
        	ss>>temp;
        	return temp;
    }
    it was primarily accepting a char * as an argumnet , but i changed it into string ! i dont know whether this makes an error or not !, so please tell me about this too.

    and now, im having a slight big problem using stringstream in converting octal to decimal. the problem is i dont know , how im supposed to make the string stream understand that , it should treat the number a octal not decimal as default!
    well conversion from decimal to octal is fine , because it treats the number as decimal at first ( as the number itself is decimal indeed!)
    but when i have an octal number, how can i do this ? i mean simply doing :
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <sstream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        
        int num = 12;
    
    
        std::stringstream stream;
        stream <<dec<<num;
        std::cout << stream.str();
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    wont give me a correct answer though!
    but this program is just fine with stream<<oct<<num, meaning the conversion from decimal to octal.! so im stuck in it !

    and by the way , is not a function for converting octal to binary just like the above ! ?
    im asking these, because im revising my whole project ! so i do change the functions , and im trying to use standard stuff at first.

    ( i cant download boost library, im using Dialup now, so , please help me with these stuff. tanx .
    -------------------------
    logs:
    replaced itoa() with a better equivalent by string stream
    replace atoi() with a stringstream based function . (Atoi(). i)
    replaced all of C style string variables with their std::string counterparts.
    DectoOctal() is removed and instead the new function :
    Code:
    string DectoOct(int dec)
    {
        std::stringstream stream;
        stream <<std::oct<<dec;
        return stream.str();
    
    }
    is used.
    meanwhile im seeking a better function for Octal to bin
    the current function i have for cotal to binary conversion is :
    Code:
    string OcttoBin(int n)
    {
    
    
             int a[6],i=0,t=0,num;
             string str,str1,str2,message;
    
             char buffer[9];
    
             string str_array[9];
             const size_t str_array_size = sizeof(str_array) / sizeof(str_array[0]);
    
             while(n!=0)
             {
                     a[i]=n%10;
                     n=n/10;
                     if(a[i]>7)
                     t=1;
                     i++;
             }
             i--;
             if(t==0)
              for(;i>=0;i--)
              {
                      switch(a[i])
                       {
                               case 0:
                               if (!str.empty())
                               str.append("000");
                               else
                               str="000";
                               break;
    
                               case 1:
                               if (!str.empty())
                               str.append("001");
                               else
                               str="001";
                               break;
    
                               case 2:
                               if (!str.empty())
                               str.append("010");
                               else
                               str="010";
                               break;
    
                               case 3:
                               if (!str.empty())
                               str.append("011");
                               else
                               str="011";
                               break;
    
                               case 4:
                               if (!str.empty())
                               str.append("100");
                               else
                               str="100";
                               break;
    
                               case 5:
                               if (!str.empty())
                               str.append("101");
                               else
                               str="101";
                               break;
    
                               case 6:
                               if (!str.empty())
                               str.append("110");
                               else
                               str="110";
                               break;
    
                               case 7:
                               if (!str.empty())
                               str.append("111");
                               else
                               str="111";
                               break;
                       }
              }
              str1="000000";
              str2="000";
                  if (str.length()==3)
                  {
                            str1.append(str);
                            str=str1;
                  }
    
                  if (str.length()==6)
                  {
                            str2.append(str);
                            str=str2;
                  }
    
    
    
             if(t==1)
              {
    
                      message="no";
                    // cout<<"Not a Octal number\n";
                     return s;
              }
    
    return str;
    }
    and the last one is to find a way to get stringstream to work properly as i said above.
    Last edited by Masterx; 01-29-2009 at 04:23 AM.

  14. #29
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    your problem not in the stringstream

    int num = 12;

    12 is decimal constant - so you already have a decimal 12 in your var when starting using the stringstream and it is late to recognize 12 as oct or hex

    change that line to
    int num = 0x12;
    for example to represent 12 as hex number
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #30
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by vart View Post
    your problem not in the stringstream

    int num = 12;

    12 is decimal constant - so you already have a decimal 12 in your var when starting using the stringstream and it is late to recognize 12 as oct or hex

    change that line to
    int num = 0x12;
    for example to represent 12 as hex number
    well , tanx, but how am i supposed to do such a thing , when i have a variable that gets its value form some other calculations! and its not just that simple!
    e.g suppose " num " has an octal value inside it(e.g 12 is octal here and it = 10 decimal!)! , so in this case , im asking for help!because i cant just add O before the number to imply its octal! so how to convert it now ! how to let the function understand its dealing with an octal integer!?

    and any suggestion about octal to binary conversion?
    Last edited by Masterx; 01-29-2009 at 05:30 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 10:48 AM
  2. Problem in debugging in Eclipse
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 08-21-2007, 10:53 AM
  3. Debugging Dev C++
    By tuurb046 in forum Tech Board
    Replies: 10
    Last Post: 08-16-2007, 01:51 PM
  4. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 08:35 PM
  5. debugging directx apps
    By confuted in forum C++ Programming
    Replies: 1
    Last Post: 08-16-2003, 09:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21