Grow the row of a 2D array with realloc?












0















I am working on a function that internalizes and extends a 2D array by reference. Basically, when I call this function I want grow the row by 1 and create a size of columns each time.



(EDIT: for clarification)



2D array **foo = NULL
call func_v2()
if **foo = NULL
then foo = [0]
then foo = [0][size]
then foo = [1]

call func_v2()
**foo != NULL
foo[1][size]
foo[2]

call func_v2()
**foo != NULL
foo[2][size]
foo[3]
//And so on and so on


The idea is that each time time I call this function I am setting up the next row so that I can allocate space for column when I call the function again.



My problem is every time I call realloc in my function it crashes. I am not sure why realloc does not work here. How can I get realloc to work and can someone explain why it doesn't work thanks.



   void func_v2(int ***arr, int size, int *len){
/*set up first row space if NULL*/
if (*arr == NULL) {
*arr = malloc((*len) *sizeof(int*));
}

/*create columns in the array*/
(*arr)[(*len) - 1] = malloc(size * sizeof(int *));


/*debug test set with values*/
for (int j = 0; j < size; j++) {
(*arr)[*len - 1][j] = (*len);
}

/*increment length of row*/
++(*len);

/*extend row size*/
*arr = realloc(*arr, ((*len)) * sizeof(int*)); //WHY DOES THIS NOT WORK?
}

int main(void) {
int **foo = NULL;
int size = 5;
int len = 1;

func_v2(&foo,size,&len);


for (int i = 0; i < len-1; i++) {
for (int j = 0; j < size; j++) {
printf("%d ", foo[i][j]);
}
printf("n");

}

return 0;
}









share|improve this question

























  • Even without calling realloc this will crash. Can you please explain again what you want? You want to have a dynamically allocated 2D array, that will grow by one row every time your function is called?

    – gsamaras
    Nov 23 '18 at 16:37











  • OK, sorry. What I want is this to create a 2D array. Based on a size. Each time the function is called it will create a array[0][size] then inc the row count by 1 so the next time the function is called it will just create enough memory for that spot than and inc the row again. I will edit the post to make it more clear sorry.

    – T.Malo
    Nov 23 '18 at 16:40











  • An array cannot have zero size! So what do you really want to do? :)

    – gsamaras
    Nov 23 '18 at 16:41













  • what do you mean?

    – T.Malo
    Nov 23 '18 at 16:52






  • 1





    @T.Malo What do you think *arr[(*len) - 1] is like? (*arr)[(*len) - 1] or *(arr[(*len) - 1])?

    – chux
    Nov 23 '18 at 17:05
















0















I am working on a function that internalizes and extends a 2D array by reference. Basically, when I call this function I want grow the row by 1 and create a size of columns each time.



(EDIT: for clarification)



2D array **foo = NULL
call func_v2()
if **foo = NULL
then foo = [0]
then foo = [0][size]
then foo = [1]

call func_v2()
**foo != NULL
foo[1][size]
foo[2]

call func_v2()
**foo != NULL
foo[2][size]
foo[3]
//And so on and so on


The idea is that each time time I call this function I am setting up the next row so that I can allocate space for column when I call the function again.



My problem is every time I call realloc in my function it crashes. I am not sure why realloc does not work here. How can I get realloc to work and can someone explain why it doesn't work thanks.



   void func_v2(int ***arr, int size, int *len){
/*set up first row space if NULL*/
if (*arr == NULL) {
*arr = malloc((*len) *sizeof(int*));
}

/*create columns in the array*/
(*arr)[(*len) - 1] = malloc(size * sizeof(int *));


/*debug test set with values*/
for (int j = 0; j < size; j++) {
(*arr)[*len - 1][j] = (*len);
}

/*increment length of row*/
++(*len);

/*extend row size*/
*arr = realloc(*arr, ((*len)) * sizeof(int*)); //WHY DOES THIS NOT WORK?
}

int main(void) {
int **foo = NULL;
int size = 5;
int len = 1;

func_v2(&foo,size,&len);


for (int i = 0; i < len-1; i++) {
for (int j = 0; j < size; j++) {
printf("%d ", foo[i][j]);
}
printf("n");

}

return 0;
}









share|improve this question

























  • Even without calling realloc this will crash. Can you please explain again what you want? You want to have a dynamically allocated 2D array, that will grow by one row every time your function is called?

    – gsamaras
    Nov 23 '18 at 16:37











  • OK, sorry. What I want is this to create a 2D array. Based on a size. Each time the function is called it will create a array[0][size] then inc the row count by 1 so the next time the function is called it will just create enough memory for that spot than and inc the row again. I will edit the post to make it more clear sorry.

    – T.Malo
    Nov 23 '18 at 16:40











  • An array cannot have zero size! So what do you really want to do? :)

    – gsamaras
    Nov 23 '18 at 16:41













  • what do you mean?

    – T.Malo
    Nov 23 '18 at 16:52






  • 1





    @T.Malo What do you think *arr[(*len) - 1] is like? (*arr)[(*len) - 1] or *(arr[(*len) - 1])?

    – chux
    Nov 23 '18 at 17:05














0












0








0








I am working on a function that internalizes and extends a 2D array by reference. Basically, when I call this function I want grow the row by 1 and create a size of columns each time.



(EDIT: for clarification)



2D array **foo = NULL
call func_v2()
if **foo = NULL
then foo = [0]
then foo = [0][size]
then foo = [1]

call func_v2()
**foo != NULL
foo[1][size]
foo[2]

call func_v2()
**foo != NULL
foo[2][size]
foo[3]
//And so on and so on


The idea is that each time time I call this function I am setting up the next row so that I can allocate space for column when I call the function again.



My problem is every time I call realloc in my function it crashes. I am not sure why realloc does not work here. How can I get realloc to work and can someone explain why it doesn't work thanks.



   void func_v2(int ***arr, int size, int *len){
/*set up first row space if NULL*/
if (*arr == NULL) {
*arr = malloc((*len) *sizeof(int*));
}

/*create columns in the array*/
(*arr)[(*len) - 1] = malloc(size * sizeof(int *));


/*debug test set with values*/
for (int j = 0; j < size; j++) {
(*arr)[*len - 1][j] = (*len);
}

/*increment length of row*/
++(*len);

/*extend row size*/
*arr = realloc(*arr, ((*len)) * sizeof(int*)); //WHY DOES THIS NOT WORK?
}

int main(void) {
int **foo = NULL;
int size = 5;
int len = 1;

func_v2(&foo,size,&len);


for (int i = 0; i < len-1; i++) {
for (int j = 0; j < size; j++) {
printf("%d ", foo[i][j]);
}
printf("n");

}

return 0;
}









share|improve this question
















I am working on a function that internalizes and extends a 2D array by reference. Basically, when I call this function I want grow the row by 1 and create a size of columns each time.



(EDIT: for clarification)



2D array **foo = NULL
call func_v2()
if **foo = NULL
then foo = [0]
then foo = [0][size]
then foo = [1]

call func_v2()
**foo != NULL
foo[1][size]
foo[2]

call func_v2()
**foo != NULL
foo[2][size]
foo[3]
//And so on and so on


The idea is that each time time I call this function I am setting up the next row so that I can allocate space for column when I call the function again.



My problem is every time I call realloc in my function it crashes. I am not sure why realloc does not work here. How can I get realloc to work and can someone explain why it doesn't work thanks.



   void func_v2(int ***arr, int size, int *len){
/*set up first row space if NULL*/
if (*arr == NULL) {
*arr = malloc((*len) *sizeof(int*));
}

/*create columns in the array*/
(*arr)[(*len) - 1] = malloc(size * sizeof(int *));


/*debug test set with values*/
for (int j = 0; j < size; j++) {
(*arr)[*len - 1][j] = (*len);
}

/*increment length of row*/
++(*len);

/*extend row size*/
*arr = realloc(*arr, ((*len)) * sizeof(int*)); //WHY DOES THIS NOT WORK?
}

int main(void) {
int **foo = NULL;
int size = 5;
int len = 1;

func_v2(&foo,size,&len);


for (int i = 0; i < len-1; i++) {
for (int j = 0; j < size; j++) {
printf("%d ", foo[i][j]);
}
printf("n");

}

return 0;
}






c arrays dynamic malloc realloc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 17:17







T.Malo

















asked Nov 23 '18 at 16:31









T.MaloT.Malo

367320




367320













  • Even without calling realloc this will crash. Can you please explain again what you want? You want to have a dynamically allocated 2D array, that will grow by one row every time your function is called?

    – gsamaras
    Nov 23 '18 at 16:37











  • OK, sorry. What I want is this to create a 2D array. Based on a size. Each time the function is called it will create a array[0][size] then inc the row count by 1 so the next time the function is called it will just create enough memory for that spot than and inc the row again. I will edit the post to make it more clear sorry.

    – T.Malo
    Nov 23 '18 at 16:40











  • An array cannot have zero size! So what do you really want to do? :)

    – gsamaras
    Nov 23 '18 at 16:41













  • what do you mean?

    – T.Malo
    Nov 23 '18 at 16:52






  • 1





    @T.Malo What do you think *arr[(*len) - 1] is like? (*arr)[(*len) - 1] or *(arr[(*len) - 1])?

    – chux
    Nov 23 '18 at 17:05



















  • Even without calling realloc this will crash. Can you please explain again what you want? You want to have a dynamically allocated 2D array, that will grow by one row every time your function is called?

    – gsamaras
    Nov 23 '18 at 16:37











  • OK, sorry. What I want is this to create a 2D array. Based on a size. Each time the function is called it will create a array[0][size] then inc the row count by 1 so the next time the function is called it will just create enough memory for that spot than and inc the row again. I will edit the post to make it more clear sorry.

    – T.Malo
    Nov 23 '18 at 16:40











  • An array cannot have zero size! So what do you really want to do? :)

    – gsamaras
    Nov 23 '18 at 16:41













  • what do you mean?

    – T.Malo
    Nov 23 '18 at 16:52






  • 1





    @T.Malo What do you think *arr[(*len) - 1] is like? (*arr)[(*len) - 1] or *(arr[(*len) - 1])?

    – chux
    Nov 23 '18 at 17:05

















Even without calling realloc this will crash. Can you please explain again what you want? You want to have a dynamically allocated 2D array, that will grow by one row every time your function is called?

– gsamaras
Nov 23 '18 at 16:37





Even without calling realloc this will crash. Can you please explain again what you want? You want to have a dynamically allocated 2D array, that will grow by one row every time your function is called?

– gsamaras
Nov 23 '18 at 16:37













OK, sorry. What I want is this to create a 2D array. Based on a size. Each time the function is called it will create a array[0][size] then inc the row count by 1 so the next time the function is called it will just create enough memory for that spot than and inc the row again. I will edit the post to make it more clear sorry.

– T.Malo
Nov 23 '18 at 16:40





OK, sorry. What I want is this to create a 2D array. Based on a size. Each time the function is called it will create a array[0][size] then inc the row count by 1 so the next time the function is called it will just create enough memory for that spot than and inc the row again. I will edit the post to make it more clear sorry.

– T.Malo
Nov 23 '18 at 16:40













An array cannot have zero size! So what do you really want to do? :)

– gsamaras
Nov 23 '18 at 16:41







An array cannot have zero size! So what do you really want to do? :)

– gsamaras
Nov 23 '18 at 16:41















what do you mean?

– T.Malo
Nov 23 '18 at 16:52





what do you mean?

– T.Malo
Nov 23 '18 at 16:52




1




1





@T.Malo What do you think *arr[(*len) - 1] is like? (*arr)[(*len) - 1] or *(arr[(*len) - 1])?

– chux
Nov 23 '18 at 17:05





@T.Malo What do you think *arr[(*len) - 1] is like? (*arr)[(*len) - 1] or *(arr[(*len) - 1])?

– chux
Nov 23 '18 at 17:05












0






active

oldest

votes











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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450181%2fgrow-the-row-of-a-2d-array-with-realloc%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53450181%2fgrow-the-row-of-a-2d-array-with-realloc%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

Wiesbaden

Marschland

Dieringhausen