473,395 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

weird problem with new

86
actually i am trying to dynamically allocate spaces for an charecter array that is to be copied from a c++ string...

suppose..
Expand|Select|Wrap|Line Numbers
  1.  
  2. string sum_string ="";
  3. getline(cin,sum_string);
  4.  
  5. char *array =new char[sum_string.length()+1];
  6.  
  7. sum_string.copy(array, sum_string.length()];
  8.  
  9. cout<<sum_string.length()<<endl;
  10.  
  11. cout<<strlen(array)<<endl;
  12.  
  13. ....
  14.  
  15. delele [] array;
  16. array =NULL;
  17.  
  18.  
suppose my string is "mynameis" now its length is 8 so that 8+1 spaces will be allocated for array..

ok fine sum_string.length() shows 8
BUT
strlen(array) is showing 12 ???

but that nothing strange yet...

for strings with greater length particularly those containing spaces both the output is same surprisingly......

again if i manually allocate 40 spaces i.e., char *array =new char[40];

then its doing fine with "mynameis"....



please help ASAP....
May 10 '07 #1
4 1160
Ganon11
3,652 Expert 2GB
There might be trouble because you are not ending your character array with a '\0'. After you copy the string over, you should manually set the last element in marray to the null character.
May 10 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Actually, you MUST manually add the \0 as ganon11 says to do.

The basic_string::copy() does not append the \0. It only copies the length specified.

As a minor point, basic_string::length() is deprecated. You are supposed tio use basic_string::size() instead.
May 10 '07 #3
ayan4u
86
ok thanx for all suggestions but what do you mean by deprecated....

but again why its working fine with longer strings particularly those containing spaces...
May 10 '07 #4
Ganon11
3,652 Expert 2GB
Deprecated means that the function is not the latest version; size() is better and more up-to-date. For now, the length() function still exists, but eventually it will be removed from the standard string class.

At least, this is the impression I got about deprecated classes ion Java, and I assume the topics are similar.
May 10 '07 #5

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

Similar topics

2
by: Gabriel Afana | last post by:
I have a simple php script to just send email.....When I first load the script in a browser...it sends 2 emails. Why?? The weird thing is then when I refresh the page, it send only one email...I...
3
by: redneck_kiwi | last post by:
Hi all: I have a really weird problem. I am developing a customer catalog system for my company and as such have delved into sessions for authentication and access levels. So far, I have managed...
0
by: LRW | last post by:
I manage our mySQL database through putty (SSH terminal client). And whenever I do a select * from the table that contains ENCODEd passwords, the funky characters do funky things with the display....
1
by: Kaneda | last post by:
Hello everyone! I have some weird(?) problems, and I am not quite sure if there are due to my errors or maybe a limitation in the .Net framework. I have a ComboBox I need to fill with the...
0
by: Kaneda | last post by:
Hello everyone! I have some weird(?) problems, and I am not quite sure if there are due to my errors or maybe a limitation in the .Net framework. I have a ComboBox I need to fill with the...
6
by: Unicorn | last post by:
I can only say this is a weird problem! I will be sitting there typing away in the code window, when the whole IDE just vanishes without a trace. I get no error messages and no indication, it...
82
by: nobody | last post by:
Howdy, Mike! mikecoxlinux@yahoo.com (Mike Cox) wrote in message news:<3d6111f1.0402271647.c20aea3@posting.google.com>... > I'm a C++ programmer, and have to use lisp because I want to use >...
13
by: Dmitry Tkach | last post by:
Hi, everybody! Here is a weird problem, I ran into... I have two huge (80 million rows each) tables (a and b), with id as a PK on both of them and also an FK from b referencing a. When I try to...
3
by: aling | last post by:
Execute following T-SQL within Queary Analyzer of SQL Server 2000: ======================================= DECLARE @dTest DATETIME SET @dTest='2001-1-1 1:1:1:991' SELECT @dTest SET...
0
by: P Pulkkinen | last post by:
Dear all, sorry, i know this code is far little too long to debug here, but there is really annoying logical error. If someone debugs this, I really offer warm virtual handshake. What this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.