C linked list with an array inside

  • Thread starter Thread starter baddin
  • Start date Start date
  • Tags Tags
    Array List
Click For Summary

Discussion Overview

The discussion revolves around a C programming issue related to implementing a linked list that contains arrays within its nodes. Participants explore the problem of only the last input being printed and the implications of reusing an array for multiple nodes.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant shares code and describes the issue of only the last number being printed, suggesting a problem with how nodes are linked.
  • Another participant humorously suggests the issue might be due to "voter fraud," indicating a misunderstanding of the program's logic.
  • Several participants point out that the same array of integers is being reused for each node, which leads to incorrect behavior.
  • One participant questions whether the array should be different for each node, indicating confusion about memory allocation and array handling.
  • A later reply explains that to maintain different values for each node, separate memory allocation for each array is necessary.
  • One participant confirms that after following the advice given, their program works correctly.
  • Another participant expresses ongoing confusion about using the same array with different values for each node, prompting further clarification about memory allocation and array management.

Areas of Agreement / Disagreement

Participants generally agree that reusing the same array for multiple nodes is problematic, and there is a consensus on the need for separate memory allocation for each node's array. However, there remains some confusion among participants regarding the implementation details and memory management.

Contextual Notes

Some participants express uncertainty about how memory allocation works in relation to arrays and linked lists, and there are unresolved questions about the best practices for managing arrays within linked list nodes.

baddin
Messages
24
Reaction score
0
I have pasted my code here : http://pastebin.com/nMNarH9i
It is in language C.
Basically, I am reading in 5 numbers at a time.
Putting these numbers in array one_vote_array.
Then I need to put this array in a structure vote_node.
I also then change my next pointer in the structure to head and then make head point to this new structure.
I want to to keep on making structures while I keep reading in 5 numbers.
The problem is my program is only printing the last number i inputted in it.
 
Technology news on Phys.org
Clearly a case of voter fraud.

so the way your program is written it will print the voting backward right? last voter to first voter.

but in your case it prints the last voter and stops so that implies that the temp->next is null.

Id go back to the first for loop and use your print test to see what voter->node is beiing set to as you iterate thru your voters.

Do you have a debugger that you can run your program thru?

it would allow you to review the values of variables step by step.
 
You are reusing the same array of five integers for each node. You need to allocate a new array of five integers for each node.
 
  • Like
Likes   Reactions: 1 person
D H said:
You are reusing the same array of five integers for each node. You need to allocate a new array of five integers for each node.

Good catch, but I thought we were supposed to lead the OP to the answer.
 
But my function read_one_vote changes the contents of the array one_vote_array everytime it is called, so shouldn't that array be different for each node?
 
How can it be different for each node? Your code makes exactly the opposite be true. It's the same array for each node.

If you need a different array for each node, you need to make a different array for each node.
 
  • Like
Likes   Reactions: 1 person
Ok, I did what you said, and it works now. Thank you very much.
 
I still am finding it confusing though, can't I use the same array with different value and set it to each node?
 
Because the values of one_vote_array change each time the function read_one_vote gets called
 
  • #10
baddin said:
I still am finding it confusing though, can't I use the same array with different value and set it to each node?

Think about it at the hardware level: If you need to keep track of say, 9 different arrays, how many arrays would you need to allocate in memory? Each block of memory can only hold one value at a time, so in order for your computer to "remember" what the content of each array is, it needs to allocate a separate memory block for each one.

What your asking is possible though. If you want, you can have a buffer in which you construct each array you want. Then when it is ready, you can copy it to another newly allocated array, and reset the original array. However, this still requires you to allocate a new array each time.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 25 ·
Replies
25
Views
3K
Replies
86
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
20
Views
2K