LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-01-2007, 09:48 AM   #1
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Rep: Reputation: 30
error: new : cannot specify initializer for arrays


Code:
#include <vector>
#include <iostream>

using std::cout;
using std::vector;

enum {DATASIZE = 20};
typedef unsigned char data_t[DATASIZE];


int setvalue(UInt8 *pValue)
{
    for (int i = 0; i < DATASIZE; i++)
       pValue[i] = '1' + i;
    return 0;
}

void displayvalue(UInt8 *pValue)
{
    for (int i = 0; i < DATASIZE; i++)
        cout << pValue[i] << "\n";
}


int main(int argc, char* argv[])
{
    enum {MAX_PROC = 5, MAX_OBJECT = 10000};

    // I want to use vector instead of array because the array size is determined by a variable
    //data_t data[MAX_PROC][MAX_OBJECT];

    vector<data_t> v[MAX_PROC] (10); // compile error here!
    for (int i = 0; i < MAX_PROC; i++) {
       setvalue(v[i][0]);
    }
    cout << "data\n";
    for (i = 0; i < MAX_PROC; i++) {
        displayvalue(v[i][0]);
    }

    return 0;
}
The above program is a test program to test how to use vector instead of array for my own project, so the setvalue and displayvalue function is simplied for test purpose (they are actually functions from another library which I cannot change).
I want to keep data_t defined as an array of unsigned char.
There is a compile error for the above program:
error: new : cannot specify initializer for arrays

How to fix this compile error?
Thanks in advance.

Last edited by powah; 02-01-2007 at 09:56 AM.
 
Old 02-01-2007, 01:58 PM   #2
asgeirss
Member
 
Registered: Aug 2003
Location: Vancouver, Canada
Distribution: RH, SL
Posts: 37

Rep: Reputation: 15
Hi,

The nice thing about <vector> and related classes, is that you don't have to statically declare their size at compile time! This means that your declaration is invalid, because you don't give a dimension to v, it is sized dynamically during execution as you append and remove items from the vector. e.g.

vector<data_t> v;
for (int i = 0; i < MAX_PROC; i++) v.push_back(data[i]);

For some examples of 2 and 3D, look at the following web page:
http://www.yolinux.com/TUTORIALS/Lin...TL.html#VECTOR

Basically 2,3d vectors (matrices) are just vectors of vectors, etc.
Good luck.
- Dave
 
Old 02-01-2007, 04:15 PM   #3
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by asgeirss
Hi,

The nice thing about <vector> and related classes, is that you don't have to statically declare their size at compile time! This means that your declaration is invalid, because you don't give a dimension to v, it is sized dynamically during execution as you append and remove items from the vector. e.g.

vector<data_t> v;
for (int i = 0; i < MAX_PROC; i++) v.push_back(data[i]);

For some examples of 2 and 3D, look at the following web page:
http://www.yolinux.com/TUTORIALS/Lin...TL.html#VECTOR

Basically 2,3d vectors (matrices) are just vectors of vectors, etc.
Good luck.
- Dave

Code:
    vector<data_t> v[MAX_PROC]; // this will compile
    for (int i = 0; i < MAX_PROC; i++) {
       setvalue(v[i][0]); // run time error
    }
If I do not declare their size at compile time, then there is a core dump at setvalue(v[i][0]);
I cannot change the function setvalue.
What can I do?

Last edited by powah; 02-01-2007 at 04:26 PM.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM
pthreads: mutex initializer cardias Programming 1 09-03-2004 12:53 PM
error code for pointers and 2-Dimensional arrays shams Programming 1 08-06-2004 10:00 PM
segmentation error c++ arrays with objects ajay_linux LQ Suggestions & Feedback 0 04-30-2004 11:21 AM
Arrays in C++ kalleanka Programming 5 02-07-2004 12:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration