Dynamic array problem

This is a discussion on Dynamic array problem within the C++ Programming forums, part of the General Programming Boards category; Can anybody tell me why this code gives me errors? Code: ifstream infile("text.txt"); infile >> numrecords_str; numrecords = atoi(numrecords_str); Record ...

  1. #1
    new here
    Guest

    Dynamic array problem

    Can anybody tell me why this code gives me errors?
    Code:
    	ifstream infile("text.txt");
    
    	infile >> numrecords_str;
    
    	numrecords = atoi(numrecords_str);
    
    	Record records[numrecords];
    Errors:
    Code:
    error C2057: expected constant expression
    error C2466: cannot allocate an array of constant size 0
    error C2133: 'records' : unknown size
    Thanks

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Hi,

    Dynamic arrays have to be allocated with the 'new' operator or the 'malloc' function.
    Code:
    ifstream infile("text.txt");
    
    infile >> numrecords_str;
    
    numrecords = atoi(numrecords_str);
    
    Record records = new Record[numrecords];
    
    // After your done using the memory you allocated release it
    delete [] records;
    Or you can consider using "std::vector" and don't worry about these details!

  3. #3
    new here
    Guest
    Code:
    error C2440: 'initializing' : cannot convert from 'class Record *' to 'class Record'

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Change augur code to

    Code:
    ifstream infile("text.txt");
    
    infile >> numrecords_str;
    
    numrecords = atoi(numrecords_str);
    
    Record *records = new Record[numrecords];
    
    // After your done using the memory you allocated release it
    delete [] records;
    But actually u dont need to allocate dynamic memory
    Code:
    #include <iostream>
    #include <fstream>
    #include <conio.h>
    #include <string>
    
    using namespace std;
    
    int main(void)
    {
    string numrecords_str;
    
    ifstream infile("c:\\text.txt");
    
    infile >> numrecords_str;
    
    int numrecords = atoi(numrecords_str.c_str());
    
    int array[numrecords];
    
    for (int j = 0; j < numrecords; j++)
        array[j] = j*2+3;
        
    for (int j = 0; j < numrecords; j++)
        cout << array[j] << endl;
    
    getch();
    return 0;
    }
    But still I would recommend dynamic memory allocation or i.e std::vector class that augur suggested, insteed of my code. If the array is to large you might encounter a stack overrun(overflow).

  5. #5
    nag
    nag is offline
    Registered User nag's Avatar
    Join Date
    May 2002
    Posts
    22
    Originally posted by ripper079

    Code:
    string numrecords_str;
    
    ifstream infile("c:\\text.txt");
    
    infile >> numrecords_str;
    
    int numrecords = atoi(numrecords_str.c_str());
    
    int array[numrecords];
    It would return error again,can you declare an array of numrecords size because it is variable!!

    It is better to use dynamic array with new operator and using it through a pointer!
    Last edited by nag; 03-12-2003 at 05:11 AM.

  6. #6
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Originally posted by nag

    It would return error again,can you declare an array of numrecords size because it is variable!!
    Of course u can!

    Perfect legal
    Code:
    #include <iostream>
    
    int main(void)
    {
    const int avariable = 5;
    int array[avariable];
    return 0;
    }
    Iīve compiled my code(original) and tested the program and it runs well. Are u experiating some problem with it???

    Maybe you are confused by the similair variable names numrecords_str and numrecords.

  7. #7
    nag
    nag is offline
    Registered User nag's Avatar
    Join Date
    May 2002
    Posts
    22
    this code
    Code:
    #include <iostream>
    #include <fstream>
    #include <conio.h>
    #include <string>
    
    using namespace std;
    
    int main(void)
    {
    string numrecords_str;
    
    ifstream infile("c:\\text.txt");
    
    infile >> numrecords_str;
    
    int numrecords = atoi(numrecords_str.c_str());
    
    int array[numrecords];
    
    for (int j = 0; j < numrecords; j++)
        array[j] = j*2+3;
        
    for (int j = 0; j < numrecords; j++)
        cout << array[j] << endl;
    
    getch();
    return 0;
    }
    On MS VC++6.0 the very first error is


    C:\Documents and Settings\1590\Desktop\tt\tt.cpp(18) : error C2057: expected constant expression


    yes the line number 18 is not well ,in your second piece of code you are using

    const int

    thats why it works
    Two men looked out from Prison Bars,One saw the mud,the other saw stars.

  8. #8
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Code:
    int numrecords = atoi(numrecords_str.c_str());
    
    int array[numrecords];
    Yeeh I wasnīt sure if this would work (my first reaction was that it woulnīt, beacuse it isnīt a constant expression) but when a compiled it with Dev c++ (ming) and it worked, I thoght I was wrong but apperntly I wasnīt. Hmmm, why does it work under Dev c++???

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    2
    Record *records = new Record[numrecords];
    if the number wasn't constant would you use pointer arithmetic on *records to access differnt values in the dynamic array? or is there an easier way?

  10. #10
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    no, records[int] should still work just fine
    just remember to delete it when you're done and you should be fine.

    see also:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Last edited by Cobras2; 03-12-2003 at 06:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem copy from array to another
    By s-men in forum C Programming
    Replies: 3
    Last Post: 09-07-2007, 01:51 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Dynamic Array Problem
    By adc85 in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2003, 01:29 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Dynamic array problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2002, 09:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21