Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. I'm a total fraud!!! God am I a newbie!!! :)

I'm a total fraud!!! God am I a newbie!!! :)

Scheduled Pinned Locked Moved C / C++ / MFC
c++questioncomdesigndebugging
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LukeV
    wrote on last edited by
    #1

    Ok... don't comment on the logical side of this code, I just have a small question that needs to be answered...;P This code will give me a "user breakpoint" before exiting if I keep "for(i=6;i<8; i++)". BUT, change that to "for(i=6;i<7; i++)", which will obviously go through the loop only once, will work and exit properly. So I guess that I don't even know how to use chars anymore (thanks to MFC and CString!!!) And I need to know why it won't work and what is the proper way to achieve what I want to do. Here's the code: int main(int argc, char* argv[]) { int i = 6; char *pBody = new char; char sz[4] = "test"; strcpy( pBody, "\0" ); for(i=6;i<8; i++) { strcat( pBody, sz ); strcat( pBody, ": " ); strcat( pBody, sz ); strcat( pBody, "\n" ); } strcat( pBody, "\0" ); return 0; delete [] pBody; pBody = NULL; } Also, I guess it would be a nice thing for me to re-learn C/C++... ;) I feel like a hobbyist... :( Thanks! --------------- Ok, we suck at C/C++, but we're good at website design and MFC apps!!! http://www.edovia.com

    L T 2 Replies Last reply
    0
    • L LukeV

      Ok... don't comment on the logical side of this code, I just have a small question that needs to be answered...;P This code will give me a "user breakpoint" before exiting if I keep "for(i=6;i<8; i++)". BUT, change that to "for(i=6;i<7; i++)", which will obviously go through the loop only once, will work and exit properly. So I guess that I don't even know how to use chars anymore (thanks to MFC and CString!!!) And I need to know why it won't work and what is the proper way to achieve what I want to do. Here's the code: int main(int argc, char* argv[]) { int i = 6; char *pBody = new char; char sz[4] = "test"; strcpy( pBody, "\0" ); for(i=6;i<8; i++) { strcat( pBody, sz ); strcat( pBody, ": " ); strcat( pBody, sz ); strcat( pBody, "\n" ); } strcat( pBody, "\0" ); return 0; delete [] pBody; pBody = NULL; } Also, I guess it would be a nice thing for me to re-learn C/C++... ;) I feel like a hobbyist... :( Thanks! --------------- Ok, we suck at C/C++, but we're good at website design and MFC apps!!! http://www.edovia.com

      L Offline
      L Offline
      LukeV
      wrote on last edited by
      #2

      Ok, I guess I should use: char *pBody = new char[somevalue]; instead of: char *pBody = new char; silly me...:-O But the thing is that I don't know how many loops I'll have. Can I resize the char on the fly? Or, I could use: char *pBody = new char[nbOfFields-6]; Is that correct? Another question I end up answering myself! :cool: --------------- Nah, we're cool! http://www.edovia.com

      B 1 Reply Last reply
      0
      • L LukeV

        Ok, I guess I should use: char *pBody = new char[somevalue]; instead of: char *pBody = new char; silly me...:-O But the thing is that I don't know how many loops I'll have. Can I resize the char on the fly? Or, I could use: char *pBody = new char[nbOfFields-6]; Is that correct? Another question I end up answering myself! :cool: --------------- Nah, we're cool! http://www.edovia.com

        B Offline
        B Offline
        Bernhard
        wrote on last edited by
        #3

        if you want to make memory leaks the second thing works perfectly.. but you can't resize arrays on the heap.. so if oyu wanna resize it.. use sthing like:

        char *pBody = 0;

        pBody = new char [14];
        delete pBody;
        pBody = new char [16];

        //now you have resized pBody from 14 to 16 all the data will be lost..

        hope this helps.. (i just can recommend that you try to give std::string a try.. cause i think c-arrays suck royal.. i shot myself in the foot with this too often )


        Sometimes I think the surest sign for intelligent life elsewhere in the universe is that none of them ever tried to contact us.

        L O 2 Replies Last reply
        0
        • B Bernhard

          if you want to make memory leaks the second thing works perfectly.. but you can't resize arrays on the heap.. so if oyu wanna resize it.. use sthing like:

          char *pBody = 0;

          pBody = new char [14];
          delete pBody;
          pBody = new char [16];

          //now you have resized pBody from 14 to 16 all the data will be lost..

          hope this helps.. (i just can recommend that you try to give std::string a try.. cause i think c-arrays suck royal.. i shot myself in the foot with this too often )


          Sometimes I think the surest sign for intelligent life elsewhere in the universe is that none of them ever tried to contact us.

          L Offline
          L Offline
          LukeV
          wrote on last edited by
          #4

          I wanted to use std::string but a function I use requires a const char[] as a parameter and string.c_str is no good. Unless there's a way to convert the string to a char when I'm done... any ideas? [edit] I guess string.c_str didn't work because I thought it was a variable, not a function. string.c_str() should work. Speaking of work, I think I work too much :laugh: --------------- http://www.edovia.com

          B L 2 Replies Last reply
          0
          • L LukeV

            I wanted to use std::string but a function I use requires a const char[] as a parameter and string.c_str is no good. Unless there's a way to convert the string to a char when I'm done... any ideas? [edit] I guess string.c_str didn't work because I thought it was a variable, not a function. string.c_str() should work. Speaking of work, I think I work too much :laugh: --------------- http://www.edovia.com

            B Offline
            B Offline
            Bernhard
            wrote on last edited by
            #5

            hemm?

            std::string s;
            char an_old_array [16] = "";

            s = "hy luke";
            a_old_function (s.c_str());
            strcpy (an_old_array, s.c_str());

            do you have got any other questions ? or what do you need exactly ?


            Sometimes I think the surest sign for intelligent life elsewhere in the universe is that none of them ever tried to contact us.

            L 1 Reply Last reply
            0
            • B Bernhard

              hemm?

              std::string s;
              char an_old_array [16] = "";

              s = "hy luke";
              a_old_function (s.c_str());
              strcpy (an_old_array, s.c_str());

              do you have got any other questions ? or what do you need exactly ?


              Sometimes I think the surest sign for intelligent life elsewhere in the universe is that none of them ever tried to contact us.

              L Offline
              L Offline
              LukeV
              wrote on last edited by
              #6

              No, you answered it perfectly! It's me that was confused... I should take a nap ;) Thanks Bernard! --------------- http://www.edovia.com

              B 1 Reply Last reply
              0
              • L LukeV

                I wanted to use std::string but a function I use requires a const char[] as a parameter and string.c_str is no good. Unless there's a way to convert the string to a char when I'm done... any ideas? [edit] I guess string.c_str didn't work because I thought it was a variable, not a function. string.c_str() should work. Speaking of work, I think I work too much :laugh: --------------- http://www.edovia.com

                L Offline
                L Offline
                luckylours
                wrote on last edited by
                #7

                perhaps you can use YourConstChar=YourCstring.GetBuffer(0);

                1 Reply Last reply
                0
                • L LukeV

                  No, you answered it perfectly! It's me that was confused... I should take a nap ;) Thanks Bernard! --------------- http://www.edovia.com

                  B Offline
                  B Offline
                  Bernhard
                  wrote on last edited by
                  #8

                  no problem... bernhard


                  Sometimes I think the surest sign for intelligent life elsewhere in the universe is that none of them ever tried to contact us.

                  1 Reply Last reply
                  0
                  • L LukeV

                    Ok... don't comment on the logical side of this code, I just have a small question that needs to be answered...;P This code will give me a "user breakpoint" before exiting if I keep "for(i=6;i<8; i++)". BUT, change that to "for(i=6;i<7; i++)", which will obviously go through the loop only once, will work and exit properly. So I guess that I don't even know how to use chars anymore (thanks to MFC and CString!!!) And I need to know why it won't work and what is the proper way to achieve what I want to do. Here's the code: int main(int argc, char* argv[]) { int i = 6; char *pBody = new char; char sz[4] = "test"; strcpy( pBody, "\0" ); for(i=6;i<8; i++) { strcat( pBody, sz ); strcat( pBody, ": " ); strcat( pBody, sz ); strcat( pBody, "\n" ); } strcat( pBody, "\0" ); return 0; delete [] pBody; pBody = NULL; } Also, I guess it would be a nice thing for me to re-learn C/C++... ;) I feel like a hobbyist... :( Thanks! --------------- Ok, we suck at C/C++, but we're good at website design and MFC apps!!! http://www.edovia.com

                    T Offline
                    T Offline
                    Tim Smith
                    wrote on last edited by
                    #9

                    WHEWWWWWWW!!!!!! When I read this subject, I thought my secret had been exposed. :) Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

                    1 Reply Last reply
                    0
                    • B Bernhard

                      if you want to make memory leaks the second thing works perfectly.. but you can't resize arrays on the heap.. so if oyu wanna resize it.. use sthing like:

                      char *pBody = 0;

                      pBody = new char [14];
                      delete pBody;
                      pBody = new char [16];

                      //now you have resized pBody from 14 to 16 all the data will be lost..

                      hope this helps.. (i just can recommend that you try to give std::string a try.. cause i think c-arrays suck royal.. i shot myself in the foot with this too often )


                      Sometimes I think the surest sign for intelligent life elsewhere in the universe is that none of them ever tried to contact us.

                      O Offline
                      O Offline
                      Oz Ben Eliezer
                      wrote on last edited by
                      #10

                      Shouldn't it be delete [] pBody; instead of delete pBody; ?

                      B 1 Reply Last reply
                      0
                      • O Oz Ben Eliezer

                        Shouldn't it be delete [] pBody; instead of delete pBody; ?

                        B Offline
                        B Offline
                        Bernhard
                        wrote on last edited by
                        #11

                        of course.. sorry.. but i have to say that i hardly (say never) use c-arrays.. so.. that may be the reason why i didn't notice that.. yeah.. have a nice time.. bernhard


                        Sometimes I think the surest sign for intelligent life elsewhere in the universe is that none of them ever tried to contact us.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups