Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!

I would like to start by saying thanks to everyone who takes some time to view this thread and try to help.


I have a global static variable of type
static wchar_t d_sondi[50]
, and a global static vector defined as

static vector<wchar_t*> vDubinaSonde;
. In my GUI, a button was added that should fill vector with text from edit control.

By checking the size of the vector, I have determined that fill works fine, but output of strings is not good.

When I output vectors content, it outputs last entry, instead of all elements.

Text is retreived by using GetDlgItemText();

Here is the handler for button control:

C#
case IDC_BUTTON9:
                {
                    if( tip_gte == L"'Литогеотермална енергија'")
                    {
                        memset( &n_sondi, '\0', sizeof(n_sondi) );

                        GetDlgItemText( hwnd, IDC_EDIT15, n_sondi, 50 );

                        if( !wcslen(n_sondi) )
                            wsprintf( n_sondi, L"%s", L"0" );

                        memset( &d_sondi, '\0', sizeof(d_sondi) );

                        GetDlgItemText( hwnd, IDC_EDIT16, d_sondi, 50 );

                        if( !wcslen(d_sondi) )
                            wsprintf( d_sondi, L"%s", L"0" );

                        vBrojSonde.push_back(n_sondi);

                        vDubinaSonde.push_back(d_sondi);

                        for( vector<wchar_t*>::size_type in = 0;
                            in < vDubinaSonde.size(); in ++ )
                            MessageBox( hwnd, vDubinaSonde[in], L"", MB_OK );

                    }
                }

                break;


I have tried this code in console, and it works the way it should :

C++
#include <iostream>
#include <vector>

using std::vector;
using std::endl;
using std::wcout;

int main()
{
	vector<wchar_t*> v;

	wchar_t test[4] = L"123";

	v.push_back(test);
	v.push_back(test);
	v.push_back(test);

	for( vector<wchar_t*>::size_type i = 0; i < v.size(); i++ )
		wcout << v[i] << endl;

	return 0;
}


So why does it display only the last value in the vector, in the above code for button handler?

I work in MS Visual Studio Express 2008, on Windows XP, in C++, using pure WIN32 API.

If any other information is required ( source code or something similar ), please ask for it, I will more than gladly supply it.
Posted
Updated 23-Jun-13 18:10pm
v6

I think the code is doing what it should do.

You are pushing a global variable d_sondi to vector again and again. So basically your vector will just contain multiple copies of the same variable(d_sondi). And since you are updating the global variable everytime you click the button, vector is reflecting the last value.

I hope it helps.
 
Share this answer
 
Comments
MyOldAccount 24-Jun-13 0:27am    
How can I change code to update the vector properly ?

When I push a button, in that variable should be stored the content of the edit control, and that content should be added to the vector.

I thought to do that by filling global variable with GetDlgItemText(), and then just to add that variable's text into vector.

Can you please help me with some advice on how to change code to achieve that ?

This is really important to me.

Thank you, and thank you for the reply.
enhzflep 24-Jun-13 0:34am    
You're storing a vector of *pointers to a string* So, since you use the same variable each time d_sondi, you push the same address each time. You could get around this by creating a new string each iteration, before pushing it into the vector.

I.e
const int bufSize = 51;
d_sondi = new wchar_t[bufSize];
GetDlgItemText( hwnd, IDC_EDIT16, d_sondi, 50 );
...
...
vDubinaSonde.push_back(d_sondi);

So, while you're still using the same variable to hold the address of the string - d_sondi in this case, you're putting a new value into this var each time. The result - you copy the address of each new string each iteration of the loop. :)
Each element points to a new string, not just the same one repeatedly.
To free the memory consumed by these strings, you call delete on each pointer in the vector, before removing all the elements from the vector. (The OS will free this mem automatically when you close the program, if you haven't done so already)
MyOldAccount 24-Jun-13 0:46am    
That worked!

So simple and effective!

Thank you so SO much!

My 5!
OK, my CPP is a little rusty, but here's some ideas to get you going...

First, your string should be (I think):
C++
wchar_t* test = L"123";


The problem you are having though is because std::vector does not support arrays as the inner type. You can create your own type in a structure for fixed size strings and use it that way, but because of the way arrays are handled this isn't supported.
 
Share this answer
 
Comments
MyOldAccount 24-Jun-13 0:00am    
But when I test things in console, it works fine...
See the update of the question.
Ron Beyer 24-Jun-13 0:09am    
I'm sorry but you changed this question 3 times while I was looking up information to support my answer, and this question is very different than the second version I was looking at.
MyOldAccount 24-Jun-13 0:24am    
Yeah.. sorry, I wasn't trying to be rude...
I am just desperate...
If you can help, thank you, if not, no hard feelings, its ok.
Use wstring instead of wchar_t array.
So you would need a wstring and a vector<wstring></wstring>.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900