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
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
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...
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