Why won't this C program let me access memory?
I'm trying to do a linked list database and for some reason the program won't let me access the memory, or at least that's what the debugger says. I'm using the gdb debugger, codeblocks.
I've already compiled the program on another machine, with the same operating system (windows). It worked flawlessly, BUT the debugger showed the same error.
void print_ListEl(ListEl* head)
{
ListEl* current = malloc(sizeof(ListEl));
current = head;
if (head==NULL) exit(EXIT_FAILURE);
if (current->next==NULL)
{
puts("No elements");
return;
}
else
{
int i=1;
while(current->next!=NULL)
{
printf("%d.%sn", i, current->name);
current=current->next;
++i;
}
}
free(current);
}
It's used like that:
ListEl* element = malloc(sizeof(ListEl));
print_listEl(element);
This is the function that seems to cause the problem according to the debugger. When I watch the "current->next" variable, the debugger says "Cannot access memory at address". If I change current->next to current, the debugger still shows the same function as causing the problem. The ListEl structure is just a regular single linked list with a char type data.
struct ListEl
{
char name[MAX_CHAR];
struct ListEl* next;
};
I also use
typedef struct ListEl ListEl
Headers are safe with ifndef and endif and are all included, I've checked.
This function, instead of putting "No elements" when there's no elements in the list, spews out some random character and crashes the program.
c debugging struct segmentation-fault dynamic-memory-allocation
add a comment |
I'm trying to do a linked list database and for some reason the program won't let me access the memory, or at least that's what the debugger says. I'm using the gdb debugger, codeblocks.
I've already compiled the program on another machine, with the same operating system (windows). It worked flawlessly, BUT the debugger showed the same error.
void print_ListEl(ListEl* head)
{
ListEl* current = malloc(sizeof(ListEl));
current = head;
if (head==NULL) exit(EXIT_FAILURE);
if (current->next==NULL)
{
puts("No elements");
return;
}
else
{
int i=1;
while(current->next!=NULL)
{
printf("%d.%sn", i, current->name);
current=current->next;
++i;
}
}
free(current);
}
It's used like that:
ListEl* element = malloc(sizeof(ListEl));
print_listEl(element);
This is the function that seems to cause the problem according to the debugger. When I watch the "current->next" variable, the debugger says "Cannot access memory at address". If I change current->next to current, the debugger still shows the same function as causing the problem. The ListEl structure is just a regular single linked list with a char type data.
struct ListEl
{
char name[MAX_CHAR];
struct ListEl* next;
};
I also use
typedef struct ListEl ListEl
Headers are safe with ifndef and endif and are all included, I've checked.
This function, instead of putting "No elements" when there's no elements in the list, spews out some random character and crashes the program.
c debugging struct segmentation-fault dynamic-memory-allocation
1
Please provide an Minimal, Complete, and Verifiable example. This code doesn’t show cleary what ishead
, you talk about passing arguments to a function but there’s no function here etc. We need a complete code to know what’s happening.
– Sami Kuhmonen
Jan 1 at 19:23
You should update the code to show what the function parameters are. Clearly you are usingmalloc
andfree
but then comment elsewhere that you didn't mean to use it in that function. Ishead
passed into the function. Alsomalloc
doesn't clear the data it points to. In this caseelement
still needs to be initialized so thatname
andnext
have meaningful values.
– Fred
Jan 1 at 19:24
@user10605163 thank you, however after changing the code so that I don't use malloc or free, the function still seems to cause a problem in the debugger (and crashes the program) EVEN if I allocate memory to member "next" of the list element passed to the function.
– Mihu
Jan 1 at 20:00
@Mihu Yes the reason is missing initialization as pointed out by bruno. I have added my comment together with the initialization problem as answer.
– user10605163
Jan 1 at 20:14
add a comment |
I'm trying to do a linked list database and for some reason the program won't let me access the memory, or at least that's what the debugger says. I'm using the gdb debugger, codeblocks.
I've already compiled the program on another machine, with the same operating system (windows). It worked flawlessly, BUT the debugger showed the same error.
void print_ListEl(ListEl* head)
{
ListEl* current = malloc(sizeof(ListEl));
current = head;
if (head==NULL) exit(EXIT_FAILURE);
if (current->next==NULL)
{
puts("No elements");
return;
}
else
{
int i=1;
while(current->next!=NULL)
{
printf("%d.%sn", i, current->name);
current=current->next;
++i;
}
}
free(current);
}
It's used like that:
ListEl* element = malloc(sizeof(ListEl));
print_listEl(element);
This is the function that seems to cause the problem according to the debugger. When I watch the "current->next" variable, the debugger says "Cannot access memory at address". If I change current->next to current, the debugger still shows the same function as causing the problem. The ListEl structure is just a regular single linked list with a char type data.
struct ListEl
{
char name[MAX_CHAR];
struct ListEl* next;
};
I also use
typedef struct ListEl ListEl
Headers are safe with ifndef and endif and are all included, I've checked.
This function, instead of putting "No elements" when there's no elements in the list, spews out some random character and crashes the program.
c debugging struct segmentation-fault dynamic-memory-allocation
I'm trying to do a linked list database and for some reason the program won't let me access the memory, or at least that's what the debugger says. I'm using the gdb debugger, codeblocks.
I've already compiled the program on another machine, with the same operating system (windows). It worked flawlessly, BUT the debugger showed the same error.
void print_ListEl(ListEl* head)
{
ListEl* current = malloc(sizeof(ListEl));
current = head;
if (head==NULL) exit(EXIT_FAILURE);
if (current->next==NULL)
{
puts("No elements");
return;
}
else
{
int i=1;
while(current->next!=NULL)
{
printf("%d.%sn", i, current->name);
current=current->next;
++i;
}
}
free(current);
}
It's used like that:
ListEl* element = malloc(sizeof(ListEl));
print_listEl(element);
This is the function that seems to cause the problem according to the debugger. When I watch the "current->next" variable, the debugger says "Cannot access memory at address". If I change current->next to current, the debugger still shows the same function as causing the problem. The ListEl structure is just a regular single linked list with a char type data.
struct ListEl
{
char name[MAX_CHAR];
struct ListEl* next;
};
I also use
typedef struct ListEl ListEl
Headers are safe with ifndef and endif and are all included, I've checked.
This function, instead of putting "No elements" when there's no elements in the list, spews out some random character and crashes the program.
c debugging struct segmentation-fault dynamic-memory-allocation
c debugging struct segmentation-fault dynamic-memory-allocation
edited Jan 1 at 19:53
Mihu
asked Jan 1 at 19:08


MihuMihu
395
395
1
Please provide an Minimal, Complete, and Verifiable example. This code doesn’t show cleary what ishead
, you talk about passing arguments to a function but there’s no function here etc. We need a complete code to know what’s happening.
– Sami Kuhmonen
Jan 1 at 19:23
You should update the code to show what the function parameters are. Clearly you are usingmalloc
andfree
but then comment elsewhere that you didn't mean to use it in that function. Ishead
passed into the function. Alsomalloc
doesn't clear the data it points to. In this caseelement
still needs to be initialized so thatname
andnext
have meaningful values.
– Fred
Jan 1 at 19:24
@user10605163 thank you, however after changing the code so that I don't use malloc or free, the function still seems to cause a problem in the debugger (and crashes the program) EVEN if I allocate memory to member "next" of the list element passed to the function.
– Mihu
Jan 1 at 20:00
@Mihu Yes the reason is missing initialization as pointed out by bruno. I have added my comment together with the initialization problem as answer.
– user10605163
Jan 1 at 20:14
add a comment |
1
Please provide an Minimal, Complete, and Verifiable example. This code doesn’t show cleary what ishead
, you talk about passing arguments to a function but there’s no function here etc. We need a complete code to know what’s happening.
– Sami Kuhmonen
Jan 1 at 19:23
You should update the code to show what the function parameters are. Clearly you are usingmalloc
andfree
but then comment elsewhere that you didn't mean to use it in that function. Ishead
passed into the function. Alsomalloc
doesn't clear the data it points to. In this caseelement
still needs to be initialized so thatname
andnext
have meaningful values.
– Fred
Jan 1 at 19:24
@user10605163 thank you, however after changing the code so that I don't use malloc or free, the function still seems to cause a problem in the debugger (and crashes the program) EVEN if I allocate memory to member "next" of the list element passed to the function.
– Mihu
Jan 1 at 20:00
@Mihu Yes the reason is missing initialization as pointed out by bruno. I have added my comment together with the initialization problem as answer.
– user10605163
Jan 1 at 20:14
1
1
Please provide an Minimal, Complete, and Verifiable example. This code doesn’t show cleary what is
head
, you talk about passing arguments to a function but there’s no function here etc. We need a complete code to know what’s happening.– Sami Kuhmonen
Jan 1 at 19:23
Please provide an Minimal, Complete, and Verifiable example. This code doesn’t show cleary what is
head
, you talk about passing arguments to a function but there’s no function here etc. We need a complete code to know what’s happening.– Sami Kuhmonen
Jan 1 at 19:23
You should update the code to show what the function parameters are. Clearly you are using
malloc
and free
but then comment elsewhere that you didn't mean to use it in that function. Is head
passed into the function. Also malloc
doesn't clear the data it points to. In this case element
still needs to be initialized so that name
and next
have meaningful values.– Fred
Jan 1 at 19:24
You should update the code to show what the function parameters are. Clearly you are using
malloc
and free
but then comment elsewhere that you didn't mean to use it in that function. Is head
passed into the function. Also malloc
doesn't clear the data it points to. In this case element
still needs to be initialized so that name
and next
have meaningful values.– Fred
Jan 1 at 19:24
@user10605163 thank you, however after changing the code so that I don't use malloc or free, the function still seems to cause a problem in the debugger (and crashes the program) EVEN if I allocate memory to member "next" of the list element passed to the function.
– Mihu
Jan 1 at 20:00
@user10605163 thank you, however after changing the code so that I don't use malloc or free, the function still seems to cause a problem in the debugger (and crashes the program) EVEN if I allocate memory to member "next" of the list element passed to the function.
– Mihu
Jan 1 at 20:00
@Mihu Yes the reason is missing initialization as pointed out by bruno. I have added my comment together with the initialization problem as answer.
– user10605163
Jan 1 at 20:14
@Mihu Yes the reason is missing initialization as pointed out by bruno. I have added my comment together with the initialization problem as answer.
– user10605163
Jan 1 at 20:14
add a comment |
2 Answers
2
active
oldest
votes
You are not supposed to
malloc
forcurrent
. You are replacing the pointer obtained from it immediately withhead
and loose the allocation. The laterfree
will free the last element of the list, but will not set the pointer of the previous element toNULL
, causing undefined behavior if the list is used later.
You are allocating memory for a node here:
ListEl* element = malloc(sizeof(ListEl));
but you never set any of its values before calling
print_listEl(element);
. Therefore the pointerelement->next
will be indeterminate and comparison againstNULL
will not be useful. You dereference this pointer later inprint_listEl
causing undefined behavior. Always initialize aftermalloc
:
ListEl* element = malloc(sizeof(ListEl));
element->next = NULL;
memset(element->name, '', sizeof(element->name));
add a comment |
may be
ListEl* current = malloc(sizeof(ListEl));
current = head;
must be
ListEl* current = malloc(sizeof(ListEl));
head = current;
else why to do the malloc and lost it immediately ?
I hope you have additional code before
if (current->next==NULL)
because current is only allocated, not initialized
When I watch the "current->next" variable, the debugger says "Cannot access memory at address".
That means current was broken because for instance it was deleted, or you just never initialized the field next. the problem is you do not give Minimal, Complete, and Verifiable example
If you code is really that :
ListEl* element = malloc(sizeof(ListEl));
print_listEl(element);
element->name nor element->next are not initialized, so in print_listEl
it is the same
Well, that doesn't really solve my problem. I want current=head, not head=current so I should've just skipped using malloc.
– Mihu
Jan 1 at 19:15
2
@Mihu I edited my answer, the problem is you do not give us enough source code, we don't see how you initialize your cells
– bruno
Jan 1 at 19:22
True, the source is incomplete. @Mihu you need to usemalloc
or an equivalent to allocate dynamic memory, so you can't "just skipped using malloc" or you have no way to initialize an arbitrary amount of elements for your list. Reread this answer and try to understand it before you say it doesn't fix your problem.
– Dan Farrell
Jan 1 at 19:23
I've added an edit which might make the code more understandable. What I get from your answer, I cannot check if member "next" of a list is equal to NULL if I don't alloc "element.next"? How would I check if a list is empty then?
– Mihu
Jan 1 at 19:44
By "skipping malloc" I mean that it's unnecesary here, as I've allocated memory for "head" previously, so I guess i don't need to allocate it to "current" just to overwrite it with "head" in the next line.
– Mihu
Jan 1 at 19:47
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998192%2fwhy-wont-this-c-program-let-me-access-memory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are not supposed to
malloc
forcurrent
. You are replacing the pointer obtained from it immediately withhead
and loose the allocation. The laterfree
will free the last element of the list, but will not set the pointer of the previous element toNULL
, causing undefined behavior if the list is used later.
You are allocating memory for a node here:
ListEl* element = malloc(sizeof(ListEl));
but you never set any of its values before calling
print_listEl(element);
. Therefore the pointerelement->next
will be indeterminate and comparison againstNULL
will not be useful. You dereference this pointer later inprint_listEl
causing undefined behavior. Always initialize aftermalloc
:
ListEl* element = malloc(sizeof(ListEl));
element->next = NULL;
memset(element->name, '', sizeof(element->name));
add a comment |
You are not supposed to
malloc
forcurrent
. You are replacing the pointer obtained from it immediately withhead
and loose the allocation. The laterfree
will free the last element of the list, but will not set the pointer of the previous element toNULL
, causing undefined behavior if the list is used later.
You are allocating memory for a node here:
ListEl* element = malloc(sizeof(ListEl));
but you never set any of its values before calling
print_listEl(element);
. Therefore the pointerelement->next
will be indeterminate and comparison againstNULL
will not be useful. You dereference this pointer later inprint_listEl
causing undefined behavior. Always initialize aftermalloc
:
ListEl* element = malloc(sizeof(ListEl));
element->next = NULL;
memset(element->name, '', sizeof(element->name));
add a comment |
You are not supposed to
malloc
forcurrent
. You are replacing the pointer obtained from it immediately withhead
and loose the allocation. The laterfree
will free the last element of the list, but will not set the pointer of the previous element toNULL
, causing undefined behavior if the list is used later.
You are allocating memory for a node here:
ListEl* element = malloc(sizeof(ListEl));
but you never set any of its values before calling
print_listEl(element);
. Therefore the pointerelement->next
will be indeterminate and comparison againstNULL
will not be useful. You dereference this pointer later inprint_listEl
causing undefined behavior. Always initialize aftermalloc
:
ListEl* element = malloc(sizeof(ListEl));
element->next = NULL;
memset(element->name, '', sizeof(element->name));
You are not supposed to
malloc
forcurrent
. You are replacing the pointer obtained from it immediately withhead
and loose the allocation. The laterfree
will free the last element of the list, but will not set the pointer of the previous element toNULL
, causing undefined behavior if the list is used later.
You are allocating memory for a node here:
ListEl* element = malloc(sizeof(ListEl));
but you never set any of its values before calling
print_listEl(element);
. Therefore the pointerelement->next
will be indeterminate and comparison againstNULL
will not be useful. You dereference this pointer later inprint_listEl
causing undefined behavior. Always initialize aftermalloc
:
ListEl* element = malloc(sizeof(ListEl));
element->next = NULL;
memset(element->name, '', sizeof(element->name));
edited Jan 1 at 20:07
answered Jan 1 at 20:01
user10605163user10605163
2,868624
2,868624
add a comment |
add a comment |
may be
ListEl* current = malloc(sizeof(ListEl));
current = head;
must be
ListEl* current = malloc(sizeof(ListEl));
head = current;
else why to do the malloc and lost it immediately ?
I hope you have additional code before
if (current->next==NULL)
because current is only allocated, not initialized
When I watch the "current->next" variable, the debugger says "Cannot access memory at address".
That means current was broken because for instance it was deleted, or you just never initialized the field next. the problem is you do not give Minimal, Complete, and Verifiable example
If you code is really that :
ListEl* element = malloc(sizeof(ListEl));
print_listEl(element);
element->name nor element->next are not initialized, so in print_listEl
it is the same
Well, that doesn't really solve my problem. I want current=head, not head=current so I should've just skipped using malloc.
– Mihu
Jan 1 at 19:15
2
@Mihu I edited my answer, the problem is you do not give us enough source code, we don't see how you initialize your cells
– bruno
Jan 1 at 19:22
True, the source is incomplete. @Mihu you need to usemalloc
or an equivalent to allocate dynamic memory, so you can't "just skipped using malloc" or you have no way to initialize an arbitrary amount of elements for your list. Reread this answer and try to understand it before you say it doesn't fix your problem.
– Dan Farrell
Jan 1 at 19:23
I've added an edit which might make the code more understandable. What I get from your answer, I cannot check if member "next" of a list is equal to NULL if I don't alloc "element.next"? How would I check if a list is empty then?
– Mihu
Jan 1 at 19:44
By "skipping malloc" I mean that it's unnecesary here, as I've allocated memory for "head" previously, so I guess i don't need to allocate it to "current" just to overwrite it with "head" in the next line.
– Mihu
Jan 1 at 19:47
|
show 3 more comments
may be
ListEl* current = malloc(sizeof(ListEl));
current = head;
must be
ListEl* current = malloc(sizeof(ListEl));
head = current;
else why to do the malloc and lost it immediately ?
I hope you have additional code before
if (current->next==NULL)
because current is only allocated, not initialized
When I watch the "current->next" variable, the debugger says "Cannot access memory at address".
That means current was broken because for instance it was deleted, or you just never initialized the field next. the problem is you do not give Minimal, Complete, and Verifiable example
If you code is really that :
ListEl* element = malloc(sizeof(ListEl));
print_listEl(element);
element->name nor element->next are not initialized, so in print_listEl
it is the same
Well, that doesn't really solve my problem. I want current=head, not head=current so I should've just skipped using malloc.
– Mihu
Jan 1 at 19:15
2
@Mihu I edited my answer, the problem is you do not give us enough source code, we don't see how you initialize your cells
– bruno
Jan 1 at 19:22
True, the source is incomplete. @Mihu you need to usemalloc
or an equivalent to allocate dynamic memory, so you can't "just skipped using malloc" or you have no way to initialize an arbitrary amount of elements for your list. Reread this answer and try to understand it before you say it doesn't fix your problem.
– Dan Farrell
Jan 1 at 19:23
I've added an edit which might make the code more understandable. What I get from your answer, I cannot check if member "next" of a list is equal to NULL if I don't alloc "element.next"? How would I check if a list is empty then?
– Mihu
Jan 1 at 19:44
By "skipping malloc" I mean that it's unnecesary here, as I've allocated memory for "head" previously, so I guess i don't need to allocate it to "current" just to overwrite it with "head" in the next line.
– Mihu
Jan 1 at 19:47
|
show 3 more comments
may be
ListEl* current = malloc(sizeof(ListEl));
current = head;
must be
ListEl* current = malloc(sizeof(ListEl));
head = current;
else why to do the malloc and lost it immediately ?
I hope you have additional code before
if (current->next==NULL)
because current is only allocated, not initialized
When I watch the "current->next" variable, the debugger says "Cannot access memory at address".
That means current was broken because for instance it was deleted, or you just never initialized the field next. the problem is you do not give Minimal, Complete, and Verifiable example
If you code is really that :
ListEl* element = malloc(sizeof(ListEl));
print_listEl(element);
element->name nor element->next are not initialized, so in print_listEl
it is the same
may be
ListEl* current = malloc(sizeof(ListEl));
current = head;
must be
ListEl* current = malloc(sizeof(ListEl));
head = current;
else why to do the malloc and lost it immediately ?
I hope you have additional code before
if (current->next==NULL)
because current is only allocated, not initialized
When I watch the "current->next" variable, the debugger says "Cannot access memory at address".
That means current was broken because for instance it was deleted, or you just never initialized the field next. the problem is you do not give Minimal, Complete, and Verifiable example
If you code is really that :
ListEl* element = malloc(sizeof(ListEl));
print_listEl(element);
element->name nor element->next are not initialized, so in print_listEl
it is the same
edited Jan 1 at 20:05
answered Jan 1 at 19:13


brunobruno
10.4k21126
10.4k21126
Well, that doesn't really solve my problem. I want current=head, not head=current so I should've just skipped using malloc.
– Mihu
Jan 1 at 19:15
2
@Mihu I edited my answer, the problem is you do not give us enough source code, we don't see how you initialize your cells
– bruno
Jan 1 at 19:22
True, the source is incomplete. @Mihu you need to usemalloc
or an equivalent to allocate dynamic memory, so you can't "just skipped using malloc" or you have no way to initialize an arbitrary amount of elements for your list. Reread this answer and try to understand it before you say it doesn't fix your problem.
– Dan Farrell
Jan 1 at 19:23
I've added an edit which might make the code more understandable. What I get from your answer, I cannot check if member "next" of a list is equal to NULL if I don't alloc "element.next"? How would I check if a list is empty then?
– Mihu
Jan 1 at 19:44
By "skipping malloc" I mean that it's unnecesary here, as I've allocated memory for "head" previously, so I guess i don't need to allocate it to "current" just to overwrite it with "head" in the next line.
– Mihu
Jan 1 at 19:47
|
show 3 more comments
Well, that doesn't really solve my problem. I want current=head, not head=current so I should've just skipped using malloc.
– Mihu
Jan 1 at 19:15
2
@Mihu I edited my answer, the problem is you do not give us enough source code, we don't see how you initialize your cells
– bruno
Jan 1 at 19:22
True, the source is incomplete. @Mihu you need to usemalloc
or an equivalent to allocate dynamic memory, so you can't "just skipped using malloc" or you have no way to initialize an arbitrary amount of elements for your list. Reread this answer and try to understand it before you say it doesn't fix your problem.
– Dan Farrell
Jan 1 at 19:23
I've added an edit which might make the code more understandable. What I get from your answer, I cannot check if member "next" of a list is equal to NULL if I don't alloc "element.next"? How would I check if a list is empty then?
– Mihu
Jan 1 at 19:44
By "skipping malloc" I mean that it's unnecesary here, as I've allocated memory for "head" previously, so I guess i don't need to allocate it to "current" just to overwrite it with "head" in the next line.
– Mihu
Jan 1 at 19:47
Well, that doesn't really solve my problem. I want current=head, not head=current so I should've just skipped using malloc.
– Mihu
Jan 1 at 19:15
Well, that doesn't really solve my problem. I want current=head, not head=current so I should've just skipped using malloc.
– Mihu
Jan 1 at 19:15
2
2
@Mihu I edited my answer, the problem is you do not give us enough source code, we don't see how you initialize your cells
– bruno
Jan 1 at 19:22
@Mihu I edited my answer, the problem is you do not give us enough source code, we don't see how you initialize your cells
– bruno
Jan 1 at 19:22
True, the source is incomplete. @Mihu you need to use
malloc
or an equivalent to allocate dynamic memory, so you can't "just skipped using malloc" or you have no way to initialize an arbitrary amount of elements for your list. Reread this answer and try to understand it before you say it doesn't fix your problem.– Dan Farrell
Jan 1 at 19:23
True, the source is incomplete. @Mihu you need to use
malloc
or an equivalent to allocate dynamic memory, so you can't "just skipped using malloc" or you have no way to initialize an arbitrary amount of elements for your list. Reread this answer and try to understand it before you say it doesn't fix your problem.– Dan Farrell
Jan 1 at 19:23
I've added an edit which might make the code more understandable. What I get from your answer, I cannot check if member "next" of a list is equal to NULL if I don't alloc "element.next"? How would I check if a list is empty then?
– Mihu
Jan 1 at 19:44
I've added an edit which might make the code more understandable. What I get from your answer, I cannot check if member "next" of a list is equal to NULL if I don't alloc "element.next"? How would I check if a list is empty then?
– Mihu
Jan 1 at 19:44
By "skipping malloc" I mean that it's unnecesary here, as I've allocated memory for "head" previously, so I guess i don't need to allocate it to "current" just to overwrite it with "head" in the next line.
– Mihu
Jan 1 at 19:47
By "skipping malloc" I mean that it's unnecesary here, as I've allocated memory for "head" previously, so I guess i don't need to allocate it to "current" just to overwrite it with "head" in the next line.
– Mihu
Jan 1 at 19:47
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998192%2fwhy-wont-this-c-program-let-me-access-memory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please provide an Minimal, Complete, and Verifiable example. This code doesn’t show cleary what is
head
, you talk about passing arguments to a function but there’s no function here etc. We need a complete code to know what’s happening.– Sami Kuhmonen
Jan 1 at 19:23
You should update the code to show what the function parameters are. Clearly you are using
malloc
andfree
but then comment elsewhere that you didn't mean to use it in that function. Ishead
passed into the function. Alsomalloc
doesn't clear the data it points to. In this caseelement
still needs to be initialized so thatname
andnext
have meaningful values.– Fred
Jan 1 at 19:24
@user10605163 thank you, however after changing the code so that I don't use malloc or free, the function still seems to cause a problem in the debugger (and crashes the program) EVEN if I allocate memory to member "next" of the list element passed to the function.
– Mihu
Jan 1 at 20:00
@Mihu Yes the reason is missing initialization as pointed out by bruno. I have added my comment together with the initialization problem as answer.
– user10605163
Jan 1 at 20:14