1. Sefmentation Fault

hi..i came across segmentation fault in my code
i thought it may be cos of array bounnd so i did :

long long int maxedges=10000;
static long long int *position,*color,*E_Name,*v1,*v2,*altnum_Edges;

position=new long long int[maxedges];
color=new long long int[maxedges];
E_Name=new long long int[maxedges];

and i checked at one place whether the size is getting bigger

if((calculated value) >maxedges)) maxedges=calculatedvalue;
but sti ll am getting segmentation fault

2. Code:
`if((calculated value) >maxedges)) maxedges=calculatedvalue;`
and this goes before or after the new calls?

--
Mats

3. thanks for the reply...can u elabrate on how do i do that

i dint use "new"....
can u guide me in using it with example
thanks

4. Originally Posted by dpp
and i checked at one place whether the size is getting bigger

if((calculated value) >maxedges)) maxedges=calculatedvalue;
Checking is one thing, but did you actually expand the array? Why not use a std::vector<long long>? (long long is not yet standard, by the way.)

You might want to post the smallest and simplest compilable program that demonstrates the problem.

5. Originally Posted by dpp
thanks for the reply...can u elabrate on how do i do that

i dint use "new"....
can u guide me in using it with example
thanks
Eh? What is this then:
Code:
```position=new long long int[maxedges];
color=new long long int[maxedges];
E_Name=new long long int[maxedges];```
--
Mats

6. i do not no about vectors...
well let me explain my scenario

cin>>a>>b;

cin>>e[a+b+a*b];

at any point of time i will have the values of a and b ...with the help of which i shud identify e..so i chose this technic like e[a+b+a*b] to identify this 'e' belongs to "a" and "b"...

but this leads to out of bound

7. well i meant that i dint use "new" after that assignment of maxedges= calculated value;
i tot it will take care on its own...

8. Originally Posted by dpp
well i meant that i dint use "new" after that assignment of maxedges= calculated value;
i tot it will take care on its own...
No, you would have to figure out what the maximum value is - or use vectors as suggested.

Try having a look for "tutorial C++ vectors" in google. (or see if any of the tutorials here cprogramming.com show you how to use vectors).

--
Mats

9. Originally Posted by dpp
i do not no about vectors...
I suggest that you start learning now. Search the Web (e.g., cppreference.com has some reference material on std::vector) and/or get a book like Accelerated C++.

Originally Posted by dpp
at any point of time i will have the values of a and b ...with the help of which i shud identify e..so i chose this technic like e[a+b+a*b] to identify this 'e' belongs to "a" and "b"...
Sorry, but I do not understand what you are trying to do. How will a and b help you identify e? Or are you saying that you will use a and b to compute the index of the element of e that you wish to access?

Originally Posted by dpp
well i meant that i dint use "new" after that assignment of maxedges= calculated value;
i tot it will take care on its own...
Assigning to the variable originally used for the array size certainly won't automagically expand the array.

10. exactly...
i use a and b to compute the index of e...

well i brwosed a few about vectors..(dynamically genertaed bounds dealt here)

will u be patient enought to get me a sample since my scenario is well explained
well i guess i can use insert at

but i dont use itertaors...can i have an example dealing my scenario please

11. vector<int> myvector (10000);

cin>>a>>b;
myvector.at(a+b+a*b)=i;
will this do

12. Originally Posted by dpp
vector<int> myvector (10000);

cin>>a>>b;
myvector.at(a+b+a*b)=i;
will this do
No, because both [] and at() require that the element is there already, and a+b+a*b may not be within the right range.

--
Mats

13. If the result of the expression (a+b+a*b) is negative or not less than 10000 then you will have a problem. It seems like maybe what you really want is a std::map<long long, int>, upon which you can (more) safely write:
Code:
```std::map<long long, int> elements;
// ...
cin >> a >> b;
elements[a + b + a * b] = i;```

14. thanks

well i used mapand got that prob solved.. but still another problem has encountered which is too much of space

since i used e[v1+v2+v1*v2]

and v1 and v2 may be large...

i did this because i have to uniquely identify the index of e using v1 and v2 values.....

any other suggestion which will avoid such v1+v2+v1*v2 big values so that space can be saved

thanks

15. Too much space? A map maps a key to a value, so as long as the result of the expression fits into the range of long long and is unique then you are fine. The other integers in between would not be placed into the map as keys.

That said, there might be better solutions, but I first would like to hear what exactly a, b and your container is supposed to solve.