473,325 Members | 2,608 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,325 software developers and data experts.

Arrays and functions

15
Hi, read the following code,
Expand|Select|Wrap|Line Numbers
  1. #include "IntegerArray.h"
  2. using namespace std;
  3.  
  4. int main() {
  5.     int size, n;
  6.  
  7.     cout << "Size of a single array: ";
  8.     while(true) {
  9.         cin >> size;
  10.         cin.ignore();
  11.         if(size >= 1 && size <= 100) break;
  12.     }
  13.  
  14.     int * a = new int[size];
  15.     int * new_a = new int[size];
  16.     int * b = new int[size];
  17.     int * new_b = new int[size];
  18.  
  19.     cout << "(to input): "; 
  20.     cin >> n;
  21.     cin.ignore();
  22.     new_a = input_array(a, n);
  23.  
  24.     cout << "(to copy): "; 
  25.     cin >> n;
  26.     cin.ignore();
  27.     new_b = copy_array(b, new_a, n);
  28.  
  29.     cout << "(to display): ";
  30.     cin >> n;
  31.     cin.ignore();
  32.     display_array(new_b, n);
  33.  
  34.     delete [] a;
  35.     delete [] new_a;
  36.     delete [] b;
  37.     delete [] new_b;
  38. }
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int * const input_array(int * const a, int n) {
  5.     int p;
  6.     for(int i=0; i<n; ) {
  7.         cin >> p;
  8.         cin.ignore();
  9.         a[n] = p;
  10.         i++;
  11.     }
  12.  
  13.     return a;
  14. }
  15.  
  16. int * const copy_array(int * const a, int * const b, int n) {
  17.     for(int i=0; i<n; ) {
  18.         a[n] = b[n];
  19.         i++;
  20.     }
  21.  
  22.     return a;
  23. }
  24.  
  25. void display_array(int * const a, int n) {
  26.     for(int i=0; i<n; ) {
  27.         cout << "[" << i << "] " << a[i] << endl;
  28.         i++;
  29.     }
  30. }
The error I get is,
Expand|Select|Wrap|Line Numbers
  1. *** glibc detected *** ./a.out: double free or corruption (fasttop): 0x09ffe008 ***
I'm tired now, that's why I may just have missed something. Thanks for help...
Aug 26 '11 #1
0 1211

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

Similar topics

2
by: Jason | last post by:
I have a number of arrays that are populated with database values. I need to determine which array has the highest ubound out of all the arrays. The array size will always change based on the...
7
by: csx | last post by:
Hi everyone! two quick questions relating to arrays. Q1, Is it possible to re-assign array elements? int array = {{2,4}, {4,5}}; array = {2,3}
19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
7
by: coinjo | last post by:
How to return an array from a function?
20
by: J de Boyne Pollard | last post by:
MThe library functions which are included to allow process Mlaunch, forking, and termination, imply that it is both Mpossible and desirable for a process to fork itself. This is Ma fundamental...
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
by: datapata | last post by:
Hi I have some C++ code that I need to access from Python. I don't need to access any classes or anything, just a couple of functions that take one array as input and produce another array as...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.