strcpy() function is enterning into infinite loop [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
hai i am having a problem with strcpy() function. this is related to embedded c programing.
the following is a part of code used in my project. Basic idea is to copy string(name) to an array _Items whose memory is dynamically allocated
char *_Items[100];
unsigned char contactname[36];
Memset(name,0,36);
Memset(_Items, 0, sizeof(_Items));
for(count=0; count<10 ; count++)
{
_Items[count] = (char*)malloc((strlen((char*)name)+1)*sizeof(char));
strcpy(_Items[count], (char*)name);
}
....
...function body
....
free(_Items);
In the first time call of the function the code is working fine, but in the second time call call of the function strcpy() func is entering an infinite loop.
I am not able to understand what the exact problem is. Please help me out.

did you malloc anything here ?:
char *_Items[100];
No. So why are you calling free(_Items); ?
did you malloc anything here?:
for(count=0; count<10 ; count++)
{
_Items[count] = (char*)malloc((strlen((char*)name)+1)*sizeof(char));
Yes. So why don't you call free for each item in the loop?
Calling free(_Items) tells the system to free some memory that hasn't been allocated using malloc, which is _undefined behaviour, and breaks the rest of the execution, can be anywhere (that's the "fun" of it).
Rewrite your free process:
// allocate
for(count=0; count<10 ; count++)
{
_Items[count] = malloc((strlen((char*)name)+1));
strcpy(_Items[count], (char*)name);
}
....
...function body
....
for(count=0; count<10 ; count++)
{
free(_Items[count]);
}

Related

Converting char array to upper case in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to convert a char array to upper case in C. But it still prints out in lower case. The logic in conversion function looks okay to me. I don't know if the problem could be that the input I'm passing to this function is member of a structure variable.
This is the structure whose member I'm passing to conversion function:
typedef struct AESParams {
unsigned char output[192];
} AESParams;
My conversion fucntion is shown below:
void stringUpr(char *s)
{
int i=0;
while(s[i]!='\0')
{
if(s[i]>='a' && s[i]<='z'){
s[i]=s[i]-32;
}
++i;
}
}
I call conversion function as follows:
AESParams parameters;
stringUpr(parameters.output);
If I print the output after calling "stringUpr" function, the value is still in lower case. Can anyone tell me what might be causing the issue here?
TIA...
Update: Code that writes value to output.
EVP_EncryptUpdate(&ctx, parameters->output, &outLen, input, length);
// This call happens in some other file. parameters is passed to the function that calls this function and after computation parameters is passed back to the place where I'm calling stringUpr().
I'm confident that this lines works and it gives the correct results after computation. It's just that I want to take this value and convert to upper case and write to a file.
Your program assumes that it's running using an ASCII character set, which is not guaranteed. Use the standard functions defined in ctype.h
void stringUpr(char* s)
{
int i = 0;
while(s[i] != '\0')
{
s[i++] = toupper((unsigned char)s[i]);
}
}

I can't understand an error in a function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
This function make a strange error after using it several times and I really can't understand the reason behind it.
char *get_range(char *str,int min,int max){
char *_res=(char *)malloc(sizeof(str));
int cur=0;
while (min<max){
_res[cur]=str[min];
min++;
cur++;
}
return _res;
}
The problem is that after using this function several times, the output comes with additional chars and I don't understand why.
Notice: The additional chars are allway used returned by the function beffor
char *_res=(char *)malloc(sizeof(str));
is wrong. sizeof(str) is measuring the size of a char pointer. This is either 4 or 8 (typically) depending on your system (32 or 64 bit).
You need
char *_res=(char *)malloc(strlen(str) + 1);
strlen returns the number of characters in the string, and you need to add 1 for the terminating 0;
Second you have to add a terminating zero at the end, do:
_res[cur] = '\0';
before returning

C Realloc Memory [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to memorize how much memory I allocated with realloc().
Help me
if(!array)
array=(Type*) calloc(1,sizeof(Type));
else
array=(Type*)realloc(array,(cont+1)*sizeof(Type));
array[cont].setName(....);
cont++;
It doesn't work: after firt insert, it say: Access violation
I initialized the cont = 0 in the constructor of my class and freed memory in the destructor.
See the comments added to your code:
int count=0;
if(!array)
array=(Type*) calloc(count,sizeof(Type*); // Problem:
// missing )
// use sizeof(Type)
// calling calloc with count being zero
// so you do not allocate any memory
// use 1 instead of count
array[c].setName(EditName->Text);
c++;
count++;
array=(Type*)realloc(array,count*sizeof(Type*)); // Problem:
// use sizeof(Type)
so it should look:
int count=0;
if(!array)
array=(Type*) calloc(1,sizeof(Type));
array[c].setName(EditName->Text);
c++;
count++;
array=(Type*)realloc(array,count*sizeof(Type));
The variable c must be initialized to zero before running this code
Likewise array must be nullptr before running this code
EDIT
There seem to be one more problem if you intend to run this code several times (which I assume you do).
This line:
array=(Type*)realloc(array,count*sizeof(Type));
^^^^^
Don't use count here as you always sets count to zero
The line shall be:
array=(Type*)realloc(array,c*sizeof(Type));
In general there seems to be no real use of count

For looping in C confused [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
What problem if I use strlen() in the condtion of for loop.
char s[i];
for (int i = 0; strlen(s); i++)
So if I use upper code there took a lot of time.
But if I store the value of strlen of s it took little time inspect to upper code.
What difference between these?
You should not use i < strlen(s) as a condition because the length of the string in s gets recomputed for each iteration of the loop. It is better to use a separate variable and compute the length in the initialization part:
for (size_t i = 0, len = strlen(s); i < len; i++) {
...
}
Note that your definition of s looks like a typo: char s[i];. What variable i are you referring to? what would be its value before the beginning of the for loop that defines a new i variable?
EDIT
After reformatting your code, I realized there is even more confusion:
for (int i = 0; strlen(s); i++)
This for loops iterates as long as string s is not empty. Is this your intent? Do you modify s inside the loop? s is uninitialized, the test invokes undefined behavior. Do you initialize s in code you did not post between the definition and the for loop? If you do, it would still be more efficient to write such a loop this way:
for (int i = 0; *s != '\0'; i++)
The condition is evaluated before every iteration of the loop.
C strings are just an array of characters, then a NULL. So to work out the length you have to start at the start and inspect every character from there until you find the NULL.
So in complexity terms, strlen is O(n). Your for is also O(n). If you check the strlen every time then your implementation is O(n*n). If you work it out once in adavance then yours os O(n). Try it with longer ss to see a much bigger difference.

Incrementation doesn't go further than 0x01 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
So I've got functions:
int f1(uint8_t* a, int b)
{
for(int i = 0; i < b; i++)
f2(&a[i]);
return 1;
}
static void f2(uint8_t* a)
{
REG1 = *a;
...
*a = REG2;
}
"a" is a data register that is used as a buffer to put the data all the way from the main loop into the target function f2() through different wrapper functions.
"REG1" and "REG2" are I/O registers into and out of which the data is passed with "a". The value of "REG2" implicitly changes while in f2() through hardware operations.
When I watch the variable changes in debug mode, the following happens:
b = 2
declare i = 0, i < b
call f2
increment i to i = 1, i < b
call f2
increment i to i = 0, i < b
And since "i" never reaches 2, the loop never ends. The program is compiled and debugged with IAR EW for AVR. The optimisation for the compiler is turned off.
As #Lundin says, it seems that memory corruption bug.
If you can use debugger, try inserting write-watchpoint on i. It might help you ^o^
(When thinking about only these codes, I can assume that a is corrupted, and *a = REG2; might write on stack, and eventually i is changed)
#Lundin is correct, I was passing an address of a pointer instead of the pointer itself with:
f2(&a[i]);

Resources