### Segmentation Fault by Using Vectors

Hi, I was studying hash functions and I wrote this code for applying what I learned but it's giving me a segmentation fault while assigning the new node to the table. I'm such a beginner and didn't manage to find a solution.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````#include #include class Node { int data; Node* next; public: static void hashInsert(std::vector&, int); }; void Node::hashInsert(std::vector& hashTable, int key) { int temp = key % 10; Node* newNode = new Node; newNode->data = key; newNode->next = nullptr; if (hashTable.at(temp) == nullptr) { hashTable.at(temp)->next = newNode; //Here, segmentation fault. } else { Node* holder = new Node; holder = hashTable.at(temp); while (holder->next == nullptr) holder = holder->next; holder->next = newNode; } } int main() { std::vector hashTable (10, nullptr); std::vector keys {16, 12, 25, 39, 6, 122, 5, 68, 75}; for (auto i : keys) Node::hashInsert(hashTable, i); }``````
If hashTable.at(temp) is null then you can obviously not access hashTable.at(temp)->next.
@Peter87 then how can I implement this idea? Or should I make all datas 0 and all nexts null? Also, debugger shows there are its next and data.
Last edited on
In the beginning you have no "datas" and "nexts". All you have is 10 null pointers.

What you actually want to do in this case is to modify the null pointer so that it points to the new node, like this:

 `` `` ``hashTable.at(temp) = newNode;``
Thanks for helping me out. I added a statement if a node is null assign an init node to it and it worked like a charm.
 I added a statement if a node is null assign an init node to it and it worked like a charm.

This sounds to me like you might be creating unnecessary nodes (with garbage data values?). There is no need for that. You only need one node per "key". No more nodes than that.

Talking about unnecessary nodes, you don't need to create a new node on line 26. It's never used for anything, and it's never destroyed, so it's just a memory leak.

Note that a node (object) is never null. It is the Node* (node pointers) that might be null.
Last edited on
You're 100% right, I used an unnecessary node because I wanted to see if the first nodes are empty.

You're right about line 26.

And thanks for the last sentence, it really enlighted me.

Also, my Malwarebytes blocked your website.

Also, should I start with SDL or SFML, or unreal engine? I want to go a little beyond with c++ but I can't decide what to do. Or should I try some Qt or VC++?
 You're 100% right, I used an unnecessary node because I wanted to see if the first nodes are empty.

Again, nodes are never empty/null (unless you use a special data value to express that a node is "empty" but that probably just complicate things for no good reason).

What you have is essentially 10 singly linked lists.

If a list is empty the "head" pointer (in the std::vector) will be null.

 Also, my Malwarebytes blocked your website.

Does it say why? I don't think my website contains any "malware" but if it does then I would like to know.

If it's a "false positive" and it's just blocking it because it doesn't trust it, perhaps because it contains .exe files that you can download, then I don't think there is much I can do.

 Also, should I start with SDL or SFML, or unreal engine?

I'm a big fan of free software so if you ask me I would recommend SDL or SFML over Unreal Engine, but I don't decide over you, it's your decision.

https://en.wikipedia.org/wiki/Free_software

Personally I have more experience with SDL. I think it's great but SFML also seems good and it might be easier especially if you're not familiar with C.

SFML is a C++ library. SDL is a C library but you can of course use it in C++. That's what I do.

SFML seems to have a bit better official tutorials and documentation than SDL.

https://www.sfml-dev.org/learn.php
https://wiki.libsdl.org/SDL2/FrontPage
Last edited on
I think your site doesn't have this HTTPS certificate. That's why chrome says the site is not secure too.

Thanks for these and I think I'll continue with SFML. But last summer I started to watch a tutorial about how to use SFML, it was too complicated for me. Also, I looked at some simple games like making tic tac toe clone on SFML but same it was complicated for me.

What about C++ visual frameworks? Like Qt, ImGui
they are different things. The window frameworks are for 'dialogs' or 'windows' and user-interface. There is overlap, and those tools can play a video or display an image or even allow you to directly draw into a window and do crude graphics or very simple games, so sure, for tic-tac-toe you can certainly do just that and its fine. But these are not high end gaming tools, so at some point their limitations for graphics will cause you to want to go back to a dedicated 3d gaming library.
bydrachen wrote:
I think your site doesn't have this HTTPS certificate. That's why chrome says the site is not secure too.

It's available with both HTTP and HTTPS.

For some reason this website does not allow the profile website URL to start with https://

cplusplus dot com wrote:
There were errors while processing your submission. Please correct them and submit again.
Please, use only the http:// scheme prefix for your website's url
Last edited on
Line 29 is wrong. It should be `while (holder->next != nullptr)`

The whole hashInsert() function can be simplified and made faster by inserting at the front of the list instead of the back:
 ``12345678`` ``````void Node::hashInsert(std::vector& hashTable, int key) { int temp = key % 10; Node* newNode = new Node; newNode->data = key; newNode->next = hashTable.at(temp); hashTable.at(temp) = newNode; }``````

 ``123`` `````` Node* newNode = new Node; newNode->data = key; newNode->next = hashTable.at(temp);``````

These always make me wonder whether C++ could somehow initialize whole object on construction, rather than this adjustment of members afterwards ...

(It can, with constructors.)
Topic archived. No new replies allowed.