How to allocate memory for pointers to pointers?
I am struggling with dynamic memory allocation. I can't see any mistakes but still get segmentation fault. Any comments on how i can improve my code are welcome.
char* balanceStatements(char* lst)
{
<some code>
char** bad = malloc(sizeof(char*)); // the list of the bad parts of char* lst
char* badOrder = &lst[0]; // the pointer to the first character of the part in lst
<some code>
bad = includeOrder(badOrder, bad);
includeOrder:
char** includeOrder(char* order, char** list)
static int i = 0;
list = realloc(list, sizeof(list) + sizeof(char*)); //allocation of memory for a new pointer in the list
list[i++] = order;
printf("vad %cn", list[0][0]);
return list;
the input that causes segfault is ""ZNGA 1300 2.66, CLH15.NYM 50 56.32 B, OWW 1000 11.623 B, OGG 20 580.1 B", the first part of it (ZNGA 1300 2.66) is bad, the parts are splitted with ','. This input "GOOG 300 542.0 B,AAPL 50 145.0 B,CSCO 250.0 29 B,GOOG 200 580.0 S", with the part "CSCO 250.0 29 B" being bad, does not cause the segfault. So is the cause of segfault in the improper memory allocation? If so how it should be done properly?
full code:
#include <stdlib.h>
#include <string.h>
// result will be freed
char** includeOrder(char* order, char** list)
static int i = 0;
list = realloc(list, sizeof(list) + sizeof(char*)); //allocation of memory for a new pointer in the list
list[i++] = order;
printf("vad %cn", list[0][0]);
return list;
char* balanceStatements(char* lst)
char type = 0;
double buy = 0;
double sell = 0;
int i = 0;
char tempQ[100] = ;
char tempP[100] = ;
char** bad = malloc(sizeof(char*)); // the list of the bad parts of char* lst
int nword = 0;
int k = 0;
int j = 0;
int isComma = 0;
char* badOrder = &lst[0]; // the pointer to the first character of the part in lst
printf("badOrder1 %c", badOrder[0]);
char* res = malloc(1000*sizeof(char));
res[0]="A";
while (lst[i]!='')
printf("char %cn", lst[i]);
if (lst[i] == ',')
printf("bbbb");
if (nword != 3)
printf("bad order 2 %cn", badOrder);
bad = includeOrder(badOrder, bad);
printf("asdan");
printf("aaaa");
if (isComma)
if (type == 'B') buy += atoi(tempQ) * atof(tempP);
else sell += atoi(tempQ) * atof(tempP);
printf("sssss");
nword = 0;
badOrder=&lst[++i];
isComma = 0;
k = 0;
j = 0;
if (lst[i] == ' ')
nword++;
i++;
if (nword == 0) i++;
if (nword == 1)
lst[i]<'0')
printf("bad ordern");
bad = includeOrder(badOrder, bad);
printf("svad %cn", bad[0][0]);
while(lst[++i]!=',');
else
tempQ[k++] = lst[i++];
if (nword == 2)
if (nword == 3)
tempP[++j] = '';
printf("tempP %sn", tempP);
// sell
//fill in
if (lst[i] != 'B' && lst[i] !='S')
includeOrder(badOrder, bad);
while(lst[++i]!=',');
type = lst[i++];
/* if (nword == 4)
includeOrder(badOrder, bad);
while(lst[++i]!=',');
*/
if (type == 'S')
sell += atoi(tempQ) * atof(tempP);
else
buy += atoi(tempQ) * atof(tempP);
i=0;
// your code
printf("buy %fn", buy);
printf("sell %fn", sell);
printf("bad %cn", bad[0][0]);
int n = 0;
while (*(bad + n))
n++;
n--;
printf("size %in", n);
while (bad[0][i]!=',' && bad[0][i]!='')
printf("%c", bad[0][i++]);
sprintf(res, "Buy: %i Sell: %i", (int)buy, (int)sell);
if (bad)
res[strlen(res)]=';';
strcpy(&res[strlen(res)], " Badly formed ");
res[strlen(res)] = n+48;
res[strlen(res)] = ':';
res[strlen(res)] = ' ';
int i = 0;
while ( bad[0][i] != ',' )
res[strlen(res)] = bad[0][i++];
res[strlen(res)] = ' ';
res[strlen(res)] = ';';
return res;
c memory-management
add a comment |
I am struggling with dynamic memory allocation. I can't see any mistakes but still get segmentation fault. Any comments on how i can improve my code are welcome.
char* balanceStatements(char* lst)
{
<some code>
char** bad = malloc(sizeof(char*)); // the list of the bad parts of char* lst
char* badOrder = &lst[0]; // the pointer to the first character of the part in lst
<some code>
bad = includeOrder(badOrder, bad);
includeOrder:
char** includeOrder(char* order, char** list)
static int i = 0;
list = realloc(list, sizeof(list) + sizeof(char*)); //allocation of memory for a new pointer in the list
list[i++] = order;
printf("vad %cn", list[0][0]);
return list;
the input that causes segfault is ""ZNGA 1300 2.66, CLH15.NYM 50 56.32 B, OWW 1000 11.623 B, OGG 20 580.1 B", the first part of it (ZNGA 1300 2.66) is bad, the parts are splitted with ','. This input "GOOG 300 542.0 B,AAPL 50 145.0 B,CSCO 250.0 29 B,GOOG 200 580.0 S", with the part "CSCO 250.0 29 B" being bad, does not cause the segfault. So is the cause of segfault in the improper memory allocation? If so how it should be done properly?
full code:
#include <stdlib.h>
#include <string.h>
// result will be freed
char** includeOrder(char* order, char** list)
static int i = 0;
list = realloc(list, sizeof(list) + sizeof(char*)); //allocation of memory for a new pointer in the list
list[i++] = order;
printf("vad %cn", list[0][0]);
return list;
char* balanceStatements(char* lst)
char type = 0;
double buy = 0;
double sell = 0;
int i = 0;
char tempQ[100] = ;
char tempP[100] = ;
char** bad = malloc(sizeof(char*)); // the list of the bad parts of char* lst
int nword = 0;
int k = 0;
int j = 0;
int isComma = 0;
char* badOrder = &lst[0]; // the pointer to the first character of the part in lst
printf("badOrder1 %c", badOrder[0]);
char* res = malloc(1000*sizeof(char));
res[0]="A";
while (lst[i]!='')
printf("char %cn", lst[i]);
if (lst[i] == ',')
printf("bbbb");
if (nword != 3)
printf("bad order 2 %cn", badOrder);
bad = includeOrder(badOrder, bad);
printf("asdan");
printf("aaaa");
if (isComma)
if (type == 'B') buy += atoi(tempQ) * atof(tempP);
else sell += atoi(tempQ) * atof(tempP);
printf("sssss");
nword = 0;
badOrder=&lst[++i];
isComma = 0;
k = 0;
j = 0;
if (lst[i] == ' ')
nword++;
i++;
if (nword == 0) i++;
if (nword == 1)
lst[i]<'0')
printf("bad ordern");
bad = includeOrder(badOrder, bad);
printf("svad %cn", bad[0][0]);
while(lst[++i]!=',');
else
tempQ[k++] = lst[i++];
if (nword == 2)
if (nword == 3)
tempP[++j] = '';
printf("tempP %sn", tempP);
// sell
//fill in
if (lst[i] != 'B' && lst[i] !='S')
includeOrder(badOrder, bad);
while(lst[++i]!=',');
type = lst[i++];
/* if (nword == 4)
includeOrder(badOrder, bad);
while(lst[++i]!=',');
*/
if (type == 'S')
sell += atoi(tempQ) * atof(tempP);
else
buy += atoi(tempQ) * atof(tempP);
i=0;
// your code
printf("buy %fn", buy);
printf("sell %fn", sell);
printf("bad %cn", bad[0][0]);
int n = 0;
while (*(bad + n))
n++;
n--;
printf("size %in", n);
while (bad[0][i]!=',' && bad[0][i]!='')
printf("%c", bad[0][i++]);
sprintf(res, "Buy: %i Sell: %i", (int)buy, (int)sell);
if (bad)
res[strlen(res)]=';';
strcpy(&res[strlen(res)], " Badly formed ");
res[strlen(res)] = n+48;
res[strlen(res)] = ':';
res[strlen(res)] = ' ';
int i = 0;
while ( bad[0][i] != ',' )
res[strlen(res)] = bad[0][i++];
res[strlen(res)] = ' ';
res[strlen(res)] = ';';
return res;
c memory-management
add a comment |
I am struggling with dynamic memory allocation. I can't see any mistakes but still get segmentation fault. Any comments on how i can improve my code are welcome.
char* balanceStatements(char* lst)
{
<some code>
char** bad = malloc(sizeof(char*)); // the list of the bad parts of char* lst
char* badOrder = &lst[0]; // the pointer to the first character of the part in lst
<some code>
bad = includeOrder(badOrder, bad);
includeOrder:
char** includeOrder(char* order, char** list)
static int i = 0;
list = realloc(list, sizeof(list) + sizeof(char*)); //allocation of memory for a new pointer in the list
list[i++] = order;
printf("vad %cn", list[0][0]);
return list;
the input that causes segfault is ""ZNGA 1300 2.66, CLH15.NYM 50 56.32 B, OWW 1000 11.623 B, OGG 20 580.1 B", the first part of it (ZNGA 1300 2.66) is bad, the parts are splitted with ','. This input "GOOG 300 542.0 B,AAPL 50 145.0 B,CSCO 250.0 29 B,GOOG 200 580.0 S", with the part "CSCO 250.0 29 B" being bad, does not cause the segfault. So is the cause of segfault in the improper memory allocation? If so how it should be done properly?
full code:
#include <stdlib.h>
#include <string.h>
// result will be freed
char** includeOrder(char* order, char** list)
static int i = 0;
list = realloc(list, sizeof(list) + sizeof(char*)); //allocation of memory for a new pointer in the list
list[i++] = order;
printf("vad %cn", list[0][0]);
return list;
char* balanceStatements(char* lst)
char type = 0;
double buy = 0;
double sell = 0;
int i = 0;
char tempQ[100] = ;
char tempP[100] = ;
char** bad = malloc(sizeof(char*)); // the list of the bad parts of char* lst
int nword = 0;
int k = 0;
int j = 0;
int isComma = 0;
char* badOrder = &lst[0]; // the pointer to the first character of the part in lst
printf("badOrder1 %c", badOrder[0]);
char* res = malloc(1000*sizeof(char));
res[0]="A";
while (lst[i]!='')
printf("char %cn", lst[i]);
if (lst[i] == ',')
printf("bbbb");
if (nword != 3)
printf("bad order 2 %cn", badOrder);
bad = includeOrder(badOrder, bad);
printf("asdan");
printf("aaaa");
if (isComma)
if (type == 'B') buy += atoi(tempQ) * atof(tempP);
else sell += atoi(tempQ) * atof(tempP);
printf("sssss");
nword = 0;
badOrder=&lst[++i];
isComma = 0;
k = 0;
j = 0;
if (lst[i] == ' ')
nword++;
i++;
if (nword == 0) i++;
if (nword == 1)
lst[i]<'0')
printf("bad ordern");
bad = includeOrder(badOrder, bad);
printf("svad %cn", bad[0][0]);
while(lst[++i]!=',');
else
tempQ[k++] = lst[i++];
if (nword == 2)
if (nword == 3)
tempP[++j] = '';
printf("tempP %sn", tempP);
// sell
//fill in
if (lst[i] != 'B' && lst[i] !='S')
includeOrder(badOrder, bad);
while(lst[++i]!=',');
type = lst[i++];
/* if (nword == 4)
includeOrder(badOrder, bad);
while(lst[++i]!=',');
*/
if (type == 'S')
sell += atoi(tempQ) * atof(tempP);
else
buy += atoi(tempQ) * atof(tempP);
i=0;
// your code
printf("buy %fn", buy);
printf("sell %fn", sell);
printf("bad %cn", bad[0][0]);
int n = 0;
while (*(bad + n))
n++;
n--;
printf("size %in", n);
while (bad[0][i]!=',' && bad[0][i]!='')
printf("%c", bad[0][i++]);
sprintf(res, "Buy: %i Sell: %i", (int)buy, (int)sell);
if (bad)
res[strlen(res)]=';';
strcpy(&res[strlen(res)], " Badly formed ");
res[strlen(res)] = n+48;
res[strlen(res)] = ':';
res[strlen(res)] = ' ';
int i = 0;
while ( bad[0][i] != ',' )
res[strlen(res)] = bad[0][i++];
res[strlen(res)] = ' ';
res[strlen(res)] = ';';
return res;
c memory-management
I am struggling with dynamic memory allocation. I can't see any mistakes but still get segmentation fault. Any comments on how i can improve my code are welcome.
char* balanceStatements(char* lst)
{
<some code>
char** bad = malloc(sizeof(char*)); // the list of the bad parts of char* lst
char* badOrder = &lst[0]; // the pointer to the first character of the part in lst
<some code>
bad = includeOrder(badOrder, bad);
includeOrder:
char** includeOrder(char* order, char** list)
static int i = 0;
list = realloc(list, sizeof(list) + sizeof(char*)); //allocation of memory for a new pointer in the list
list[i++] = order;
printf("vad %cn", list[0][0]);
return list;
the input that causes segfault is ""ZNGA 1300 2.66, CLH15.NYM 50 56.32 B, OWW 1000 11.623 B, OGG 20 580.1 B", the first part of it (ZNGA 1300 2.66) is bad, the parts are splitted with ','. This input "GOOG 300 542.0 B,AAPL 50 145.0 B,CSCO 250.0 29 B,GOOG 200 580.0 S", with the part "CSCO 250.0 29 B" being bad, does not cause the segfault. So is the cause of segfault in the improper memory allocation? If so how it should be done properly?
full code:
#include <stdlib.h>
#include <string.h>
// result will be freed
char** includeOrder(char* order, char** list)
static int i = 0;
list = realloc(list, sizeof(list) + sizeof(char*)); //allocation of memory for a new pointer in the list
list[i++] = order;
printf("vad %cn", list[0][0]);
return list;
char* balanceStatements(char* lst)
char type = 0;
double buy = 0;
double sell = 0;
int i = 0;
char tempQ[100] = ;
char tempP[100] = ;
char** bad = malloc(sizeof(char*)); // the list of the bad parts of char* lst
int nword = 0;
int k = 0;
int j = 0;
int isComma = 0;
char* badOrder = &lst[0]; // the pointer to the first character of the part in lst
printf("badOrder1 %c", badOrder[0]);
char* res = malloc(1000*sizeof(char));
res[0]="A";
while (lst[i]!='')
printf("char %cn", lst[i]);
if (lst[i] == ',')
printf("bbbb");
if (nword != 3)
printf("bad order 2 %cn", badOrder);
bad = includeOrder(badOrder, bad);
printf("asdan");
printf("aaaa");
if (isComma)
if (type == 'B') buy += atoi(tempQ) * atof(tempP);
else sell += atoi(tempQ) * atof(tempP);
printf("sssss");
nword = 0;
badOrder=&lst[++i];
isComma = 0;
k = 0;
j = 0;
if (lst[i] == ' ')
nword++;
i++;
if (nword == 0) i++;
if (nword == 1)
lst[i]<'0')
printf("bad ordern");
bad = includeOrder(badOrder, bad);
printf("svad %cn", bad[0][0]);
while(lst[++i]!=',');
else
tempQ[k++] = lst[i++];
if (nword == 2)
if (nword == 3)
tempP[++j] = '';
printf("tempP %sn", tempP);
// sell
//fill in
if (lst[i] != 'B' && lst[i] !='S')
includeOrder(badOrder, bad);
while(lst[++i]!=',');
type = lst[i++];
/* if (nword == 4)
includeOrder(badOrder, bad);
while(lst[++i]!=',');
*/
if (type == 'S')
sell += atoi(tempQ) * atof(tempP);
else
buy += atoi(tempQ) * atof(tempP);
i=0;
// your code
printf("buy %fn", buy);
printf("sell %fn", sell);
printf("bad %cn", bad[0][0]);
int n = 0;
while (*(bad + n))
n++;
n--;
printf("size %in", n);
while (bad[0][i]!=',' && bad[0][i]!='')
printf("%c", bad[0][i++]);
sprintf(res, "Buy: %i Sell: %i", (int)buy, (int)sell);
if (bad)
res[strlen(res)]=';';
strcpy(&res[strlen(res)], " Badly formed ");
res[strlen(res)] = n+48;
res[strlen(res)] = ':';
res[strlen(res)] = ' ';
int i = 0;
while ( bad[0][i] != ',' )
res[strlen(res)] = bad[0][i++];
res[strlen(res)] = ' ';
res[strlen(res)] = ';';
return res;
c memory-management
c memory-management
asked Nov 11 '18 at 11:22
norvdnorvd
32
32
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The most obvious error that will crash this program is in this line:
list = realloc(list, sizeof(list) + sizeof(char*));
sizeof()
is a compile time operator (except for variable lenght arrays (VLA), but these are not here), that returns the size in bytes of the type of the argument. So sizeof(list)
return the size of a char**
, not the allocated size of the object. There is not a function that returns the allocated size of a dynamic memory block in C, you have to keep track of that yourself, in an additional variable.
char** includeOrder(char* order, char** list, size_t *list_len)
list = realloc(list, *list_len + sizeof(char*));
...
return list;
And then in the main function:
size_t list_len = sizeof(char*); //0 is also valid
char** bad = malloc(list_len);
...
bad = includeOrder(badOrder, bad, &list_len);
Now that you are into pass-by-pointer functions you could do the same for the list
variable:
void includeOrder(char* order, char*** list, size_t *list_len)
*list = realloc(*list, *list_len + sizeof(char*));
...
ok, got it, the static int i looks like a good option for it, what if i use it like list = realloc(list, (i + 1) * sizeof(char*));?
– norvd
Nov 11 '18 at 11:35
@norvd: Usually the size of the list should be in the same place as the list itself. Ifbad
is a local variable, thenbad_len
should be a local variable, too. Also think if you want it as number of bytes or number of elements. My code assumed number of bytes but your comment is number of elements; either one is good.
– rodrigo
Nov 11 '18 at 11:38
@norvd: A sensible approach would be to create a typestruct string_list char **ptr; size_t len;
and a functionstring_list_add()
that does the hard work.
– rodrigo
Nov 11 '18 at 11:39
add a comment |
Your problem is a misunderstanding of sizeof(list)
. sizeof
returns the memory size of list
, i.e. the size of a char**
. It doesn't take into consideration the number of elements in your list.
Keep in mind that sizeof
isn't a function that is executed during runtime. It is a compile time unary operator.
add a comment |
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%2f53248212%2fhow-to-allocate-memory-for-pointers-to-pointers%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
The most obvious error that will crash this program is in this line:
list = realloc(list, sizeof(list) + sizeof(char*));
sizeof()
is a compile time operator (except for variable lenght arrays (VLA), but these are not here), that returns the size in bytes of the type of the argument. So sizeof(list)
return the size of a char**
, not the allocated size of the object. There is not a function that returns the allocated size of a dynamic memory block in C, you have to keep track of that yourself, in an additional variable.
char** includeOrder(char* order, char** list, size_t *list_len)
list = realloc(list, *list_len + sizeof(char*));
...
return list;
And then in the main function:
size_t list_len = sizeof(char*); //0 is also valid
char** bad = malloc(list_len);
...
bad = includeOrder(badOrder, bad, &list_len);
Now that you are into pass-by-pointer functions you could do the same for the list
variable:
void includeOrder(char* order, char*** list, size_t *list_len)
*list = realloc(*list, *list_len + sizeof(char*));
...
ok, got it, the static int i looks like a good option for it, what if i use it like list = realloc(list, (i + 1) * sizeof(char*));?
– norvd
Nov 11 '18 at 11:35
@norvd: Usually the size of the list should be in the same place as the list itself. Ifbad
is a local variable, thenbad_len
should be a local variable, too. Also think if you want it as number of bytes or number of elements. My code assumed number of bytes but your comment is number of elements; either one is good.
– rodrigo
Nov 11 '18 at 11:38
@norvd: A sensible approach would be to create a typestruct string_list char **ptr; size_t len;
and a functionstring_list_add()
that does the hard work.
– rodrigo
Nov 11 '18 at 11:39
add a comment |
The most obvious error that will crash this program is in this line:
list = realloc(list, sizeof(list) + sizeof(char*));
sizeof()
is a compile time operator (except for variable lenght arrays (VLA), but these are not here), that returns the size in bytes of the type of the argument. So sizeof(list)
return the size of a char**
, not the allocated size of the object. There is not a function that returns the allocated size of a dynamic memory block in C, you have to keep track of that yourself, in an additional variable.
char** includeOrder(char* order, char** list, size_t *list_len)
list = realloc(list, *list_len + sizeof(char*));
...
return list;
And then in the main function:
size_t list_len = sizeof(char*); //0 is also valid
char** bad = malloc(list_len);
...
bad = includeOrder(badOrder, bad, &list_len);
Now that you are into pass-by-pointer functions you could do the same for the list
variable:
void includeOrder(char* order, char*** list, size_t *list_len)
*list = realloc(*list, *list_len + sizeof(char*));
...
ok, got it, the static int i looks like a good option for it, what if i use it like list = realloc(list, (i + 1) * sizeof(char*));?
– norvd
Nov 11 '18 at 11:35
@norvd: Usually the size of the list should be in the same place as the list itself. Ifbad
is a local variable, thenbad_len
should be a local variable, too. Also think if you want it as number of bytes or number of elements. My code assumed number of bytes but your comment is number of elements; either one is good.
– rodrigo
Nov 11 '18 at 11:38
@norvd: A sensible approach would be to create a typestruct string_list char **ptr; size_t len;
and a functionstring_list_add()
that does the hard work.
– rodrigo
Nov 11 '18 at 11:39
add a comment |
The most obvious error that will crash this program is in this line:
list = realloc(list, sizeof(list) + sizeof(char*));
sizeof()
is a compile time operator (except for variable lenght arrays (VLA), but these are not here), that returns the size in bytes of the type of the argument. So sizeof(list)
return the size of a char**
, not the allocated size of the object. There is not a function that returns the allocated size of a dynamic memory block in C, you have to keep track of that yourself, in an additional variable.
char** includeOrder(char* order, char** list, size_t *list_len)
list = realloc(list, *list_len + sizeof(char*));
...
return list;
And then in the main function:
size_t list_len = sizeof(char*); //0 is also valid
char** bad = malloc(list_len);
...
bad = includeOrder(badOrder, bad, &list_len);
Now that you are into pass-by-pointer functions you could do the same for the list
variable:
void includeOrder(char* order, char*** list, size_t *list_len)
*list = realloc(*list, *list_len + sizeof(char*));
...
The most obvious error that will crash this program is in this line:
list = realloc(list, sizeof(list) + sizeof(char*));
sizeof()
is a compile time operator (except for variable lenght arrays (VLA), but these are not here), that returns the size in bytes of the type of the argument. So sizeof(list)
return the size of a char**
, not the allocated size of the object. There is not a function that returns the allocated size of a dynamic memory block in C, you have to keep track of that yourself, in an additional variable.
char** includeOrder(char* order, char** list, size_t *list_len)
list = realloc(list, *list_len + sizeof(char*));
...
return list;
And then in the main function:
size_t list_len = sizeof(char*); //0 is also valid
char** bad = malloc(list_len);
...
bad = includeOrder(badOrder, bad, &list_len);
Now that you are into pass-by-pointer functions you could do the same for the list
variable:
void includeOrder(char* order, char*** list, size_t *list_len)
*list = realloc(*list, *list_len + sizeof(char*));
...
edited Nov 11 '18 at 11:36
answered Nov 11 '18 at 11:32
rodrigorodrigo
63.6k493129
63.6k493129
ok, got it, the static int i looks like a good option for it, what if i use it like list = realloc(list, (i + 1) * sizeof(char*));?
– norvd
Nov 11 '18 at 11:35
@norvd: Usually the size of the list should be in the same place as the list itself. Ifbad
is a local variable, thenbad_len
should be a local variable, too. Also think if you want it as number of bytes or number of elements. My code assumed number of bytes but your comment is number of elements; either one is good.
– rodrigo
Nov 11 '18 at 11:38
@norvd: A sensible approach would be to create a typestruct string_list char **ptr; size_t len;
and a functionstring_list_add()
that does the hard work.
– rodrigo
Nov 11 '18 at 11:39
add a comment |
ok, got it, the static int i looks like a good option for it, what if i use it like list = realloc(list, (i + 1) * sizeof(char*));?
– norvd
Nov 11 '18 at 11:35
@norvd: Usually the size of the list should be in the same place as the list itself. Ifbad
is a local variable, thenbad_len
should be a local variable, too. Also think if you want it as number of bytes or number of elements. My code assumed number of bytes but your comment is number of elements; either one is good.
– rodrigo
Nov 11 '18 at 11:38
@norvd: A sensible approach would be to create a typestruct string_list char **ptr; size_t len;
and a functionstring_list_add()
that does the hard work.
– rodrigo
Nov 11 '18 at 11:39
ok, got it, the static int i looks like a good option for it, what if i use it like list = realloc(list, (i + 1) * sizeof(char*));?
– norvd
Nov 11 '18 at 11:35
ok, got it, the static int i looks like a good option for it, what if i use it like list = realloc(list, (i + 1) * sizeof(char*));?
– norvd
Nov 11 '18 at 11:35
@norvd: Usually the size of the list should be in the same place as the list itself. If
bad
is a local variable, then bad_len
should be a local variable, too. Also think if you want it as number of bytes or number of elements. My code assumed number of bytes but your comment is number of elements; either one is good.– rodrigo
Nov 11 '18 at 11:38
@norvd: Usually the size of the list should be in the same place as the list itself. If
bad
is a local variable, then bad_len
should be a local variable, too. Also think if you want it as number of bytes or number of elements. My code assumed number of bytes but your comment is number of elements; either one is good.– rodrigo
Nov 11 '18 at 11:38
@norvd: A sensible approach would be to create a type
struct string_list char **ptr; size_t len;
and a function string_list_add()
that does the hard work.– rodrigo
Nov 11 '18 at 11:39
@norvd: A sensible approach would be to create a type
struct string_list char **ptr; size_t len;
and a function string_list_add()
that does the hard work.– rodrigo
Nov 11 '18 at 11:39
add a comment |
Your problem is a misunderstanding of sizeof(list)
. sizeof
returns the memory size of list
, i.e. the size of a char**
. It doesn't take into consideration the number of elements in your list.
Keep in mind that sizeof
isn't a function that is executed during runtime. It is a compile time unary operator.
add a comment |
Your problem is a misunderstanding of sizeof(list)
. sizeof
returns the memory size of list
, i.e. the size of a char**
. It doesn't take into consideration the number of elements in your list.
Keep in mind that sizeof
isn't a function that is executed during runtime. It is a compile time unary operator.
add a comment |
Your problem is a misunderstanding of sizeof(list)
. sizeof
returns the memory size of list
, i.e. the size of a char**
. It doesn't take into consideration the number of elements in your list.
Keep in mind that sizeof
isn't a function that is executed during runtime. It is a compile time unary operator.
Your problem is a misunderstanding of sizeof(list)
. sizeof
returns the memory size of list
, i.e. the size of a char**
. It doesn't take into consideration the number of elements in your list.
Keep in mind that sizeof
isn't a function that is executed during runtime. It is a compile time unary operator.
answered Nov 11 '18 at 11:28
Maxime ChéramyMaxime Chéramy
9,71043160
9,71043160
add a comment |
add a comment |
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%2f53248212%2fhow-to-allocate-memory-for-pointers-to-pointers%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