954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Trouble with Pointers and Arrays

Hello, I am having trouble with arrays.
Basically, this is what happens:

vector <char*> constArray(10);
char* changingString = "hello";

constArray[0] = changingString;
changingString = "hi";

printf("constArray[0] = %s\nchangingString = %s", constArray[0], changingString);


Output:
constArray[0] = hi
changingString = hi

So, I think constArray[0] is changing because it points to the same location as changingString. My problem is, how do I force constArray[0] NOT to change when changingString is altered?

ilikerps
Light Poster
45 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 
vector <char*> constArray(10);
	char* changingString = "hello";

	constArray[0] = new char;
	constArray[0] = changingString;
	changingString = "yadayada";

	printf("constArray[0] = %s\nchangingString = %s\n", constArray[0], changingString);
WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

Well you got a strange compiler, mine outputs hello, hi...
the constArray[0] points to the static string "hello", if you want to point at the pointer pointing to hello you gotta do &p...
allocating a new char is just a memory leak since the pointer gets overwritten in the next operation (se above post).

perniciosus
Junior Poster in Training
78 posts since Nov 2005
Reputation Points: 29
Solved Threads: 4
 
Well you got a strange compiler, mine outputs hello, hi...

Yeah I just ran it. So does mine.allocating a new char is just a memory leak since the pointer gets overwritten in the next operation (se above post).yeah thought as much. Anyway got the desired output ( but i know it is bad code). Tried calling delete on it, but that does not also work. Why is that?

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

delete what?
(this should work, included some possible errors I could think of)

vector <char*> constArray(10);
	char* changingString = "hello";

        delete constArray[0] ; // error, not initated (I can never remember if deleting 0 is an error or not, so I usually dont) 
        // the vector initates all elements with default constructor, however 
        // I dont remember if default constructor for pointers actually does anything since they are built in types (optimization)
        // testing this results in only zero values but that is
        // likely due to using dynamic memory (which mostly is zero before use, since it get cleared when the process is handed it...
        // for stack variables crapp exists in variables after skipping initation... (enough of ranting, stopping now)...
	constArray[0] = new char;
        delete constArray[0] ; // this is ok, we just gave it a pointer to a char
        delete [] constArray[0] ; // error, did not allocate an char string, only a single char
	constArray[0] = changingString;
        delete [] constArray[0] ; // error, not dynamically allocated
	changingString = "yadayada";
allocated
perniciosus
Junior Poster in Training
78 posts since Nov 2005
Reputation Points: 29
Solved Threads: 4
 

Sorry, the example I gave must have been incorrect; I hadn't tested it. The thing that is wrong with my example is that changingString is an array rather than a character pointer. This is more accurate:

#include <vector>
#include <stdlib.h>
using namespace std;

vector <char*> msgs(100);

int main(){
	char changingString[200];
	memcpy(changingString, "hello", 6);
	int numMsgs = 0;

	msgs[numMsgs] = changingString;
	memcpy(changingString, "hi", 3);

	printf("msgs[%d] = %s\nchangingString = %s", numMsgs, msgs[numMsgs], changingString);

	return 0;
}


My output:
hi
hi

Anyone know how to change this? I tried setting msgs[0] to new char, but that didn't work...

ilikerps
Light Poster
45 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

Sorry, the example I gave must have been incorrect; I hadn't tested it. The thing that is wrong with my example is that changingString is an array rather than a character pointer. This is more accurate:

#include <vector>
#include <stdlib.h>
using namespace std;

vector <char*> msgs(100);

int main(){
	char changingString[200];
	memcpy(changingString, "hello", 6);
	int numMsgs = 0;

	msgs[numMsgs] = changingString;
	memcpy(changingString, "hi", 3);

	printf("msgs[%d] = %s\nchangingString = %s", numMsgs, msgs[numMsgs], changingString);

	return 0;
}

My output: hi hi

Anyone know how to change this? I tried setting msgs[0] to new char, but that didn't work...

if you were to do printf("%p == %p ?\n", changingString, msgs[numMsgs] ) You would find that both pointers are identical... Either create a new string and copy the value over

msgs[numMsgs] = new char[ strlen( changingString ) + 1 ]; 
strcpy( msgs[numMsgs], changingString ) ;

or use stl strings

std::vector< std::string > msgs(size) ;
...
msgs[numMsgs] = changingString ;

This will also copy the data and not just the pointer.

perniciosus
Junior Poster in Training
78 posts since Nov 2005
Reputation Points: 29
Solved Threads: 4
 

Thanks for that information, perniciosus. I will try that as soon as I can get a compiler handy :P

ilikerps
Light Poster
45 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 
msgs[numMsgs] = new char[ strlen( changingString ) + 1 ]; 
strcpy( msgs[numMsgs], changingString ) ;

or use stl strings

std::vector< std::string > msgs(size) ;
...
msgs[numMsgs] = changingString ;


Neither of these work.... The latter makes the text very weird (some weird character, a black smiley face, and an 8, rather than the text "hello") and the former has the same result as what previously was happening, where if changingString is altered, so is msgs[x].

ilikerps
Light Poster
45 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

Oh! Nevermind, I fixed my error. Perniciosus, you were correct. I had been so focused upon the "strlen(changingString) + 1" part that I forgot about the strcpy. Now it works. Sorry for the three posts in a row, but they are on three subjects and it would have been weird to make a statement and disagree with it in the same post :P.

Thanks perniciosus!

ilikerps
Light Poster
45 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You