How can I know why a program causes a coredump sigfault?How can I get GDB to tell me what address caused a segfault?What does this mean in gdb?Should segmentation fault handlers be written at all by a application programmer in C?strcat simulation in cPipe to and from child process is not workingSIGSEGV Segmentation fault, different messagewrite to shared memory segmentation faultSegmentation fault for simple memory allocationIncreasing number of rows in 2D c-arraySignal 11 (Coredump)

What secular civic space would pioneers build for small frontier towns?

String whitespaces

Medic abilities

Beyond Futuristic Technology for an Alien Warship?

"I will not" or "I don't" in the following context?

How to stop the death waves in my city?

One-digit products in a row of numbers

What is the difference between an astronaut in the ISS and a freediver in perfect neutral buoyancy?

Top off gas with old oil, is that bad?

Youtube not blocked by iptables

What happens to a net with the Returning Weapon artificer infusion after it hits?

Which lens has the same capability of lens mounted in Nikon P1000?

Another student has been assigned the same MSc thesis as mine (and already defended)

Align all symbols in a LaTeX equation

Number of list elements less than a given integer

Why is STARTTLS still used?

Neural Network vs regression

Is the iPhone's eSim for the home or roaming carrier?

How should I answer custom and border protection questions if I'm a returning citizen that hasn't been in the country for almost a decade?

Windows 10 deletes lots of tiny files super slowly. Anything that can be done to speed it up?

Would an object shot from earth fall into the sun?

What does it mean by "my days-of-the-week underwear only go to Thursday" in this context?

Character Transformation

Can my former employer sue me if I don't give them the photos I took (taking pictures was not part of my job description)?



How can I know why a program causes a coredump sigfault?


How can I get GDB to tell me what address caused a segfault?What does this mean in gdb?Should segmentation fault handlers be written at all by a application programmer in C?strcat simulation in cPipe to and from child process is not workingSIGSEGV Segmentation fault, different messagewrite to shared memory segmentation faultSegmentation fault for simple memory allocationIncreasing number of rows in 2D c-arraySignal 11 (Coredump)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















I have those functions and I would like to know if anyone can help me. I have to investigate why they cause a "segfault", and why it happens faster or slower depending on its conditions.
I supposed that in Rec1, it's caused by an infinite loop that collapses the memory of the memory stack. In rec2 I suppose it's caused faster because of the same condition that in Rec1 but adding that it is allocating memory everytime for the pointer too.
In Rec3 () that crashes instantly because it's allocating the same memory spot in the second iteration and causes a problem because the program is trying to access the same allocated memory.
In Rec4 () I think it's caused because it creates an array with infinite positions, ask is the limiting of the array max space.
Can you give me some advices on those suppositions?



#include <stdio.h>
#include <stdlib.h>

#define MOD 10000

int k = 0;

char global[100];

void
panic (char *m)

fprintf (stderr, "%sn", m);
exit (0);


void
rec1 ()

k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);
rec1 ();


void
rec2 ()

int *tmp;

tmp = malloc (100 * sizeof (int));
if (tmp == NULL)
exit (0);

k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);
rec2 ();


void
rec3 ()

int tmp[100];

k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);
rec3 ();


void
rec4 ()

global[k] = k;
if (k % 200 == 0)
fprintf (stderr, "%d ", k);
k++;
rec4 ();


int
main (int argc, char *argv[])

int mode = 1;
if (argc > 1)
mode = atoi (argv[1]);

printf ("Testing rec%d...n", mode);
switch (mode)

case 1:
rec1 ();
break;
case 2:
rec2 ();
break;
case 3:
rec3 ();
break;
case 4:
rec4 ();
break;
default:
panic ("Wrong mode");


return 0;



This is the output when I run the compiled C program in terminal.



Testing rec1...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554904 in rec1 () at stack.c:24
24 rec1 ();

Testing rec2...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a7b96a in __GI___libc_free (mem=0x555555757670) at malloc.c:3086
3086 malloc.c: No existe el archivo o el directorio.

Testing rec3...
1
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554a43 in rec3 () at stack.c:53
53 rec3 ();

Testing rec4...
0 200 400 600 800 1000 1200 1400 1600 1800 2000 2200 2400 2600 2800 3000 3200 3400 3600 3800 4000 Violación de segmento (`core' generado)
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554a1f in rec4 ()









share|improve this question


























  • I don't see a main() to know how rec1() or rec2() are called. I don't know what k is. I do see recursive functions that are probably blowing your stack. Generally, when you crash, you open it the program in a debugger and examine the final state of the program. This, with practice, tells you why a program crashed.

    – Michael Dorgan
    Mar 28 at 18:50












  • they crash because you didn't compile optimization enabled. Both are optimizable to tail calls that don't leak memory

    – Antti Haapala
    Mar 28 at 18:51






  • 1





    Do you know what the name of this site means?

    – Eugene Sh.
    Mar 28 at 18:53






  • 2





    @AnttiHaapala - Any program that requires optimization to work scare the hell out of me :)

    – Michael Dorgan
    Mar 28 at 18:54











  • @MichaelDorgan Actually it is funny. This code does not contain UB per se as far as I can tell, but still is depending on the optimization. Maybe I am missing something though..

    – Eugene Sh.
    Mar 28 at 18:56


















-1















I have those functions and I would like to know if anyone can help me. I have to investigate why they cause a "segfault", and why it happens faster or slower depending on its conditions.
I supposed that in Rec1, it's caused by an infinite loop that collapses the memory of the memory stack. In rec2 I suppose it's caused faster because of the same condition that in Rec1 but adding that it is allocating memory everytime for the pointer too.
In Rec3 () that crashes instantly because it's allocating the same memory spot in the second iteration and causes a problem because the program is trying to access the same allocated memory.
In Rec4 () I think it's caused because it creates an array with infinite positions, ask is the limiting of the array max space.
Can you give me some advices on those suppositions?



#include <stdio.h>
#include <stdlib.h>

#define MOD 10000

int k = 0;

char global[100];

void
panic (char *m)

fprintf (stderr, "%sn", m);
exit (0);


void
rec1 ()

k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);
rec1 ();


void
rec2 ()

int *tmp;

tmp = malloc (100 * sizeof (int));
if (tmp == NULL)
exit (0);

k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);
rec2 ();


void
rec3 ()

int tmp[100];

k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);
rec3 ();


void
rec4 ()

global[k] = k;
if (k % 200 == 0)
fprintf (stderr, "%d ", k);
k++;
rec4 ();


int
main (int argc, char *argv[])

int mode = 1;
if (argc > 1)
mode = atoi (argv[1]);

printf ("Testing rec%d...n", mode);
switch (mode)

case 1:
rec1 ();
break;
case 2:
rec2 ();
break;
case 3:
rec3 ();
break;
case 4:
rec4 ();
break;
default:
panic ("Wrong mode");


return 0;



This is the output when I run the compiled C program in terminal.



Testing rec1...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554904 in rec1 () at stack.c:24
24 rec1 ();

Testing rec2...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a7b96a in __GI___libc_free (mem=0x555555757670) at malloc.c:3086
3086 malloc.c: No existe el archivo o el directorio.

Testing rec3...
1
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554a43 in rec3 () at stack.c:53
53 rec3 ();

Testing rec4...
0 200 400 600 800 1000 1200 1400 1600 1800 2000 2200 2400 2600 2800 3000 3200 3400 3600 3800 4000 Violación de segmento (`core' generado)
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554a1f in rec4 ()









share|improve this question


























  • I don't see a main() to know how rec1() or rec2() are called. I don't know what k is. I do see recursive functions that are probably blowing your stack. Generally, when you crash, you open it the program in a debugger and examine the final state of the program. This, with practice, tells you why a program crashed.

    – Michael Dorgan
    Mar 28 at 18:50












  • they crash because you didn't compile optimization enabled. Both are optimizable to tail calls that don't leak memory

    – Antti Haapala
    Mar 28 at 18:51






  • 1





    Do you know what the name of this site means?

    – Eugene Sh.
    Mar 28 at 18:53






  • 2





    @AnttiHaapala - Any program that requires optimization to work scare the hell out of me :)

    – Michael Dorgan
    Mar 28 at 18:54











  • @MichaelDorgan Actually it is funny. This code does not contain UB per se as far as I can tell, but still is depending on the optimization. Maybe I am missing something though..

    – Eugene Sh.
    Mar 28 at 18:56














-1












-1








-1








I have those functions and I would like to know if anyone can help me. I have to investigate why they cause a "segfault", and why it happens faster or slower depending on its conditions.
I supposed that in Rec1, it's caused by an infinite loop that collapses the memory of the memory stack. In rec2 I suppose it's caused faster because of the same condition that in Rec1 but adding that it is allocating memory everytime for the pointer too.
In Rec3 () that crashes instantly because it's allocating the same memory spot in the second iteration and causes a problem because the program is trying to access the same allocated memory.
In Rec4 () I think it's caused because it creates an array with infinite positions, ask is the limiting of the array max space.
Can you give me some advices on those suppositions?



#include <stdio.h>
#include <stdlib.h>

#define MOD 10000

int k = 0;

char global[100];

void
panic (char *m)

fprintf (stderr, "%sn", m);
exit (0);


void
rec1 ()

k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);
rec1 ();


void
rec2 ()

int *tmp;

tmp = malloc (100 * sizeof (int));
if (tmp == NULL)
exit (0);

k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);
rec2 ();


void
rec3 ()

int tmp[100];

k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);
rec3 ();


void
rec4 ()

global[k] = k;
if (k % 200 == 0)
fprintf (stderr, "%d ", k);
k++;
rec4 ();


int
main (int argc, char *argv[])

int mode = 1;
if (argc > 1)
mode = atoi (argv[1]);

printf ("Testing rec%d...n", mode);
switch (mode)

case 1:
rec1 ();
break;
case 2:
rec2 ();
break;
case 3:
rec3 ();
break;
case 4:
rec4 ();
break;
default:
panic ("Wrong mode");


return 0;



This is the output when I run the compiled C program in terminal.



Testing rec1...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554904 in rec1 () at stack.c:24
24 rec1 ();

Testing rec2...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a7b96a in __GI___libc_free (mem=0x555555757670) at malloc.c:3086
3086 malloc.c: No existe el archivo o el directorio.

Testing rec3...
1
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554a43 in rec3 () at stack.c:53
53 rec3 ();

Testing rec4...
0 200 400 600 800 1000 1200 1400 1600 1800 2000 2200 2400 2600 2800 3000 3200 3400 3600 3800 4000 Violación de segmento (`core' generado)
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554a1f in rec4 ()









share|improve this question
















I have those functions and I would like to know if anyone can help me. I have to investigate why they cause a "segfault", and why it happens faster or slower depending on its conditions.
I supposed that in Rec1, it's caused by an infinite loop that collapses the memory of the memory stack. In rec2 I suppose it's caused faster because of the same condition that in Rec1 but adding that it is allocating memory everytime for the pointer too.
In Rec3 () that crashes instantly because it's allocating the same memory spot in the second iteration and causes a problem because the program is trying to access the same allocated memory.
In Rec4 () I think it's caused because it creates an array with infinite positions, ask is the limiting of the array max space.
Can you give me some advices on those suppositions?



#include <stdio.h>
#include <stdlib.h>

#define MOD 10000

int k = 0;

char global[100];

void
panic (char *m)

fprintf (stderr, "%sn", m);
exit (0);


void
rec1 ()

k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);
rec1 ();


void
rec2 ()

int *tmp;

tmp = malloc (100 * sizeof (int));
if (tmp == NULL)
exit (0);

k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);
rec2 ();


void
rec3 ()

int tmp[100];

k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);
rec3 ();


void
rec4 ()

global[k] = k;
if (k % 200 == 0)
fprintf (stderr, "%d ", k);
k++;
rec4 ();


int
main (int argc, char *argv[])

int mode = 1;
if (argc > 1)
mode = atoi (argv[1]);

printf ("Testing rec%d...n", mode);
switch (mode)

case 1:
rec1 ();
break;
case 2:
rec2 ();
break;
case 3:
rec3 ();
break;
case 4:
rec4 ();
break;
default:
panic ("Wrong mode");


return 0;



This is the output when I run the compiled C program in terminal.



Testing rec1...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554904 in rec1 () at stack.c:24
24 rec1 ();

Testing rec2...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a7b96a in __GI___libc_free (mem=0x555555757670) at malloc.c:3086
3086 malloc.c: No existe el archivo o el directorio.

Testing rec3...
1
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554a43 in rec3 () at stack.c:53
53 rec3 ();

Testing rec4...
0 200 400 600 800 1000 1200 1400 1600 1800 2000 2200 2400 2600 2800 3000 3200 3400 3600 3800 4000 Violación de segmento (`core' generado)
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554a1f in rec4 ()






c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 29 at 10:34







Stryivix

















asked Mar 28 at 18:35









StryivixStryivix

12 bronze badges




12 bronze badges















  • I don't see a main() to know how rec1() or rec2() are called. I don't know what k is. I do see recursive functions that are probably blowing your stack. Generally, when you crash, you open it the program in a debugger and examine the final state of the program. This, with practice, tells you why a program crashed.

    – Michael Dorgan
    Mar 28 at 18:50












  • they crash because you didn't compile optimization enabled. Both are optimizable to tail calls that don't leak memory

    – Antti Haapala
    Mar 28 at 18:51






  • 1





    Do you know what the name of this site means?

    – Eugene Sh.
    Mar 28 at 18:53






  • 2





    @AnttiHaapala - Any program that requires optimization to work scare the hell out of me :)

    – Michael Dorgan
    Mar 28 at 18:54











  • @MichaelDorgan Actually it is funny. This code does not contain UB per se as far as I can tell, but still is depending on the optimization. Maybe I am missing something though..

    – Eugene Sh.
    Mar 28 at 18:56


















  • I don't see a main() to know how rec1() or rec2() are called. I don't know what k is. I do see recursive functions that are probably blowing your stack. Generally, when you crash, you open it the program in a debugger and examine the final state of the program. This, with practice, tells you why a program crashed.

    – Michael Dorgan
    Mar 28 at 18:50












  • they crash because you didn't compile optimization enabled. Both are optimizable to tail calls that don't leak memory

    – Antti Haapala
    Mar 28 at 18:51






  • 1





    Do you know what the name of this site means?

    – Eugene Sh.
    Mar 28 at 18:53






  • 2





    @AnttiHaapala - Any program that requires optimization to work scare the hell out of me :)

    – Michael Dorgan
    Mar 28 at 18:54











  • @MichaelDorgan Actually it is funny. This code does not contain UB per se as far as I can tell, but still is depending on the optimization. Maybe I am missing something though..

    – Eugene Sh.
    Mar 28 at 18:56

















I don't see a main() to know how rec1() or rec2() are called. I don't know what k is. I do see recursive functions that are probably blowing your stack. Generally, when you crash, you open it the program in a debugger and examine the final state of the program. This, with practice, tells you why a program crashed.

– Michael Dorgan
Mar 28 at 18:50






I don't see a main() to know how rec1() or rec2() are called. I don't know what k is. I do see recursive functions that are probably blowing your stack. Generally, when you crash, you open it the program in a debugger and examine the final state of the program. This, with practice, tells you why a program crashed.

– Michael Dorgan
Mar 28 at 18:50














they crash because you didn't compile optimization enabled. Both are optimizable to tail calls that don't leak memory

– Antti Haapala
Mar 28 at 18:51





they crash because you didn't compile optimization enabled. Both are optimizable to tail calls that don't leak memory

– Antti Haapala
Mar 28 at 18:51




1




1





Do you know what the name of this site means?

– Eugene Sh.
Mar 28 at 18:53





Do you know what the name of this site means?

– Eugene Sh.
Mar 28 at 18:53




2




2





@AnttiHaapala - Any program that requires optimization to work scare the hell out of me :)

– Michael Dorgan
Mar 28 at 18:54





@AnttiHaapala - Any program that requires optimization to work scare the hell out of me :)

– Michael Dorgan
Mar 28 at 18:54













@MichaelDorgan Actually it is funny. This code does not contain UB per se as far as I can tell, but still is depending on the optimization. Maybe I am missing something though..

– Eugene Sh.
Mar 28 at 18:56






@MichaelDorgan Actually it is funny. This code does not contain UB per se as far as I can tell, but still is depending on the optimization. Maybe I am missing something though..

– Eugene Sh.
Mar 28 at 18:56













1 Answer
1






active

oldest

votes


















2
















The Code that you have is very likely to trigger an error in my experience. Without any compiler or program feedback, it's a little difficult to discern exactly what went wrong, but I believe you may be looking (Generally) for information regarding stacks, heaps, and recursion.



Firstly, please note that



void rec1 () 
k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);

rec1 ();



is NOT "Iteration". Iteration refers to the repetition of a sequential portion of code (usually a for or while loop). What you have here is recursion. Recursion creates a new instance of the method to operate from, along with a stack pointer to navigate to the last execution point (As well as store any immediately relevant variables). This occurs every time you call the rec1 () function from your rec1 () function Eventually, you'll run out of space on the stack to store those pointers. The number of pointers you can store on a stack is usually quite large on modern computers, but considering that you have no return statement, you will run into the maximum capacity eventually.



EDIT



This post has been edited to reflect the new material presented by the question.



Okay...From the material you presented, it looks like you're essentially being asked about WHERE each rec stores and processes information...



In the case of Rec1, it does indeed appear to be a simple case of stack overflow. The pointer to the last execution point of the previous Rec1 is stored on the stack, ultimately resulting in the program's crash after about 520,000 instances. Given that each pointer is 4 bytes, that's around 2 MB of just recursive pointer information alone stored on your stack before it collapses and triggers a Seg Fault due to stack overflow.



The second case is a little trickier. Note that your program indicates that it makes it to roughly 260,000 recursions before it triggers a Seg Fault. This is exactly half of Rec1. HOWEVER, this is not necessarily a stack overflow per se. Rec2 allocates 400 bytes of data on the heap per recursion. The pointer to the heap is stored on the stack, meaning that 8 bytes are stored on the stack per recursion (which may be related to why its exactly half, but could also be explained by the ratio of your stack / heap size). Now, the error for Rec2 states that malloc could not find the file or directory, which seems to me as though malloc could not complete correctly. This may actually indicate that the max heap size has been hit.



Rec3 is pretty straightforward. The entire integer array tmp is stored on the stack for each recursion. that's 4 bytes per integer times 100 ints, which is 400 bytes on the stack PER recursion. This is no surprise that it crashes between 10,000 to 20,000 recursions. There just wasn't enough space to store the data on the stack. NOTE: In relation to something you mentioned in your question, this tmp array does not attempt to allocate the same region of memory. Due to the fact that this is recursively built, it creates a new space on the stack for that function instance.



Rec4 is a simple case of buffer overflow. After overwriting the first 100 bytes of memory allocated in global[100], it was only a matter of time before k++ would cause global[k] to point to an address space restricted to the process. This triggered the seg fault after about 4000 recursions (k was not mod 10,000 in rec4).



I hope this clarification helps.






share|improve this answer



























  • It is not at all guaranteed to trigger error. There is nothing in the C standard that says "infinite recursion must crash your program". In fact, even rec2 compiled with Clang and -O3 neither leaks memory nor does crash

    – Antti Haapala
    Mar 28 at 19:43












  • Ok, now it is better ;)

    – Antti Haapala
    Mar 28 at 19:47











  • I should have added more information @armitus. The leak is caused in a Virtual ubuntu machine compiling with gcc and runing with gbd. Anyway, this is the output

    – Stryivix
    Mar 29 at 9:54












  • Hopefully this new edit proves helpful @Stryivix

    – armitus
    Mar 29 at 15:43











  • @armitus Thankyou for this information. I've been "playing" with the stack size in ubuntu to see if it would affect the execution of those programs and indeed it does.

    – Stryivix
    Mar 29 at 21:40













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/4.0/"u003ecc by-sa 4.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
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55404675%2fhow-can-i-know-why-a-program-causes-a-coredump-sigfault%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2
















The Code that you have is very likely to trigger an error in my experience. Without any compiler or program feedback, it's a little difficult to discern exactly what went wrong, but I believe you may be looking (Generally) for information regarding stacks, heaps, and recursion.



Firstly, please note that



void rec1 () 
k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);

rec1 ();



is NOT "Iteration". Iteration refers to the repetition of a sequential portion of code (usually a for or while loop). What you have here is recursion. Recursion creates a new instance of the method to operate from, along with a stack pointer to navigate to the last execution point (As well as store any immediately relevant variables). This occurs every time you call the rec1 () function from your rec1 () function Eventually, you'll run out of space on the stack to store those pointers. The number of pointers you can store on a stack is usually quite large on modern computers, but considering that you have no return statement, you will run into the maximum capacity eventually.



EDIT



This post has been edited to reflect the new material presented by the question.



Okay...From the material you presented, it looks like you're essentially being asked about WHERE each rec stores and processes information...



In the case of Rec1, it does indeed appear to be a simple case of stack overflow. The pointer to the last execution point of the previous Rec1 is stored on the stack, ultimately resulting in the program's crash after about 520,000 instances. Given that each pointer is 4 bytes, that's around 2 MB of just recursive pointer information alone stored on your stack before it collapses and triggers a Seg Fault due to stack overflow.



The second case is a little trickier. Note that your program indicates that it makes it to roughly 260,000 recursions before it triggers a Seg Fault. This is exactly half of Rec1. HOWEVER, this is not necessarily a stack overflow per se. Rec2 allocates 400 bytes of data on the heap per recursion. The pointer to the heap is stored on the stack, meaning that 8 bytes are stored on the stack per recursion (which may be related to why its exactly half, but could also be explained by the ratio of your stack / heap size). Now, the error for Rec2 states that malloc could not find the file or directory, which seems to me as though malloc could not complete correctly. This may actually indicate that the max heap size has been hit.



Rec3 is pretty straightforward. The entire integer array tmp is stored on the stack for each recursion. that's 4 bytes per integer times 100 ints, which is 400 bytes on the stack PER recursion. This is no surprise that it crashes between 10,000 to 20,000 recursions. There just wasn't enough space to store the data on the stack. NOTE: In relation to something you mentioned in your question, this tmp array does not attempt to allocate the same region of memory. Due to the fact that this is recursively built, it creates a new space on the stack for that function instance.



Rec4 is a simple case of buffer overflow. After overwriting the first 100 bytes of memory allocated in global[100], it was only a matter of time before k++ would cause global[k] to point to an address space restricted to the process. This triggered the seg fault after about 4000 recursions (k was not mod 10,000 in rec4).



I hope this clarification helps.






share|improve this answer



























  • It is not at all guaranteed to trigger error. There is nothing in the C standard that says "infinite recursion must crash your program". In fact, even rec2 compiled with Clang and -O3 neither leaks memory nor does crash

    – Antti Haapala
    Mar 28 at 19:43












  • Ok, now it is better ;)

    – Antti Haapala
    Mar 28 at 19:47











  • I should have added more information @armitus. The leak is caused in a Virtual ubuntu machine compiling with gcc and runing with gbd. Anyway, this is the output

    – Stryivix
    Mar 29 at 9:54












  • Hopefully this new edit proves helpful @Stryivix

    – armitus
    Mar 29 at 15:43











  • @armitus Thankyou for this information. I've been "playing" with the stack size in ubuntu to see if it would affect the execution of those programs and indeed it does.

    – Stryivix
    Mar 29 at 21:40















2
















The Code that you have is very likely to trigger an error in my experience. Without any compiler or program feedback, it's a little difficult to discern exactly what went wrong, but I believe you may be looking (Generally) for information regarding stacks, heaps, and recursion.



Firstly, please note that



void rec1 () 
k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);

rec1 ();



is NOT "Iteration". Iteration refers to the repetition of a sequential portion of code (usually a for or while loop). What you have here is recursion. Recursion creates a new instance of the method to operate from, along with a stack pointer to navigate to the last execution point (As well as store any immediately relevant variables). This occurs every time you call the rec1 () function from your rec1 () function Eventually, you'll run out of space on the stack to store those pointers. The number of pointers you can store on a stack is usually quite large on modern computers, but considering that you have no return statement, you will run into the maximum capacity eventually.



EDIT



This post has been edited to reflect the new material presented by the question.



Okay...From the material you presented, it looks like you're essentially being asked about WHERE each rec stores and processes information...



In the case of Rec1, it does indeed appear to be a simple case of stack overflow. The pointer to the last execution point of the previous Rec1 is stored on the stack, ultimately resulting in the program's crash after about 520,000 instances. Given that each pointer is 4 bytes, that's around 2 MB of just recursive pointer information alone stored on your stack before it collapses and triggers a Seg Fault due to stack overflow.



The second case is a little trickier. Note that your program indicates that it makes it to roughly 260,000 recursions before it triggers a Seg Fault. This is exactly half of Rec1. HOWEVER, this is not necessarily a stack overflow per se. Rec2 allocates 400 bytes of data on the heap per recursion. The pointer to the heap is stored on the stack, meaning that 8 bytes are stored on the stack per recursion (which may be related to why its exactly half, but could also be explained by the ratio of your stack / heap size). Now, the error for Rec2 states that malloc could not find the file or directory, which seems to me as though malloc could not complete correctly. This may actually indicate that the max heap size has been hit.



Rec3 is pretty straightforward. The entire integer array tmp is stored on the stack for each recursion. that's 4 bytes per integer times 100 ints, which is 400 bytes on the stack PER recursion. This is no surprise that it crashes between 10,000 to 20,000 recursions. There just wasn't enough space to store the data on the stack. NOTE: In relation to something you mentioned in your question, this tmp array does not attempt to allocate the same region of memory. Due to the fact that this is recursively built, it creates a new space on the stack for that function instance.



Rec4 is a simple case of buffer overflow. After overwriting the first 100 bytes of memory allocated in global[100], it was only a matter of time before k++ would cause global[k] to point to an address space restricted to the process. This triggered the seg fault after about 4000 recursions (k was not mod 10,000 in rec4).



I hope this clarification helps.






share|improve this answer



























  • It is not at all guaranteed to trigger error. There is nothing in the C standard that says "infinite recursion must crash your program". In fact, even rec2 compiled with Clang and -O3 neither leaks memory nor does crash

    – Antti Haapala
    Mar 28 at 19:43












  • Ok, now it is better ;)

    – Antti Haapala
    Mar 28 at 19:47











  • I should have added more information @armitus. The leak is caused in a Virtual ubuntu machine compiling with gcc and runing with gbd. Anyway, this is the output

    – Stryivix
    Mar 29 at 9:54












  • Hopefully this new edit proves helpful @Stryivix

    – armitus
    Mar 29 at 15:43











  • @armitus Thankyou for this information. I've been "playing" with the stack size in ubuntu to see if it would affect the execution of those programs and indeed it does.

    – Stryivix
    Mar 29 at 21:40













2














2










2









The Code that you have is very likely to trigger an error in my experience. Without any compiler or program feedback, it's a little difficult to discern exactly what went wrong, but I believe you may be looking (Generally) for information regarding stacks, heaps, and recursion.



Firstly, please note that



void rec1 () 
k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);

rec1 ();



is NOT "Iteration". Iteration refers to the repetition of a sequential portion of code (usually a for or while loop). What you have here is recursion. Recursion creates a new instance of the method to operate from, along with a stack pointer to navigate to the last execution point (As well as store any immediately relevant variables). This occurs every time you call the rec1 () function from your rec1 () function Eventually, you'll run out of space on the stack to store those pointers. The number of pointers you can store on a stack is usually quite large on modern computers, but considering that you have no return statement, you will run into the maximum capacity eventually.



EDIT



This post has been edited to reflect the new material presented by the question.



Okay...From the material you presented, it looks like you're essentially being asked about WHERE each rec stores and processes information...



In the case of Rec1, it does indeed appear to be a simple case of stack overflow. The pointer to the last execution point of the previous Rec1 is stored on the stack, ultimately resulting in the program's crash after about 520,000 instances. Given that each pointer is 4 bytes, that's around 2 MB of just recursive pointer information alone stored on your stack before it collapses and triggers a Seg Fault due to stack overflow.



The second case is a little trickier. Note that your program indicates that it makes it to roughly 260,000 recursions before it triggers a Seg Fault. This is exactly half of Rec1. HOWEVER, this is not necessarily a stack overflow per se. Rec2 allocates 400 bytes of data on the heap per recursion. The pointer to the heap is stored on the stack, meaning that 8 bytes are stored on the stack per recursion (which may be related to why its exactly half, but could also be explained by the ratio of your stack / heap size). Now, the error for Rec2 states that malloc could not find the file or directory, which seems to me as though malloc could not complete correctly. This may actually indicate that the max heap size has been hit.



Rec3 is pretty straightforward. The entire integer array tmp is stored on the stack for each recursion. that's 4 bytes per integer times 100 ints, which is 400 bytes on the stack PER recursion. This is no surprise that it crashes between 10,000 to 20,000 recursions. There just wasn't enough space to store the data on the stack. NOTE: In relation to something you mentioned in your question, this tmp array does not attempt to allocate the same region of memory. Due to the fact that this is recursively built, it creates a new space on the stack for that function instance.



Rec4 is a simple case of buffer overflow. After overwriting the first 100 bytes of memory allocated in global[100], it was only a matter of time before k++ would cause global[k] to point to an address space restricted to the process. This triggered the seg fault after about 4000 recursions (k was not mod 10,000 in rec4).



I hope this clarification helps.






share|improve this answer















The Code that you have is very likely to trigger an error in my experience. Without any compiler or program feedback, it's a little difficult to discern exactly what went wrong, but I believe you may be looking (Generally) for information regarding stacks, heaps, and recursion.



Firstly, please note that



void rec1 () 
k++;
if (k % MOD == 0)
fprintf (stderr, "%d ", k / MOD);

rec1 ();



is NOT "Iteration". Iteration refers to the repetition of a sequential portion of code (usually a for or while loop). What you have here is recursion. Recursion creates a new instance of the method to operate from, along with a stack pointer to navigate to the last execution point (As well as store any immediately relevant variables). This occurs every time you call the rec1 () function from your rec1 () function Eventually, you'll run out of space on the stack to store those pointers. The number of pointers you can store on a stack is usually quite large on modern computers, but considering that you have no return statement, you will run into the maximum capacity eventually.



EDIT



This post has been edited to reflect the new material presented by the question.



Okay...From the material you presented, it looks like you're essentially being asked about WHERE each rec stores and processes information...



In the case of Rec1, it does indeed appear to be a simple case of stack overflow. The pointer to the last execution point of the previous Rec1 is stored on the stack, ultimately resulting in the program's crash after about 520,000 instances. Given that each pointer is 4 bytes, that's around 2 MB of just recursive pointer information alone stored on your stack before it collapses and triggers a Seg Fault due to stack overflow.



The second case is a little trickier. Note that your program indicates that it makes it to roughly 260,000 recursions before it triggers a Seg Fault. This is exactly half of Rec1. HOWEVER, this is not necessarily a stack overflow per se. Rec2 allocates 400 bytes of data on the heap per recursion. The pointer to the heap is stored on the stack, meaning that 8 bytes are stored on the stack per recursion (which may be related to why its exactly half, but could also be explained by the ratio of your stack / heap size). Now, the error for Rec2 states that malloc could not find the file or directory, which seems to me as though malloc could not complete correctly. This may actually indicate that the max heap size has been hit.



Rec3 is pretty straightforward. The entire integer array tmp is stored on the stack for each recursion. that's 4 bytes per integer times 100 ints, which is 400 bytes on the stack PER recursion. This is no surprise that it crashes between 10,000 to 20,000 recursions. There just wasn't enough space to store the data on the stack. NOTE: In relation to something you mentioned in your question, this tmp array does not attempt to allocate the same region of memory. Due to the fact that this is recursively built, it creates a new space on the stack for that function instance.



Rec4 is a simple case of buffer overflow. After overwriting the first 100 bytes of memory allocated in global[100], it was only a matter of time before k++ would cause global[k] to point to an address space restricted to the process. This triggered the seg fault after about 4000 recursions (k was not mod 10,000 in rec4).



I hope this clarification helps.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 29 at 13:28

























answered Mar 28 at 19:01









armitusarmitus

5192 silver badges16 bronze badges




5192 silver badges16 bronze badges















  • It is not at all guaranteed to trigger error. There is nothing in the C standard that says "infinite recursion must crash your program". In fact, even rec2 compiled with Clang and -O3 neither leaks memory nor does crash

    – Antti Haapala
    Mar 28 at 19:43












  • Ok, now it is better ;)

    – Antti Haapala
    Mar 28 at 19:47











  • I should have added more information @armitus. The leak is caused in a Virtual ubuntu machine compiling with gcc and runing with gbd. Anyway, this is the output

    – Stryivix
    Mar 29 at 9:54












  • Hopefully this new edit proves helpful @Stryivix

    – armitus
    Mar 29 at 15:43











  • @armitus Thankyou for this information. I've been "playing" with the stack size in ubuntu to see if it would affect the execution of those programs and indeed it does.

    – Stryivix
    Mar 29 at 21:40

















  • It is not at all guaranteed to trigger error. There is nothing in the C standard that says "infinite recursion must crash your program". In fact, even rec2 compiled with Clang and -O3 neither leaks memory nor does crash

    – Antti Haapala
    Mar 28 at 19:43












  • Ok, now it is better ;)

    – Antti Haapala
    Mar 28 at 19:47











  • I should have added more information @armitus. The leak is caused in a Virtual ubuntu machine compiling with gcc and runing with gbd. Anyway, this is the output

    – Stryivix
    Mar 29 at 9:54












  • Hopefully this new edit proves helpful @Stryivix

    – armitus
    Mar 29 at 15:43











  • @armitus Thankyou for this information. I've been "playing" with the stack size in ubuntu to see if it would affect the execution of those programs and indeed it does.

    – Stryivix
    Mar 29 at 21:40
















It is not at all guaranteed to trigger error. There is nothing in the C standard that says "infinite recursion must crash your program". In fact, even rec2 compiled with Clang and -O3 neither leaks memory nor does crash

– Antti Haapala
Mar 28 at 19:43






It is not at all guaranteed to trigger error. There is nothing in the C standard that says "infinite recursion must crash your program". In fact, even rec2 compiled with Clang and -O3 neither leaks memory nor does crash

– Antti Haapala
Mar 28 at 19:43














Ok, now it is better ;)

– Antti Haapala
Mar 28 at 19:47





Ok, now it is better ;)

– Antti Haapala
Mar 28 at 19:47













I should have added more information @armitus. The leak is caused in a Virtual ubuntu machine compiling with gcc and runing with gbd. Anyway, this is the output

– Stryivix
Mar 29 at 9:54






I should have added more information @armitus. The leak is caused in a Virtual ubuntu machine compiling with gcc and runing with gbd. Anyway, this is the output

– Stryivix
Mar 29 at 9:54














Hopefully this new edit proves helpful @Stryivix

– armitus
Mar 29 at 15:43





Hopefully this new edit proves helpful @Stryivix

– armitus
Mar 29 at 15:43













@armitus Thankyou for this information. I've been "playing" with the stack size in ubuntu to see if it would affect the execution of those programs and indeed it does.

– Stryivix
Mar 29 at 21:40





@armitus Thankyou for this information. I've been "playing" with the stack size in ubuntu to see if it would affect the execution of those programs and indeed it does.

– Stryivix
Mar 29 at 21:40


















draft saved

draft discarded















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55404675%2fhow-can-i-know-why-a-program-causes-a-coredump-sigfault%23new-answer', 'question_page');

);

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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript