C : char ** dynamic allocation -> [closed]Simple Linux char driverChecking for palindromes using dynamic...
Why can Carol Danvers change her suit colours in the first place?
The probability of Bus A arriving before Bus B
It grows, but water kills it
Has any country ever had 2 former presidents in jail simultaneously?
Is this toilet slogan correct usage of the English language?
Non-trope happy ending?
How could a planet have erratic days?
Terse Method to Swap Lowest for Highest?
Biological Blimps: Propulsion
What is going wrong in this circuit which supposedly should step down AC to arduino friendly voltage?
Are Captain Marvel's powers affected by Thanos' actions in Infinity War
Why is so much work done on numerical verification of the Riemann Hypothesis?
Creepy dinosaur pc game identification
How to explain what's wrong with this application of the chain rule?
Is it better practice to read straight from sheet music rather than memorize it?
Is preaching recommended or mandatory to a temple priest?
Is it improper etiquette to ask your opponent what his/her rating is before the game?
What is Cash Advance APR?
Pre-mixing cryogenic fuels and using only one fuel tank
Are paving bricks differently sized for sand bedding vs mortar bedding?
Why did the EU agree to delay the Brexit deadline?
What was this official D&D 3.5e Lovecraft-flavored rulebook?
A social experiment. What is the worst that can happen?
Did arcade monitors have same pixel aspect ratio as TV sets?
C : char ** dynamic allocation -> [closed]
Simple Linux char driverChecking for palindromes using dynamic memory allocationInitializing and printing an array using pointers and dynamic allocation in CDynamic array of integersString “Contains” char functionRead file into char*Allocation and reallocation of memoryDynamic Allocation and Reallocation of MemoryC Implementation of dynamic mem allocation using first-fit algoMacro for allocation in C
$begingroup$
I get the following error, why?
* Error in `parser': free(): invalid next size (fast): 0x000000000129b630 *
int numErr = 0;
char ** err;
char * token;
int main() {
int i;
token = malloc(sizeof(char *));
err = malloc(sizeof(char *));
for(i = 0; i < 20; i++) {
memset(token, '', strlen(token) + 1);
sprintf(token, "hello%d", i);
func();
}
for(i = 0; i < numErr; i++) {
printf("%sn", err[i]);
free(err[i]);
}
free(err);
}
void func() {
numErr++;
err = realloc(err, numErr * sizeof(char *));
err[numErr-1] = (char *) malloc(strlen(token) + 1);
memset(err[numErr-1], '', strlen(token) + 1);
strcpy(err[numErr-1], token);
}
I re-wrote everything because I have too many lines of code to just paste what my program has and apparently a lot of people don't read comments before saying something is broken.
SO this simple program should accomplish the same thing my program does. What is wrong with this allocation/free?
Thanks
c
$endgroup$
closed as off-topic by user673679, Zeta, 1201ProgramAlarm, πάντα ῥεῖ, Mast Mar 14 at 17:40
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – user673679, Zeta, 1201ProgramAlarm, πάντα ῥεῖ, Mast
If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 1 more comment
$begingroup$
I get the following error, why?
* Error in `parser': free(): invalid next size (fast): 0x000000000129b630 *
int numErr = 0;
char ** err;
char * token;
int main() {
int i;
token = malloc(sizeof(char *));
err = malloc(sizeof(char *));
for(i = 0; i < 20; i++) {
memset(token, '', strlen(token) + 1);
sprintf(token, "hello%d", i);
func();
}
for(i = 0; i < numErr; i++) {
printf("%sn", err[i]);
free(err[i]);
}
free(err);
}
void func() {
numErr++;
err = realloc(err, numErr * sizeof(char *));
err[numErr-1] = (char *) malloc(strlen(token) + 1);
memset(err[numErr-1], '', strlen(token) + 1);
strcpy(err[numErr-1], token);
}
I re-wrote everything because I have too many lines of code to just paste what my program has and apparently a lot of people don't read comments before saying something is broken.
SO this simple program should accomplish the same thing my program does. What is wrong with this allocation/free?
Thanks
c
$endgroup$
closed as off-topic by user673679, Zeta, 1201ProgramAlarm, πάντα ῥεῖ, Mast Mar 14 at 17:40
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – user673679, Zeta, 1201ProgramAlarm, πάντα ῥεῖ, Mast
If this question can be reworded to fit the rules in the help center, please edit the question.
$begingroup$
OT: regarding:token = malloc(sizeof(char *));
when calling any of the heap allocation functions:malloc()
calloc()
realloc()
, always check (!=NULL) the returned value to assure the operation was successful.
$endgroup$
– user3629249
Mar 14 at 17:17
$begingroup$
regarding:memset(token, '', strlen(token) + 1);
The char array:token[]
contains nothing but garbage at this point in the code, so there is no assurance of a NUL byte at any known location. the result is undefined behavior and can lead to a seg fault event. Suggest removing that statement and callingcalloc()
rather thanmalloc()
earlier in the code
$endgroup$
– user3629249
Mar 14 at 17:20
1
$begingroup$
Welcome to Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing your code. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– Zeta
Mar 14 at 17:22
$begingroup$
OT: regarding:err[numErr-1] = (char *) malloc(strlen(token) + 1);
1) the returned type isvoid*
which can be assigned to any pointer. Casting just clutters the code, making it more difficult to understand, debug, etc Suggest removing the cast.
$endgroup$
– user3629249
Mar 14 at 17:22
$begingroup$
Strongly suggest moving this question tostackoverflow.com
as that site is for fixing code
$endgroup$
– user3629249
Mar 14 at 17:23
|
show 1 more comment
$begingroup$
I get the following error, why?
* Error in `parser': free(): invalid next size (fast): 0x000000000129b630 *
int numErr = 0;
char ** err;
char * token;
int main() {
int i;
token = malloc(sizeof(char *));
err = malloc(sizeof(char *));
for(i = 0; i < 20; i++) {
memset(token, '', strlen(token) + 1);
sprintf(token, "hello%d", i);
func();
}
for(i = 0; i < numErr; i++) {
printf("%sn", err[i]);
free(err[i]);
}
free(err);
}
void func() {
numErr++;
err = realloc(err, numErr * sizeof(char *));
err[numErr-1] = (char *) malloc(strlen(token) + 1);
memset(err[numErr-1], '', strlen(token) + 1);
strcpy(err[numErr-1], token);
}
I re-wrote everything because I have too many lines of code to just paste what my program has and apparently a lot of people don't read comments before saying something is broken.
SO this simple program should accomplish the same thing my program does. What is wrong with this allocation/free?
Thanks
c
$endgroup$
I get the following error, why?
* Error in `parser': free(): invalid next size (fast): 0x000000000129b630 *
int numErr = 0;
char ** err;
char * token;
int main() {
int i;
token = malloc(sizeof(char *));
err = malloc(sizeof(char *));
for(i = 0; i < 20; i++) {
memset(token, '', strlen(token) + 1);
sprintf(token, "hello%d", i);
func();
}
for(i = 0; i < numErr; i++) {
printf("%sn", err[i]);
free(err[i]);
}
free(err);
}
void func() {
numErr++;
err = realloc(err, numErr * sizeof(char *));
err[numErr-1] = (char *) malloc(strlen(token) + 1);
memset(err[numErr-1], '', strlen(token) + 1);
strcpy(err[numErr-1], token);
}
I re-wrote everything because I have too many lines of code to just paste what my program has and apparently a lot of people don't read comments before saying something is broken.
SO this simple program should accomplish the same thing my program does. What is wrong with this allocation/free?
Thanks
c
c
edited Mar 14 at 17:20
Casey Bowden
asked Mar 14 at 17:04
Casey BowdenCasey Bowden
11
11
closed as off-topic by user673679, Zeta, 1201ProgramAlarm, πάντα ῥεῖ, Mast Mar 14 at 17:40
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – user673679, Zeta, 1201ProgramAlarm, πάντα ῥεῖ, Mast
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by user673679, Zeta, 1201ProgramAlarm, πάντα ῥεῖ, Mast Mar 14 at 17:40
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – user673679, Zeta, 1201ProgramAlarm, πάντα ῥεῖ, Mast
If this question can be reworded to fit the rules in the help center, please edit the question.
$begingroup$
OT: regarding:token = malloc(sizeof(char *));
when calling any of the heap allocation functions:malloc()
calloc()
realloc()
, always check (!=NULL) the returned value to assure the operation was successful.
$endgroup$
– user3629249
Mar 14 at 17:17
$begingroup$
regarding:memset(token, '', strlen(token) + 1);
The char array:token[]
contains nothing but garbage at this point in the code, so there is no assurance of a NUL byte at any known location. the result is undefined behavior and can lead to a seg fault event. Suggest removing that statement and callingcalloc()
rather thanmalloc()
earlier in the code
$endgroup$
– user3629249
Mar 14 at 17:20
1
$begingroup$
Welcome to Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing your code. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– Zeta
Mar 14 at 17:22
$begingroup$
OT: regarding:err[numErr-1] = (char *) malloc(strlen(token) + 1);
1) the returned type isvoid*
which can be assigned to any pointer. Casting just clutters the code, making it more difficult to understand, debug, etc Suggest removing the cast.
$endgroup$
– user3629249
Mar 14 at 17:22
$begingroup$
Strongly suggest moving this question tostackoverflow.com
as that site is for fixing code
$endgroup$
– user3629249
Mar 14 at 17:23
|
show 1 more comment
$begingroup$
OT: regarding:token = malloc(sizeof(char *));
when calling any of the heap allocation functions:malloc()
calloc()
realloc()
, always check (!=NULL) the returned value to assure the operation was successful.
$endgroup$
– user3629249
Mar 14 at 17:17
$begingroup$
regarding:memset(token, '', strlen(token) + 1);
The char array:token[]
contains nothing but garbage at this point in the code, so there is no assurance of a NUL byte at any known location. the result is undefined behavior and can lead to a seg fault event. Suggest removing that statement and callingcalloc()
rather thanmalloc()
earlier in the code
$endgroup$
– user3629249
Mar 14 at 17:20
1
$begingroup$
Welcome to Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing your code. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– Zeta
Mar 14 at 17:22
$begingroup$
OT: regarding:err[numErr-1] = (char *) malloc(strlen(token) + 1);
1) the returned type isvoid*
which can be assigned to any pointer. Casting just clutters the code, making it more difficult to understand, debug, etc Suggest removing the cast.
$endgroup$
– user3629249
Mar 14 at 17:22
$begingroup$
Strongly suggest moving this question tostackoverflow.com
as that site is for fixing code
$endgroup$
– user3629249
Mar 14 at 17:23
$begingroup$
OT: regarding:
token = malloc(sizeof(char *));
when calling any of the heap allocation functions: malloc()
calloc()
realloc()
, always check (!=NULL) the returned value to assure the operation was successful.$endgroup$
– user3629249
Mar 14 at 17:17
$begingroup$
OT: regarding:
token = malloc(sizeof(char *));
when calling any of the heap allocation functions: malloc()
calloc()
realloc()
, always check (!=NULL) the returned value to assure the operation was successful.$endgroup$
– user3629249
Mar 14 at 17:17
$begingroup$
regarding:
memset(token, '', strlen(token) + 1);
The char array: token[]
contains nothing but garbage at this point in the code, so there is no assurance of a NUL byte at any known location. the result is undefined behavior and can lead to a seg fault event. Suggest removing that statement and calling calloc()
rather than malloc()
earlier in the code$endgroup$
– user3629249
Mar 14 at 17:20
$begingroup$
regarding:
memset(token, '', strlen(token) + 1);
The char array: token[]
contains nothing but garbage at this point in the code, so there is no assurance of a NUL byte at any known location. the result is undefined behavior and can lead to a seg fault event. Suggest removing that statement and calling calloc()
rather than malloc()
earlier in the code$endgroup$
– user3629249
Mar 14 at 17:20
1
1
$begingroup$
Welcome to Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing your code. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– Zeta
Mar 14 at 17:22
$begingroup$
Welcome to Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing your code. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– Zeta
Mar 14 at 17:22
$begingroup$
OT: regarding:
err[numErr-1] = (char *) malloc(strlen(token) + 1);
1) the returned type is void*
which can be assigned to any pointer. Casting just clutters the code, making it more difficult to understand, debug, etc Suggest removing the cast.$endgroup$
– user3629249
Mar 14 at 17:22
$begingroup$
OT: regarding:
err[numErr-1] = (char *) malloc(strlen(token) + 1);
1) the returned type is void*
which can be assigned to any pointer. Casting just clutters the code, making it more difficult to understand, debug, etc Suggest removing the cast.$endgroup$
– user3629249
Mar 14 at 17:22
$begingroup$
Strongly suggest moving this question to
stackoverflow.com
as that site is for fixing code$endgroup$
– user3629249
Mar 14 at 17:23
$begingroup$
Strongly suggest moving this question to
stackoverflow.com
as that site is for fixing code$endgroup$
– user3629249
Mar 14 at 17:23
|
show 1 more comment
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
OT: regarding:
token = malloc(sizeof(char *));
when calling any of the heap allocation functions:malloc()
calloc()
realloc()
, always check (!=NULL) the returned value to assure the operation was successful.$endgroup$
– user3629249
Mar 14 at 17:17
$begingroup$
regarding:
memset(token, '', strlen(token) + 1);
The char array:token[]
contains nothing but garbage at this point in the code, so there is no assurance of a NUL byte at any known location. the result is undefined behavior and can lead to a seg fault event. Suggest removing that statement and callingcalloc()
rather thanmalloc()
earlier in the code$endgroup$
– user3629249
Mar 14 at 17:20
1
$begingroup$
Welcome to Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing your code. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– Zeta
Mar 14 at 17:22
$begingroup$
OT: regarding:
err[numErr-1] = (char *) malloc(strlen(token) + 1);
1) the returned type isvoid*
which can be assigned to any pointer. Casting just clutters the code, making it more difficult to understand, debug, etc Suggest removing the cast.$endgroup$
– user3629249
Mar 14 at 17:22
$begingroup$
Strongly suggest moving this question to
stackoverflow.com
as that site is for fixing code$endgroup$
– user3629249
Mar 14 at 17:23