1

I'm a beginner, so I'm sorry if this is really dumb question/problem. The assignment that I have is printing out a dynamic array from an input file. I tried googling it and I found some similar problems... but the answers were all like "use vectors" etc but we haven't learned those yet. It's also said that a function must be used. This is what I came up with:

#include <iostream>
#include <fstream> //file input

using namespace std;

int *out(int *arr, int siz){

    arr = new int[siz];
    for (int i = 0; i < siz; i++) {
        cout << arr [i] << " ";
    }
    return arr; //this should print out the array later???

}

int main(){

    int siz;
    int *arr;

    ifstream inf ("input.txt");
    inf >> siz; //
    for (int i = 0; i < siz; i++) {
        inf >> arr[i];
    }
    inf.close();


    cout << "This array contains following elements: ";
    *arr = *out(arr, siz) ; 

    delete[] arr;
    return 0;}

So, it doesn't give any errors with Dev-C++ but when I try to run it, it crashes. I tried debugging it and then it gave me "segmentation error" or something like that. Then of course, I googled it and there must be something wrong with the pointers, right? Could you help me out? Thanks.

3
  • 2
    Use a more recent and C++11 standard conforming compiler (e.g. GCC), and compile with all warnings and debug info (g++ -Wall -g). Then use std::vector. Learn how to use the debugger (gdb) Commented Oct 20, 2014 at 10:57
  • Other than that, your parts of your code make no sense (The thing with the return value...) Commented Oct 20, 2014 at 10:58
  • 1
    You are reading your file to an unalloacated memory 'arr'. First allocate your memory and then read Commented Oct 20, 2014 at 10:58

4 Answers 4

1

arr is an uninitialized pointer. Do arr = new int[size]; before you read data into arr.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, I have no idea why I thought that. I'm having a look around to see what could have lead me to believe that.
It's a fair concern, just wasn't a valid argument in this case. C does not allow non-const integrals for array size during declaration.
THAT'S IT. I was trying to remember which language it was.
0

You are trying to access arr, when arr has not been allocated or initialized to a valid array. Your main needs to allocate arr before using arr to populate elements: So, here's the changed version:

#include <iostream>
#include <fstream> //file input

using namespace std;

void out(int *arr, int siz){
    for (int i = 0; i < siz; i++) {
        cout << arr [i] << " ";
    }
}

int main(){

    int siz;
    int *arr;

    ifstream inf ("input.txt");
    inf >> siz; 
    arr = new int[siz]; // added
    for (int i = 0; i < siz; i++) {
        inf >> arr[i];
    }
    inf.close();

    cout << "This array contains following elements: ";
    out(arr, siz); 

    delete[] arr;
    return 0;
}

Comments

0

You haven't allocated memory to the array, which you'd likely need to do with malloc. Once you've read in the size of the array, allocate the memory.

inf >> siz;
arr = malloc(siz * sizeof(*int));
//Rest of program

//delete[] arr; <- you use this with the new keyword
free(arr); //Use 'free' with malloc
return 0;

Comments

0

I think what you want might be sth like this

#include <iostream>
#include <fstream>
int main(){
    int siz(0);
    std::ifstream inf ("input.txt");//Assume that the input file and this file are in the same folder
    inf >> siz; //Assume that the first number in  input file is the size of array
    int *arr=new int[siz];
    for (int i = 0; (siz-i)&&inf ; ++i) {
        inf >> arr[i];
    }
    inf.close();

    std::cout << "This array contains following elements: ";
    for (int i = 0; siz -i ; ++i ) {
        std::cout << arr [i] << " ";
    }
    delete[] arr;
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.