segmantation fault malloc pointers functions











up vote
1
down vote

favorite












hello guys this is my code :



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

int power(int a, int b) {
int exponent = b, result = 1;
while (exponent != 0) {
result = result * a;
exponent--;
}
//printf("%d",result);
return result;
}

int fill_it(char ** p, int N, int fliptimes, int column2) {
if (N < 0) return 0;
int counter = 0, l;
char a = 'H';
for (l = 0; l < power(2, fliptimes); l++) {
p[l][column2] = a;
counter++;
if (counter == (power(2, N) / 2)) {
counter = 0;
if (a == 'H') a = 'T';
if (a == 'T') a = 'H';
}
}
fill_it(p, N--, fliptimes, column2++);
}

int main() {
int i, fores, j, l, m;
char ** p;
printf("how many times did you toss the coin?:");
scanf("%d", & fores);
p = (char ** ) malloc((power(2, fores)) * sizeof(char * ));
for (i = 0; i < fores; i++)
p[i] = (char * ) malloc(fores * sizeof(char));
fill_it(p, fores, fores, 0);
for (l = 0; l < power(2, fores); l++) {
for (m = 0; m < fores; m++) {
printf("%c", p[l][m]);
}
}
printf(",");
}


it does compile.But when i run the program it returns a "segmantation fault (core dumped)" error



i know it means that i tried to access memory,i dont have acces to but i dont understand which part of the program is defective










share|improve this question




















  • 1




    (Unrelated to the seggie, but if (a == 'H') a = 'T'; if (a == 'T') a = 'H'; will make a == 'H' in both cases. You need an else here.)
    – M Oehm
    Nov 21 at 10:24






  • 1




    Run your code in a debugger (ie gdb) and it will tell you where in your code it is crashing
    – Chris Turner
    Nov 21 at 10:26






  • 2




    Valgrind is your friend when you have to find memory error, you should try it
    – Loufi
    Nov 21 at 10:34






  • 1




    (You call ´fill_it` recursively, but you have got a tail recursion here, which you can turn into a loop. With two nested loops, the function would be much clearer, in my opinion.)
    – M Oehm
    Nov 21 at 10:46






  • 1




    @MOehm there's also a similar problem with column2++ in the recursive call
    – Chris Turner
    Nov 21 at 10:46















up vote
1
down vote

favorite












hello guys this is my code :



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

int power(int a, int b) {
int exponent = b, result = 1;
while (exponent != 0) {
result = result * a;
exponent--;
}
//printf("%d",result);
return result;
}

int fill_it(char ** p, int N, int fliptimes, int column2) {
if (N < 0) return 0;
int counter = 0, l;
char a = 'H';
for (l = 0; l < power(2, fliptimes); l++) {
p[l][column2] = a;
counter++;
if (counter == (power(2, N) / 2)) {
counter = 0;
if (a == 'H') a = 'T';
if (a == 'T') a = 'H';
}
}
fill_it(p, N--, fliptimes, column2++);
}

int main() {
int i, fores, j, l, m;
char ** p;
printf("how many times did you toss the coin?:");
scanf("%d", & fores);
p = (char ** ) malloc((power(2, fores)) * sizeof(char * ));
for (i = 0; i < fores; i++)
p[i] = (char * ) malloc(fores * sizeof(char));
fill_it(p, fores, fores, 0);
for (l = 0; l < power(2, fores); l++) {
for (m = 0; m < fores; m++) {
printf("%c", p[l][m]);
}
}
printf(",");
}


it does compile.But when i run the program it returns a "segmantation fault (core dumped)" error



i know it means that i tried to access memory,i dont have acces to but i dont understand which part of the program is defective










share|improve this question




















  • 1




    (Unrelated to the seggie, but if (a == 'H') a = 'T'; if (a == 'T') a = 'H'; will make a == 'H' in both cases. You need an else here.)
    – M Oehm
    Nov 21 at 10:24






  • 1




    Run your code in a debugger (ie gdb) and it will tell you where in your code it is crashing
    – Chris Turner
    Nov 21 at 10:26






  • 2




    Valgrind is your friend when you have to find memory error, you should try it
    – Loufi
    Nov 21 at 10:34






  • 1




    (You call ´fill_it` recursively, but you have got a tail recursion here, which you can turn into a loop. With two nested loops, the function would be much clearer, in my opinion.)
    – M Oehm
    Nov 21 at 10:46






  • 1




    @MOehm there's also a similar problem with column2++ in the recursive call
    – Chris Turner
    Nov 21 at 10:46













up vote
1
down vote

favorite









up vote
1
down vote

favorite











hello guys this is my code :



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

int power(int a, int b) {
int exponent = b, result = 1;
while (exponent != 0) {
result = result * a;
exponent--;
}
//printf("%d",result);
return result;
}

int fill_it(char ** p, int N, int fliptimes, int column2) {
if (N < 0) return 0;
int counter = 0, l;
char a = 'H';
for (l = 0; l < power(2, fliptimes); l++) {
p[l][column2] = a;
counter++;
if (counter == (power(2, N) / 2)) {
counter = 0;
if (a == 'H') a = 'T';
if (a == 'T') a = 'H';
}
}
fill_it(p, N--, fliptimes, column2++);
}

int main() {
int i, fores, j, l, m;
char ** p;
printf("how many times did you toss the coin?:");
scanf("%d", & fores);
p = (char ** ) malloc((power(2, fores)) * sizeof(char * ));
for (i = 0; i < fores; i++)
p[i] = (char * ) malloc(fores * sizeof(char));
fill_it(p, fores, fores, 0);
for (l = 0; l < power(2, fores); l++) {
for (m = 0; m < fores; m++) {
printf("%c", p[l][m]);
}
}
printf(",");
}


it does compile.But when i run the program it returns a "segmantation fault (core dumped)" error



i know it means that i tried to access memory,i dont have acces to but i dont understand which part of the program is defective










share|improve this question















hello guys this is my code :



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

int power(int a, int b) {
int exponent = b, result = 1;
while (exponent != 0) {
result = result * a;
exponent--;
}
//printf("%d",result);
return result;
}

int fill_it(char ** p, int N, int fliptimes, int column2) {
if (N < 0) return 0;
int counter = 0, l;
char a = 'H';
for (l = 0; l < power(2, fliptimes); l++) {
p[l][column2] = a;
counter++;
if (counter == (power(2, N) / 2)) {
counter = 0;
if (a == 'H') a = 'T';
if (a == 'T') a = 'H';
}
}
fill_it(p, N--, fliptimes, column2++);
}

int main() {
int i, fores, j, l, m;
char ** p;
printf("how many times did you toss the coin?:");
scanf("%d", & fores);
p = (char ** ) malloc((power(2, fores)) * sizeof(char * ));
for (i = 0; i < fores; i++)
p[i] = (char * ) malloc(fores * sizeof(char));
fill_it(p, fores, fores, 0);
for (l = 0; l < power(2, fores); l++) {
for (m = 0; m < fores; m++) {
printf("%c", p[l][m]);
}
}
printf(",");
}


it does compile.But when i run the program it returns a "segmantation fault (core dumped)" error



i know it means that i tried to access memory,i dont have acces to but i dont understand which part of the program is defective







c arrays pointers segmentation-fault coredump






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 10:24









Sander De Dycker

12.3k12331




12.3k12331










asked Nov 21 at 10:19









dimitraaaa

82




82








  • 1




    (Unrelated to the seggie, but if (a == 'H') a = 'T'; if (a == 'T') a = 'H'; will make a == 'H' in both cases. You need an else here.)
    – M Oehm
    Nov 21 at 10:24






  • 1




    Run your code in a debugger (ie gdb) and it will tell you where in your code it is crashing
    – Chris Turner
    Nov 21 at 10:26






  • 2




    Valgrind is your friend when you have to find memory error, you should try it
    – Loufi
    Nov 21 at 10:34






  • 1




    (You call ´fill_it` recursively, but you have got a tail recursion here, which you can turn into a loop. With two nested loops, the function would be much clearer, in my opinion.)
    – M Oehm
    Nov 21 at 10:46






  • 1




    @MOehm there's also a similar problem with column2++ in the recursive call
    – Chris Turner
    Nov 21 at 10:46














  • 1




    (Unrelated to the seggie, but if (a == 'H') a = 'T'; if (a == 'T') a = 'H'; will make a == 'H' in both cases. You need an else here.)
    – M Oehm
    Nov 21 at 10:24






  • 1




    Run your code in a debugger (ie gdb) and it will tell you where in your code it is crashing
    – Chris Turner
    Nov 21 at 10:26






  • 2




    Valgrind is your friend when you have to find memory error, you should try it
    – Loufi
    Nov 21 at 10:34






  • 1




    (You call ´fill_it` recursively, but you have got a tail recursion here, which you can turn into a loop. With two nested loops, the function would be much clearer, in my opinion.)
    – M Oehm
    Nov 21 at 10:46






  • 1




    @MOehm there's also a similar problem with column2++ in the recursive call
    – Chris Turner
    Nov 21 at 10:46








1




1




(Unrelated to the seggie, but if (a == 'H') a = 'T'; if (a == 'T') a = 'H'; will make a == 'H' in both cases. You need an else here.)
– M Oehm
Nov 21 at 10:24




(Unrelated to the seggie, but if (a == 'H') a = 'T'; if (a == 'T') a = 'H'; will make a == 'H' in both cases. You need an else here.)
– M Oehm
Nov 21 at 10:24




1




1




Run your code in a debugger (ie gdb) and it will tell you where in your code it is crashing
– Chris Turner
Nov 21 at 10:26




Run your code in a debugger (ie gdb) and it will tell you where in your code it is crashing
– Chris Turner
Nov 21 at 10:26




2




2




Valgrind is your friend when you have to find memory error, you should try it
– Loufi
Nov 21 at 10:34




Valgrind is your friend when you have to find memory error, you should try it
– Loufi
Nov 21 at 10:34




1




1




(You call ´fill_it` recursively, but you have got a tail recursion here, which you can turn into a loop. With two nested loops, the function would be much clearer, in my opinion.)
– M Oehm
Nov 21 at 10:46




(You call ´fill_it` recursively, but you have got a tail recursion here, which you can turn into a loop. With two nested loops, the function would be much clearer, in my opinion.)
– M Oehm
Nov 21 at 10:46




1




1




@MOehm there's also a similar problem with column2++ in the recursive call
– Chris Turner
Nov 21 at 10:46




@MOehm there's also a similar problem with column2++ in the recursive call
– Chris Turner
Nov 21 at 10:46












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










The problem is, you're not allocating enough memory. This line is fine



p = (char ** ) malloc((power(2, fores)) * sizeof(char * ));


but this loop is only allocating memory for part of the 2-dimensional array.



for (i = 0; i < fores; i++)
p[i] = (char * ) malloc(fores * sizeof(char));


The memory allocation should look more like this...



foresSquared = power(2, fores);
p = malloc(foresSquared*sizeof(char *));
for (i = 0; i < foresSquared; i++)
p[i] = malloc(fores);


Since the result of power is going to be consistent, it makes sense to store the value in a variable and use that rather than recalculating it. It'll make the code clearer too.



You also don't need to cast the return value of malloc as C handles that for you. And sizeof(char) isn't needed as it's guaranteed to always be 1.






share|improve this answer





















  • You shouldn't cast void pointers in C
    – Jean-Marc Zimmer
    Nov 21 at 10:52










  • @Jean-MarcZimmer can't see where in my fixed version of the code that I'm doing?
    – Chris Turner
    Nov 21 at 10:54










  • Oh, whoops, my bad, I misread. It was on the first code section. I'm really terrbile at understanding written stuff...
    – Jean-Marc Zimmer
    Nov 21 at 11:02










  • @ChrisTurner thank you very much,you made my day! :-)
    – dimitraaaa
    Nov 21 at 12:08










  • @dimitraaaa if my answer is right it's traditional to mark it as such with a tick
    – Chris Turner
    Nov 21 at 13:13











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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409866%2fsegmantation-fault-malloc-pointers-functions%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








up vote
2
down vote



accepted










The problem is, you're not allocating enough memory. This line is fine



p = (char ** ) malloc((power(2, fores)) * sizeof(char * ));


but this loop is only allocating memory for part of the 2-dimensional array.



for (i = 0; i < fores; i++)
p[i] = (char * ) malloc(fores * sizeof(char));


The memory allocation should look more like this...



foresSquared = power(2, fores);
p = malloc(foresSquared*sizeof(char *));
for (i = 0; i < foresSquared; i++)
p[i] = malloc(fores);


Since the result of power is going to be consistent, it makes sense to store the value in a variable and use that rather than recalculating it. It'll make the code clearer too.



You also don't need to cast the return value of malloc as C handles that for you. And sizeof(char) isn't needed as it's guaranteed to always be 1.






share|improve this answer





















  • You shouldn't cast void pointers in C
    – Jean-Marc Zimmer
    Nov 21 at 10:52










  • @Jean-MarcZimmer can't see where in my fixed version of the code that I'm doing?
    – Chris Turner
    Nov 21 at 10:54










  • Oh, whoops, my bad, I misread. It was on the first code section. I'm really terrbile at understanding written stuff...
    – Jean-Marc Zimmer
    Nov 21 at 11:02










  • @ChrisTurner thank you very much,you made my day! :-)
    – dimitraaaa
    Nov 21 at 12:08










  • @dimitraaaa if my answer is right it's traditional to mark it as such with a tick
    – Chris Turner
    Nov 21 at 13:13















up vote
2
down vote



accepted










The problem is, you're not allocating enough memory. This line is fine



p = (char ** ) malloc((power(2, fores)) * sizeof(char * ));


but this loop is only allocating memory for part of the 2-dimensional array.



for (i = 0; i < fores; i++)
p[i] = (char * ) malloc(fores * sizeof(char));


The memory allocation should look more like this...



foresSquared = power(2, fores);
p = malloc(foresSquared*sizeof(char *));
for (i = 0; i < foresSquared; i++)
p[i] = malloc(fores);


Since the result of power is going to be consistent, it makes sense to store the value in a variable and use that rather than recalculating it. It'll make the code clearer too.



You also don't need to cast the return value of malloc as C handles that for you. And sizeof(char) isn't needed as it's guaranteed to always be 1.






share|improve this answer





















  • You shouldn't cast void pointers in C
    – Jean-Marc Zimmer
    Nov 21 at 10:52










  • @Jean-MarcZimmer can't see where in my fixed version of the code that I'm doing?
    – Chris Turner
    Nov 21 at 10:54










  • Oh, whoops, my bad, I misread. It was on the first code section. I'm really terrbile at understanding written stuff...
    – Jean-Marc Zimmer
    Nov 21 at 11:02










  • @ChrisTurner thank you very much,you made my day! :-)
    – dimitraaaa
    Nov 21 at 12:08










  • @dimitraaaa if my answer is right it's traditional to mark it as such with a tick
    – Chris Turner
    Nov 21 at 13:13













up vote
2
down vote



accepted







up vote
2
down vote



accepted






The problem is, you're not allocating enough memory. This line is fine



p = (char ** ) malloc((power(2, fores)) * sizeof(char * ));


but this loop is only allocating memory for part of the 2-dimensional array.



for (i = 0; i < fores; i++)
p[i] = (char * ) malloc(fores * sizeof(char));


The memory allocation should look more like this...



foresSquared = power(2, fores);
p = malloc(foresSquared*sizeof(char *));
for (i = 0; i < foresSquared; i++)
p[i] = malloc(fores);


Since the result of power is going to be consistent, it makes sense to store the value in a variable and use that rather than recalculating it. It'll make the code clearer too.



You also don't need to cast the return value of malloc as C handles that for you. And sizeof(char) isn't needed as it's guaranteed to always be 1.






share|improve this answer












The problem is, you're not allocating enough memory. This line is fine



p = (char ** ) malloc((power(2, fores)) * sizeof(char * ));


but this loop is only allocating memory for part of the 2-dimensional array.



for (i = 0; i < fores; i++)
p[i] = (char * ) malloc(fores * sizeof(char));


The memory allocation should look more like this...



foresSquared = power(2, fores);
p = malloc(foresSquared*sizeof(char *));
for (i = 0; i < foresSquared; i++)
p[i] = malloc(fores);


Since the result of power is going to be consistent, it makes sense to store the value in a variable and use that rather than recalculating it. It'll make the code clearer too.



You also don't need to cast the return value of malloc as C handles that for you. And sizeof(char) isn't needed as it's guaranteed to always be 1.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 10:44









Chris Turner

6,3771917




6,3771917












  • You shouldn't cast void pointers in C
    – Jean-Marc Zimmer
    Nov 21 at 10:52










  • @Jean-MarcZimmer can't see where in my fixed version of the code that I'm doing?
    – Chris Turner
    Nov 21 at 10:54










  • Oh, whoops, my bad, I misread. It was on the first code section. I'm really terrbile at understanding written stuff...
    – Jean-Marc Zimmer
    Nov 21 at 11:02










  • @ChrisTurner thank you very much,you made my day! :-)
    – dimitraaaa
    Nov 21 at 12:08










  • @dimitraaaa if my answer is right it's traditional to mark it as such with a tick
    – Chris Turner
    Nov 21 at 13:13


















  • You shouldn't cast void pointers in C
    – Jean-Marc Zimmer
    Nov 21 at 10:52










  • @Jean-MarcZimmer can't see where in my fixed version of the code that I'm doing?
    – Chris Turner
    Nov 21 at 10:54










  • Oh, whoops, my bad, I misread. It was on the first code section. I'm really terrbile at understanding written stuff...
    – Jean-Marc Zimmer
    Nov 21 at 11:02










  • @ChrisTurner thank you very much,you made my day! :-)
    – dimitraaaa
    Nov 21 at 12:08










  • @dimitraaaa if my answer is right it's traditional to mark it as such with a tick
    – Chris Turner
    Nov 21 at 13:13
















You shouldn't cast void pointers in C
– Jean-Marc Zimmer
Nov 21 at 10:52




You shouldn't cast void pointers in C
– Jean-Marc Zimmer
Nov 21 at 10:52












@Jean-MarcZimmer can't see where in my fixed version of the code that I'm doing?
– Chris Turner
Nov 21 at 10:54




@Jean-MarcZimmer can't see where in my fixed version of the code that I'm doing?
– Chris Turner
Nov 21 at 10:54












Oh, whoops, my bad, I misread. It was on the first code section. I'm really terrbile at understanding written stuff...
– Jean-Marc Zimmer
Nov 21 at 11:02




Oh, whoops, my bad, I misread. It was on the first code section. I'm really terrbile at understanding written stuff...
– Jean-Marc Zimmer
Nov 21 at 11:02












@ChrisTurner thank you very much,you made my day! :-)
– dimitraaaa
Nov 21 at 12:08




@ChrisTurner thank you very much,you made my day! :-)
– dimitraaaa
Nov 21 at 12:08












@dimitraaaa if my answer is right it's traditional to mark it as such with a tick
– Chris Turner
Nov 21 at 13:13




@dimitraaaa if my answer is right it's traditional to mark it as such with a tick
– Chris Turner
Nov 21 at 13:13


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53409866%2fsegmantation-fault-malloc-pointers-functions%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

Sphinx de Gizeh

Dijon

Determine an Integral..