473,831 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use vector <const char*>

151 New Member
Hi, i am coding one program by using string class but i want to replace it with const char* in order to enhance the performance.

Sample code:
vector <string> vec;
char str[10] = {"Sunday","Mond ay"............ ............}
for (int i = 0; i < 10; i++)
vec.push_back(s tr[i]);

If i use const char*, there is always memory leak.
vector <const char* > vec;
char str[10] = {"Sunday","Mond ay"............ ............}
for (int i = 0; i < 10; i++)
vec.push_back(s tr[i]);

Please help, how can i avoid such pblm .

Regards
May 17 '10 #1
5 15178
potatosoftware
3 New Member
The memory leak is because of the conversion of a non const char* to a const char*
in the following instruction :

vec.push_back(s tr[i]);

Either use the same type, or use the following to cast between the two types :

const_cast
=>

vec.push_back( const_cast( str[i] ) );

Hope this helps.
May 17 '10 #2
Banfa
9,065 Recognized Expert Moderator Expert
How do you detect the memory leak Man4ish?
What is it that you observe when the program runs?


BTW implicitly cast char * to const char * is in no way every going to cause a memory leak.
May 17 '10 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
vector is implemented as an array.

vector (as with all standard containers) is optimized for speed.

I seriously doubt your array code will be faster than vector.

Plus to manage your array you will need to write most of the code already in vector.
May 17 '10 #4
Man4ish
151 New Member
If i use the vector with const char* still the memory leak is there. Which means same value is going to be returned again. Let say i push_back thousand lines from one file in vector but when i roll it to get the vector values, It return only first line 1000 times.
May 18 '10 #5
Banfa
9,065 Recognized Expert Moderator Expert
Actually this

char str[10] = {"Sunday","Mond ay"............ ............}

doesn't compile even with the implied extra days of the week and a semi-colon added.

Perhaps you should post the actual code that is causing your problem.

Having every entry in the vector return the same value is indicative of having a single buffer and change the value of the buffer rather than having multiple buffers.
May 18 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
4892
by: Joseph Turian | last post by:
Some function requires a vector<const foo*> argument. How can I cast a vector<foo*> to vector<const foo*>? Thanks! Joseph
9
6833
by: sshock | last post by:
Hi all, I want to read from a file into a vector<unsigned char>. Right now my code looks like this: FILE* f = fopen( "datafile", "rb" ); enum { SIZE = 100 }; vector<unsigned char> buf(SIZE); fread(&buf, 1, SIZE, f);
4
20145
by: Bobrick | last post by:
Hi. I'm in the process of making a GUI for a function someone else wrote, and i've come across a type i'm unfamiliar with, namely "std::vector<unsigned char>". I need to get the contents of this variable into a form I can display in a text box, but i'm not sure what to expect inside of the variable, whether I can just treat it like an array e.t.c. Any help would be appreciated. Thanks,
6
5659
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. char temp; std::vector<unsigned charmmessage; while (!done){
4
2795
by: hn.ft.pris | last post by:
Hi: I've got the following simple code to test vector<const T>, #include <vector> #include <iostream> using namespace std; int main( void ){ vector<const intvec;
3
40716
by: timor.super | last post by:
Hi group, how to convert a string to a vector of unsigned char ? I used to iterate trough the string to set the vector, but I think this is not the best way to do this. I'm a beginner with the STL How about allocating the vector ? should I know before the size ? And I would like to do then opposite conversion, from an unsigned char
1
4581
by: Javier | last post by:
Hello, thanks for the replies to my questions. I have one more: class A { public: m1() const; m2(); };
9
2246
by: t | last post by:
Can you use a plain iterator to vector<const intto write to it? It doesn't seem like it should be possible, but Visual C++ 2005 Express lets me: vector<const intvc; vc.push_back(5); vector<const int>::iterator iter = vc.begin(); *iter = 3; // compiles
6
3436
by: muzicmakr | last post by:
I'm porting some code from windows to mac, and there are some instances of std::vector<const MyType>, that compiled just fine on the pc, but won't compile under gcc. I'd never tried to do this particular construct myself, and after some searching online, I found a post somewhere saying this isn't legal c++ because the standard containers need types that are assignable. My questions-- is that correct? Why does visual studio allow it if...
12
5695
by: eiji.anonremail | last post by:
Hi all, I'm facing some uncertainty with const template arguments. Maybe someone could explain the general strategy. #include <vector> int main(int arc, char** argv) {
0
9794
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10778
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10538
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10210
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3077
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.