Sefmentation Fault

This is a discussion on Sefmentation Fault within the C++ Programming forums, part of the General Programming Boards category; hi..i came across segmentation fault in my code i thought it may be cos of array bounnd so i did ...

  1. #1
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    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. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if((calculated value) >maxedges)) maxedges=calculatedvalue;
    and this goes before or after the new calls?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    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
    please
    thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dpp View Post
    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
    please
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    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. #7
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    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. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dpp View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    Quote 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++.

    Quote 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?

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    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. #11
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    vector<int> myvector (10000);



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

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dpp View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    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;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    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
    Last edited by dpp; 01-15-2009 at 09:40 PM.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

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