Results 1 to 4 of 4

Thread: char* member in a struct, pointing to a dynamically created char[] array

  1. #1

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827

    Question

    Hi all.

    I've been reading 'Beginning Visual C++ 6' from Wrox, and one of the exercises (chapter 7 if you're interested) is based on this:

    Create a struct, X, with two int members and a char* member called sptr. declare 2 variables of type X, a and b. Dynamically create a string buffer and make a.sptr point to it. Assign a to b, and then output the string b.sptr points to. Change the string via a.sptr, then output it through b.sptr. What happens and why?

    Well, that's a summary of what it says. First of all I get errors when I run my code:

    Code:
    // Ex7-1
    // Struct assignment
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    struct X
    {	int x;
    	int y;
    	char* sptr;
    };
    
    void main(...)
    {	X a, b;
    	a.x = 1;
    	a.y = 2;
    	
    	if(!(a.sptr = new char[14]))
    	{	cout << endl <<"Error allocating memory" << endl;
    		exit(1);
    	}
    
    	a.sptr = "String Buffer";
    
    	cout << endl << a.sptr << endl;
    	
    	b = a;
    
    	cout << endl << a.sptr << endl;
    	cout << endl << b.sptr << endl;
    
    	a.sptr = "New String___";
    
    	cout << endl << a.sptr << endl;
    	cout << endl << b.sptr << endl;
    
    	delete [] a.sptr;
    	return;
    }
    The errors I get are "Debug assertion failed!"

    Although I get errors, the code still executes to the end, and I get this output:

    Code:
    String Buffer
    
    String Buffer
    
    String Buffer
    
    New String___
    
    String Buffer
    So I can see that the data seemingly appears different in a to b.

    Is it because two strings are being created, one in each variable? Hmm... it's confusing, the way that char* variables work. When I use 'a.sptr' as an expression, is it a string literal or is it an address? The same goes for char[] arrays. I'm confused, could someone please clear this up for me?
    Harry.

    "From one thing, know ten thousand things."

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    When you enter a string in a C++ program like that, it doesn't copy it, it merely sets the location to where in memory your program has been loaded to (plus a little bit). In order to avoid invalidating the pointer and orphaning the previous data, you need to use strcpy:

    Code:
    strcpy(dest_pointer, "My String is X");
    This way, you change the data, not the pointer.

    BTW - if you're already using STL, take a look at the string class, which is incredibly powerful, and fairly fast. It also handles reallocation properly.

    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Ahh I see... yes I remember now from Programming lectures that you can't copy strings directly in C without using a function. So it makes sense that when you copy a struct with a string in it, it won't copy the string.

    I'm still not sure, though, what exactly is happening. I can see what isn't happening. So what does this bit of code do?

    Code:
    a.sptr = new char[14];     //Line 1
    
    a.sptr = "String Buffer";  //Line 2
    
    b = a;                     //Line 3
    What I mean more specifically is, where is b.sptr pointing to now? I was intending it to point to the char[14] array in the free store. Is a.sptr pointing to the char[14] array in the free store?

    Gosh, I know I must sound like I'm a bit slow, but could you just explain the operations in those three lines? It's a little confusing for me to work out how you can establish the address of a string declard as char*; as far as I can tell the variable name, in that context, will yield the string that it points to and not the address of the string it points to.

    Hmm, I'm not sure if I've expressed that correctly, but if you can make some sense of it then I'd appreciate your help


    Oh I haven't used ATL yet, I'm only just starting with Classes
    Harry.

    "From one thing, know ten thousand things."

  4. #4

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827

    Cool

    It's okay I think, I found out the answer to my second question.

    Thanks for your help
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width