Thread: What doesn't this work.

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    141

    What doesn't this work.

    Code:
    #include <string>
    #include <iostream>
    
    class Particle_A {
    
    
    public:
    
            // should this be a void??
    	void AddParticle( char* Buffer, char* Particle )
    	{
    		this->m_Buffer   = Buffer;
    		this->m_Particle = Particle;
    
    		if ( this->m_Particle )
    		{
    
    			strcpy( this->m_Buffer, this->m_Particle  );
    
    		}
    
    	}
    
    private:
    	char* m_Buffer; 
    	char* m_Particle;
    
    };
    
    int main ()
    {
    	char* Buffer;
    	char* Particle;
    
    	Particle = "Test";
    
    	Particle_A p;
    
    	p.AddParticle( Buffer, Particle );
    
    	std::cout << Buffer << std:: endl;
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    1. You've got things inside the class pointing to things outside the class.
    2. Your buffer (in main) isn't pointing anywhere, so trying to strcpy to it will blow up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by Salem View Post
    1. You've got things inside the class pointing to things outside the class.
    2. Your buffer (in main) isn't pointing anywhere, so trying to strcpy to it will blow up.
    Dunno bout 1.
    Uhh do you mean the buffer doesn't have any value or it's not pointing, cause it is. ;P

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Uhh do you mean the buffer doesn't have any value or it's not pointing, cause it is. ;P
    It is pointing to some random location unknown to you...
    When you copy to that location - you probably got a crash...

    If you are not so lucky - you will overwrite some essential data of your program, and it will crash later - when it will try to use this corrupted data
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    141
    Code:
    #include <string>
    #include <iostream>
    
    class Particle_A {
    
    
    public:
    
    
    	void AddParticle( char* Buffer, char* Particle )
    	{
    		this->m_Buffer   = Buffer;
    		this->m_Particle = Particle;
    
    		if ( this->m_Particle )
    		{
    
    			strcpy( this->m_Buffer, this->m_Particle  );
    
    		}
    
    	}
    
    private:
    	char* m_Buffer; 
    	char* m_Particle;
    
    };
    
    int main ()
    {
    	char Buffer[] = "";
    	char* Particle;
    
    	Particle = "Whats up";
    
    	Particle_A p;
    
    	p.AddParticle( Buffer, Particle );
    
    	std::cout << Buffer << std:: endl;
    
    
    }
    Well I got what I wanted now, but it still is getting errors after I run the program.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Buffer has room for 0 characters (plus \0), yet you try to copy 8. Why do you think that will work?

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by tabstop View Post
    Buffer has room for 0 characters (plus \0), yet you try to copy 8. Why do you think that will work?
    Code:
    : error C2664: 'Particle_A::AddParticle' : cannot convert parameter 1 from 'char' to 'char *'
    1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    Well if I didn't have [] = "", then it gives me the above, if I add char * then it gives me the unitialized error, which then it still crashes either way, and if I try to put the NULL pointer or 0 there it still crashes.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Fist, string literals are const char: http://cpwiki.sourceforge.net/Common...kes_and_errors
    Second, pointers are not storage units - they can merely point to a storage area. Again, read link above.
    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.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by Elysia View Post
    Fist, string literals are const char: http://cpwiki.sourceforge.net/Common...kes_and_errors
    Second, pointers are not storage units - they can merely point to a storage area. Again, read link above.
    Thanks for this. I know pretty much the basics of pointers now. ;D

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    141
    Holy ........ it worked.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, and for further notes - since a class is a self-sustaining object, the class really should handle the memory management. In other words, you should not pass in a buffer yourself.
    And prefixing "this->" before accessing a class member is not necessary.
    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.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The question is why you are not using the standard C++ string object, particularly seeing that you include the header for it. Functions related to manipulating char arrays are found in the header <cstring> (c prefix meaning it is inherited from C).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM