Reversing linked list C [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
node *rever(node *root)
{
node *prev = NULL;
node *xnew = NULL;
while (root != NULL) {
xnew = malloc(sizeof(root));
xnew->value = root->value;
xnew->next = prev;
prev = xnew;
root = root->next;
}
return xnew;
}
Hello I wrote this linked list reverse function. However it doesn't work(empty response): I suspect it's because of prev index getting overwritten. Could someone explain me whats going on? I know I could find working code on the internet but I wanna know what am I doing wrong.
Thanks

This looks close to correct. One problem I see: sizeof(root) is the same as sizeof(node*). That is, it's enough space for a pointer to a node.
You need enough space for an actual node struct. So malloc sizeof(*root) bytes (or sizeof(node), which is the same).

xnew = malloc(sizeof(root));
That should really be sizeof(*root) — you want a memory block the size of the structure pointed to, not the size of the pointer. As a result, you're likely overwriting whatever happens to be sitting in memory just past the pointer-sized block you allocated, resulting in undefined behavior, which could include anything from working as expected to causing demons to fly out of your nose.

Related

Trouble with C pointers when writing a linked list implementation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I've been getting back into C programming recently and have some issues with a linked list implementation I'm writing.
Here are my structure definitions:
typedef struct linked_list_entry {
struct linked_list_entry *next;
struct linked_list_entry *prev;
void* data;
} linked_list_entry;
typedef struct linked_list {
uint32 count;
struct linked_list_entry *first;
struct linked_list_entry *last;
} linked_list;
Here's the code in question:
int linked_list_add_entry(linked_list** linked_list, void* data)
{
linked_list_entry* new = malloc(sizeof(linked_list_entry));
...
DPRINT("last(%p), new (%p)\n", (*linked_list)->last, new);
(*linked_list)->last->next = new;
DPRINT("(*linked_list)->last(%p)->next (%p)\n", (*linked_list)->last, (*linked_list)->last->next);
With debug output:
data-types/linked-list.c:37:linked_list_add_entry(): last(0x7fa497402790), new (0x7fa4974027b0)
data-types/linked-list.c:39:linked_list_add_entry(): (*linked_list)->last(0x7fa4974027b0)->next (0x0)
Any idea why the last pointer changes to new and the new pointer changes to NULL?
Thanks
The dump you provided shows that the value of (*linked_list)->last has changed after you did
(*linked_list)->last->next = new;
But this line of code does not change (*linked_list)->last (at least, it is not supposed to).
Possible explanations for such "magical" behavior include
**linked_list is not properly allocated. Too little memory is allocated for
**linked_list object. As a consequence, *(*linked_list)->last happens to overlap the memory location of **linked_list. This is why the above assignment appears to change (*linked_list)->last as well. I.e. due to the aforementioned pointer problem (*linked_list)->last and (*linked_list)->last->next occupy the same location in memory.
Things like that can happen, for example, when incorrect (insufficient) amount of memory gets allocated for an object. In that case the problem is in the calling code. It supplies a "broken" list as input.
How does the calling code allocate memory for linked_list itself? Note that in this function you used name linked_list for function parameter, thus hiding the name of the type linked_list. If you did something like that in the "other" function - the one that allocated linked_list object itself - then trying to use sizeof(linked_list) in there would have returned incorrect value (size of pointer instead of side of struct type). That would explain underallocation of memory.
(*linked_list)->last is not a properly initialized pointer. It points into some unpredictable location. As a consequence, *(*linked_list)->last happens to overlap the memory location of **linked_list etc etc etc (see above).
The code you were running is not the code you posted here.
The answer is that I needed to use the struct keyword when using malloc:
linked_list_entry* new = malloc(sizeof(struct linked_list_entry));

malloc() on double pointer [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
double *p;
p = malloc(sizeof(p));
if (p != NULL)
{
*p = 5.15;
}
For some reason, p = malloc(sizeof(p));doesn't work. I try to allocate as much memory as p needs. What is wrong with that?
I try to allocate as much memory as p needs.
p itself (as a variable) has got the (own) memory allocated, what you're trying is basically allocate the memory for which p will be pointing to.
Here, p points to a double, so it needs to have a memory area to be able to store a double value. So, the allocation should be equal to the size of a double, i.e,
p = malloc(sizeof*p);

Getting error when trying to malloc memory to pointer variable declared separately [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have declared a char pointer in the following manner:
School *student[10];
for(i=0;i<10;i++){
*student[i] = malloc(sizeof(Student)); <--- Error points here
}
The error I get is:
incompatible types when assigning to type 'struct Student' from type 'void*'
Does anyone know why I am getting this error?
But how come if I were to allocate memory in the same line it would be with the star. For example: Student *name = malloc(sizeof(Student)); Why does this work? Im a bit confused
*student[i] = malloc(sizeof(School)); should be student[i] = malloc(sizeof(School));
students is an array of pointer to struct of type School. So you need to allocate for each pointer in that array. When you write *student[i] - you are dereferencing pointer i instead of allocating memory for it.
And as NicolasMiari pointed out, the sizeof operator must apply to School instead of student.
But how come if I were to allocate memory in the same line it would be with the star. For example: Student *name = malloc(sizeof(Student)); Why does this work? Im a bit confused
That's different. When you write Student *name = malloc(sizeof(Student)); you are both declaring a pointer and initialize it with malloc. You can do both steps in a single line like that. Alternatively, you declare it first, then assign it with malloc in a different line - in that case you must remove the asterisk.
You may want to refer to this question pointer initialization and pointer assignment.

Check whether struct has been initialized or allocated some memory yet [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to make a Linked List. So, I declared a LL data structure as below:
typedef struct linkedList
{
char* word;
struct linkedList* next;
} linkedList;
Now, I declared a global variable representing head of the LL as linkedList* head = NULL;
Now, I am surprised that even though I assigned a NULL value to the pointer I am able to get inside the below while loop.
linkedList* node = head;
while(node != NULL);
{
//Why I am here when I have not yet malloc'ed head and through global variable initialization my head is NULL
}
Because of this I am feeling like I have no way to check if my linked list head has been initialized or allocated some memory yet. I cannot check with node->next because it will result in segmentation fault error and node == NULL is passing!!!
Please note that:
Question is strictly for C language
I do not want to have some other global variable to track whether my linked list head has been initialized or allocated some memory yet. I want to do this using my linked list data structure. This is because suppose my shipping my data structure and want that if somebody is trying to delete something and linked list then empty then some meaning full error should be printed/thrown.
Any idea please.
It's because of this:
while(node != NULL);
Remove the semi-colon ;. Otherwise, when compiling, it doesn't know that the block between {...} is after the while, and will enter it, regardless of the verification in the while (...).

Why is the sizeof() this struct 8? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
struct t{
char days[20];
int date;
char x;
struct t *next;
}*head
printf("%ld\n", sizeof(head));
where sizeof(*void)=8, sizeof(int)=4, sizeof(char)=1
Why does it print 8?
head is a pointer to the struct t, which is 8 bytes since I'm assuming you're running an x64 program. If you want the size of the underlying type, do this:
sizeof(*head)
Notice that head is a pointer to the struct rather than an actual instance of the struct. This means that sizeof(head) is the size of the pointer, which on your system happens to be 8 (notice that sizeof(void*) is also 8).
Hope this helps!

Resources