C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 25th, 2006, 11:39 AM
donjonson donjonson is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 4 donjonson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 47 sec
Reputation Power: 0
I have a runtime error that I cant figure out

the runtime error I get is

Run-Time Check Failure #2 - Stack around the variable 'chChount' was corrupted.

I cant see anything wrong with what I am doing here can you?

here is my code


#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
string buffer;
char chbuf;

long chCount[256]= {0};
//memset(chCount, 0, 256 * sizeof(int));

cout<<"please entere a filename to analyze: ";
getline(cin, buffer);
infile.open(buffer.c_str(), ios::in);

if(infile.is_open())
{
while(!infile.eof())
{

chbuf = infile.get();

chCount[chbuf]++;
}

for(int i =0;i<256;i++)
{
if(chCount[i] != 0)
{
cout<<char(i)<<": "<<chCount[i]<<endl;
}
}

infile.close();
}
return 0;
}

Reply With Quote
  #2  
Old March 25th, 2006, 12:29 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Logical Flaw in your code at the point where you have written stmt.

Code:
 chCount[chbuf]++;


Here you are incrementing array chcount ( i.e trying to change the address of array. This is not allowed with an array.

Secondly, i presume you are trying to count characters.Then also
chBuf ( though act like an integer) but can give error.

Do chCount[chbuf++] instead.

Reply With Quote
  #3  
Old March 25th, 2006, 02:07 PM
donjonson donjonson is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 4 donjonson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 47 sec
Reputation Power: 0
Quote:
Originally Posted by Cirus
Logical Flaw in your code at the point where you have written stmt.

Code:
 chCount[chbuf]++;


Here you are incrementing array chcount ( i.e trying to change the address of array. This is not allowed with an array.

Secondly, i presume you are trying to count characters.Then also
chBuf ( though act like an integer) but can give error.

Do chCount[chbuf++] instead.


would that really increment the array aelement int the chbuf location?

I may be wrong but it looks to me that it would simply call teh the chbuf + 1 element in chCount array then do nothing with that element.

What I want to do is increment the integer located in the chCount[chbuf] location

I dont understand why this causes a problem. an array is just a pointer to data. why cant I increment the data at that location?

Reply With Quote
  #4  
Old March 25th, 2006, 03:12 PM
donjonson donjonson is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 4 donjonson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 47 sec
Reputation Power: 0
I figured it out.

The problem is that I wasn't checking to make sure that the data I pulled from the file was an array. when I tried to access the chbuf location of a size 256 array and chbuf > 256 well that is a problem isn't it. hehe

but my method of incrementing the worked ok.

Reply With Quote
  #5  
Old March 25th, 2006, 04:59 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
An Array though a pointer is different from a normal pointer in that it is a constant pointer with fixed address assigned to it during compile time.In case of normal pointer address can be changed using ncrement operator.

Reply With Quote
  #6  
Old March 25th, 2006, 05:26 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Quote:
Originally Posted by donjonson
would that really increment the array aelement int the chbuf location?

I may be wrong but it looks to me that it would simply call teh the chbuf + 1 element in chCount array then do nothing with that element.

What I want to do is increment the integer located in the chCount[chbuf] location

I dont understand why this causes a problem. an array is just a pointer to data. why cant I increment the data at that location?


I hink you are right.Sorry, I was mistaken.
It will increment the elelemnt pointed to by that position in the array.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > I have a runtime error that I cant figure out


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT