473,396 Members | 1,780 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,396 software developers and data experts.

Problem using new

Hi!

I think I don't grasp the concept of "new". I tried to create a
2-dimensional array using new (see code below). What I don't understand
is, why does the program below work while the lines I commented out and
labelled as "case 1" and "case 2" yield either a segmentation fault
(case 1) or a type conflict (case 2).

Thanks in advance

Jochen

Quelltext:

#include <list>

class SomeClass {
std::list<int> *list_array[10][10]; // case 2
// std::list<int> list_array[10][10]; // case 1
public:
SomeClass() {
int i,j;
for (i=0;i<=10;i++) {
for (j=0;j<=10;j++) {
list_array[i][j] = new std::list<int>;
// list_array[i][j] = *(new std::list<int>); // case 1
}
}
//list_array = new std::list<int>[10][10]; // case 2
}
};

main() {
SomeClass *x = new SomeClass();
}
Jul 23 '05 #1
2 1167
ben
list<int>** list_array;

for (int i = 0; i < 10; ++i)
{
list_array[i] = new (list*)[10]; // you forgot to new each list<int>[]

for (int j = 0; j < 10; j++)
{
list_array[i][j] = new list<int>;
}
}

ben

"Jochen Luig" <jo***********@gmx.de> wrote in message
news:42**********************@news.freenet.de...
Hi!

I think I don't grasp the concept of "new". I tried to create a
2-dimensional array using new (see code below). What I don't understand
is, why does the program below work while the lines I commented out and
labelled as "case 1" and "case 2" yield either a segmentation fault
(case 1) or a type conflict (case 2).

Thanks in advance

Jochen

Quelltext:

#include <list>

class SomeClass {
std::list<int> *list_array[10][10]; // case 2
// std::list<int> list_array[10][10]; // case 1
public:
SomeClass() {
int i,j;
for (i=0;i<=10;i++) {
for (j=0;j<=10;j++) {
list_array[i][j] = new std::list<int>;
// list_array[i][j] = *(new std::list<int>); // case 1 }
}
//list_array = new std::list<int>[10][10]; // case 2
}
};

main() {
SomeClass *x = new SomeClass();
}

Jul 23 '05 #2
Jochen Luig wrote:

Hi!

I think I don't grasp the concept of "new". I tried to create a
2-dimensional array using new (see code below). What I don't understand
is, why does the program below work while the lines I commented out and
labelled as "case 1" and "case 2" yield either a segmentation fault
(case 1) or a type conflict (case 2).

Thanks in advance

Jochen
Part of your problems stem from the fact that you have an array overflow.
if you declare an array
int a[10]

then this array consists of 10 entries (you specify the number of entries
to the array when declaring it).

Valid indices to that array are
0, 1, 2, 3, 4, 5, 6, 7, 8, 9

(cout them, they are 10)
So the highest possible index into an array is always 1 less then the
number of elements you specified.

Now look at the following:
for (i=0;i<=10;i++) {
for (j=0;j<=10;j++) {


What values will i or j take

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

count them. They are 11! But you specified only 10 for the array sizes.

Thus the segmentation fault occours due to overflowing the array.

As for your second question:
Why do you have a type conflict.

You specified
std::list<int> *list_array[10][10];

That makes list_array a 10*10 array of pointers to list<int>
Note: This is an array of pointers. Not a pointer to an array!

Now it should be clear why

list_array = new std::list<int>[10][10];

gives a type conflict.
On the left side of the assignement there is an array, while on
the right side there is a pointer.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Aaron Prohaska | last post by:
I have just run into a problem where I have a page that posts back to itself to execute code, except when the page does the post back it somehow executes code that is in our home page for the site....
9
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
5
by: IkBenHet | last post by:
Hello, I use this script to upload image files to a folder on a IIS6 server: ******************* START UPLOAD.ASPX FILE ********************** <%@ Page Language="VB" Debug="true" %>
8
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.