Thread: C++ (char *) problem?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    15

    C++ (char *) problem?

    I've been slowly teaching myself C++ and haven't gotten around to the String class just yet. I'm working under Vista Business (laugh if you must) and Visual Studio 2005 (again).

    When attempting to use (something a tad more familiar to me) strcpy() in VS05 I quickly was presented with the "Safer" alternative, strcpy_s() - after switching over to it I've got some concerns... no longer does my program crash after a lengthy run time, but after copying only two strings! (previously it was copying somewhere in the vacinity of 148 strings successfully.) I was wondering exactly what made it crash, so I setup a test program, which pretty much emulates what I'm trying to do:

    Code:
    #include <iostream>
    
    char * exp_str( void );
    
    int main ( int argc, char **argv )
    {
    	char * pword = new char;
    	strcpy_s( pword, 1024, exp_str( ) );
    	return 0;
    }
    
    char * exp_str( void )
    {
    	static char word[1024];
    	strcpy_s( word, 1024, "testing!" );
    	return word;
    }
    Compiles fine, but didn't work, popped out of Vista with a "Test Program.exe has stopped working..."

    Tried to trace where it was failing, so I interjected some printf() statements.. and all of a sudden it was working, without altering a single line of the other code. So I spent a little time and found my solution:

    Code:
    #include <iostream>
    
    char * exp_str( void );
    
    int main ( int argc, char **argv )
    {
    	char * pword = new char;
    	printf("");
    	strcpy_s( pword, 1024, exp_str( ) ); // Only works with the printf("")
    	return 0;
    }
    
    char * exp_str( void )
    {
    	static char word[1024];
    	strcpy_s( word, 1024, "testing!" ); // Always works.
    	return word;
    }
    Compiles & Executes without error on my computer.

    My goal was to copy the contents of one string pointer to another, as the first pointer's contents change pretty often...

    I'm not married to this method, but was hoping to write a temp-function until I can better grasp C++ strings... however my question to you guys is: Can anyone explain to me why that works?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int main ( int argc, char **argv )
    {
    	char * pword = new char;
    	printf("");
    	strcpy_s( pword, 1024, exp_str( ) ); // Only works with the printf("")
    	return 0;
    }
    This is undefined behavior. You allocate one char, but set buffer size to 1024? You're pretty much negating the use of strcpy_s. Specify proper or real buffer size. It should then assert if you try to copy too much data.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Resolved, thanks. -- Still curious though, why would the printf(""); make it work?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Teegtahn View Post
    Resolved, thanks. -- Still curious though, why would the printf(""); make it work?
    Because the program with the printf("") is a different program than one without it. Things were arranged differently in memory in a way that concealed your bug. Also, adding a function call can change the effects of an uninitialized-local-variable bug.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Just a shift in memory? Seems simple enough

    Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  2. char problem
    By ForlornOdium in forum C++ Programming
    Replies: 10
    Last Post: 10-29-2003, 02:39 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM