473,748 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic array....probab le memory allocation problem..need advise!

Baka otaku
4 New Member
The program I need to write must accept code no, name and total bill amount for "n" number of customers and then print all the things along with the grand total. I worked out the program and while building it shows no errors. while debugging, it takes the first set of values but after that it says - The instruction at "0x00402161 " referenced memory at "0xcccccccc ". The memory could not be "written". Is there something wrong with the program or memory...please help.

The code is :

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class customer
  5. { private:
  6. int ncust;
  7. int *codeno;
  8. char **name;
  9. int *tbill;
  10.  
  11. public: 
  12. void cust_accept(void);
  13. void cust_print(void);
  14. };
  15.  
  16. void customer::cust_accept(void)
  17. { cout << "Enter the total number of customers " << endl;
  18. cin >> ncust;
  19. codeno=(int*)new int[ncust]; 
  20. name=(char**)new char*[ncust];
  21. for (int i=0; i<ncust; i++)
  22. { name[i]=(char*)new char[20];
  23. }
  24. for (int j=0; j<ncust; j++)
  25. { cout << "Enter the customer code no " << endl;
  26. cin >> codeno[j];
  27. cout << "Enter customer name " << endl;
  28. cin >> name[j]; // insert name of customer
  29. cout << "Enter the total bill amount " << endl;
  30. cin >> tbill[j];
  31. }
  32. }
  33.  
  34. void customer::cust_print(void)
  35. { for(int i=0;  i<ncust; i++)
  36. { cout << "Customer" << endl; 
  37. cout << "Code no   Name   Total Bill amount " << endl;
  38. cout << codeno[i] ;
  39. cout << name[i] ;
  40. cout << tbill[i];
  41. }
  42. int gtotal;
  43. for (int j=0; j<ncust; j++)
  44. {gtotal+=tbill[j];
  45. cout << endl;
  46. cout << "Grand total" << gtotal;
  47. }
  48. }
  49.  
  50.  
  51. int main ()
  52. { customer srec;
  53. srec.cust_accept();
  54. srec.cust_print();
  55. return 0;
  56. }
  57.  
  58.  
Mar 6 '07 #1
1 2072
horace1
1,510 Recognized Expert Top Contributor
the memory you allocate for name is the number of customers
Expand|Select|Wrap|Line Numbers
  1. name=(char**)new char*[ncust];
  2.  
so if ncust is 3 each name is a maximum of 2 characters long
perhaps it should be
Expand|Select|Wrap|Line Numbers
  1. name=(char**)new char*[20];
  2.  
and the name can be up to 20 characters long.

would it not be better for class customer to hold data on one customer and then have a vector of customers?
Expand|Select|Wrap|Line Numbers
  1.     vector<customer> v;
  2.  
Mar 6 '07 #2

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

Similar topics

12
2395
by: Tan Thuan Seah | last post by:
Hi all, I was told this in one of the university course I was doing. In C we may expect good performance for: double a, c, d; for (i=0; i<N; i++) for(j=0; j<N; j++) a = a + c *d;
6
8212
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory allocation to create object of that class ? 2. If it says "dynamic memory allocation", is it mean the following code : DestinationAddress* dest = new DestinationAddress(); // code 1
11
3400
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I don't know how many points there will be in every struct in the end, so I have to allocate memory dynamically for them and can't use an array of fixed size, unfortunately. I would like to know if there is a better way to access struct members...
5
4142
by: Bill Carson | last post by:
I'm trying to dynamically allocate memory to an array of strings with the following (incomplete, for reference only) : int nLines, nChars, m, n, Cols.sTcolumn ; char ***sAtt; sAtt = (char ***)malloc(nLines * sizeof(char *)); for(m=0; m < nLines; m++) {
13
4717
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically create an array (using "new") since one doesn't know it size when writing the program. However, it looks to me that the size information must come from somewhere at run time, and this information can be passed to array creation function as a...
11
3051
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
19088
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
4
5069
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
1
7973
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
0
8996
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
8832
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
9386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9254
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...
0
8255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
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
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
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.