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
5 15178
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.
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.
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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);
|
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,
|
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){
|
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;
| |
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
|
by: Javier |
last post by:
Hello,
thanks for the replies to my questions.
I have one more:
class A
{
public:
m1() const;
m2();
};
|
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
|
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...
|
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)
{
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |