Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am stuck please help , i get a segmentation fault

this is something you can debug if you want to help me :
debug.h
```
#ifndef DEBUG_H_
#define DEBUG_H_

template <class T>
class SortedList
 {
  T** data;
  int size;
  int max_size;
  void expand();
  static const int EXPAND_RATE=2;
  static const int INITIAL_SIZE=10;
public:
SortedList();
void insert(const T&);
};
template <class T>
SortedList<T>::SortedList():data(new T*[INITIAL_SIZE]),size(0),max_size(INITIAL_SIZE){}
template<class T>
void SortedList<T>::expand()
{
    int new_size = max_size * EXPAND_RATE;
    T** new_data = new T * [new_size];
    for (int i = 0; i < size; ++i)
    {
        T Item(*(data[i]));
        T* ptr = &Item;
        new_data[i] = ptr;
    }
    delete[] data;
    data = new_data;
    max_size = new_size;
}
template <class T>
void SortedList<T>::insert(const T& object)
{
 if(size>=max_size)
 {
     expand();
 }
 int index=0;
 for(int i=0;i<size;i++)
 {
  T item=*data[i];
// when I try to print item nothing goes out
  if(item<object)
//      ^^^ here I get a segmentation fault
  {
    continue;
  }
  index=i;
  break;
 }
 size++;
 for (int i = size-1; i >index; i--)
 {
  data[i]=data[i-1]; 
 }
 T Item(object);
 T* ptr = &Item;
 data[index] = ptr;
 //when I print *data[index] it does get printed perfectly
}
#endif

```
debug.c
#include "debug.h"
#include <string>
using std::string;
int main()
{
  SortedList<string> lst1 = SortedList<string>();
    lst1.insert("Charlie");
    lst1.insert("Bob");
    return 0;
}


please please help.. I have been trying for hours
PS: I made data an array of pointers so that we could use the list even with a class that doesn't have a T()
if there is anything I wrote wrong and you want me to edit you can tell me in the comments below, thanks in advance!!

What I have tried:

I tried to do this in the insert function:
template <class T>
void SortedList<T>::insert(const T& object)
{
 if(size>=max_size)
 {
     expand();
 }
 int index=0;
 for(int i=0;i<size;i++)
 {

  if(*data[i] <object)
//      ^^^ here I get a segmentation fault
  {
    continue;
  }
  std::cout << *data[i];
  index=i;
  break;
 }
 size++;
 for (int i = size-1; i >index; i--)
 {
  data[i]=data[i-1]; 
 }
 T Item(object);
 data[index] = &Item;
 //when I print *data[index] it does get printed perfectly
 std::cout << *data[index];
}


when I did this I didn't get a segmentation fault but when I try to get to the
*data[i]

from the out side or anywhere else, it gives me nothing as if nothing was stored inside
Posted
Updated 12-Jun-21 4:09am
Comments
KarstenK 12-Jun-21 13:41pm    
learn to use the debugger to find the problematic code line.

In taking a few minutes to scan your code, it looks like you're trying to sort strings. But you create them by passing them as C strings ("Charlie" and "Bob"). Doing this creates a temporary string as an argument, and it disappears right after insert returns. After that, you'll briefly be looking at an empty string, and even worse things will happen later, when the string's original location gets reused and you try to interpret it as a string.
 
Share this answer
 
v2
Comments
Raghad Aea 12-Jun-21 10:26am    
yes that's what's happening, it's as if the data disappears, do you know how can I prevent it from happening?
Raghad Aea 12-Jun-21 10:28am    
like how can I pass them not as C strings? so that they wouldn't be temporary string?
Greg Utas 12-Jun-21 10:40am    
I'm amazed that you've graduated to writing a template that allocates memory without knowing how to do this! :)

There are various options. One is not to store pointers to strings, but rather the strings themselves. They will therefore be copied into the template. It is more common for a container template to store T instead of T*.

If that's not appropriate for your design, you could allocate the strings using new. That will keep them allocated until you delete them. But now, to prevent a memory leak, you to have to remember to delete them.

Storing T* instead of T would typically be done when someone already owns the object in question. The problem with this is that they can change the object without the template knowing it. Say "Bob" gets changed to "Doug". Now the names are no longer sorted! Or say "Bob" gets deleted, which is basically what is happening here. Now the template blows up.
A segmentation fault doesn't mean that the poitner was null: it means the pointer was invalid - it pointed at memory that didn't exist, or that doesn't "belong" to that process.

The first thing you need to do is us eteh debugger to find out the value of i (probably zero) and the value of data (probably rubbish)

That's some odd code: you appear to be using the value in data before you assign it ... which is probably related to your problem!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900