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]);
Related
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]);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
1.
int main() { int a; a = 0; }
//OR
2.
#define ZERO 0 int main() { int a; a = ZERO; }
I think that the second one takes less execution time because it is preprocesssed.
The one liner - compilation time and execution time are different metrics altogether!!
Any program, written in mid to high level language, is not executed as-is by the system. For C, it is the compiler, which takes the source file as input and produces the binary (machine-executable code). In the process, it incorporates a lot of optimization to the code.
In this case, it's most likely, that the compiler will optimize out the statements altogether, as they are not used meaningfully. Both the snippets are likely to generate the same binary (unless you forcefully turn off the optimization). If at all, you have to check the generated binary and perform the time-measurement to check the final result.
They are equal. The compiler's preprocessor just replaces ZERO before doing anything.
Assuming it is written like this:
#define ZERO 0
int main() { int a; a = ZERO; }
The program will be transformed to
int main() { int a; a = 0; }
before it is compiled and it will not reach optimization step.
Update:
Just to clarify, sometimes you see codes like this:
#define ERROR_MEMORY_ALLOCATION_FAILED -1
char *buf = malloc(10);
if (!buf)
return ERROR_MEMORY_ALLOCATION_FAILED;
This can help code readability a lot, especially when you reuse these definitions.
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 declared a two dimensional array as row and col and I left the value empty, but when I check the value in each array position, some of the indexes contain some weird number.
You are looking at uninitilized memory. It can have whatever which value to it. You should never trust the value of a variable you haven't initialized.
Access of an uninitialized value is undefined behavior. You can solve your problem quite simply by initializing the array to all zero to begin with, e.g.
int seatNo[5][5] = {{0}};
Now any subsequent access to any of the elements of seatNo will succeed because each element has been initialized to zero. As a rule, especially when you are learning C, you will save yourself grief if you simply initialize ALL your variables.
(you will also want to turn warnings on, e.g. -Wall -Wextra, at minimum, so your compiler will warn you when a variable may be uninitialized)
#include <stdio.h>
int main (void) {
int seatNo[5][5] = {{0}};
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
printf (" seatNo[%d][%d] = %d\n", i, j, seatNo[i][j]);
return 0;
}
Example
$ ./bin/initarray
seatNo[0][0] = 0
seatNo[0][1] = 0
seatNo[0][2] = 0
seatNo[0][3] = 0
seatNo[0][4] = 0
seatNo[1][0] = 0
seatNo[1][1] = 0
<snip>
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My teacher gave us a question in programming class today and I don't understand how he got the answer. I was hoping someone could explain it to me. We basically have to show what the programs output would be, however I am somewhat confused as to how to get the answer to the question. The Question is as follows:
#include <stdio.h>
void do_something (int , int * );
int main (void)
{
int first = 1, second = 2 ;
do_something(second, &first);
printf("%4d%4d\n", first, second);
return (0);
}
void do_something (int thisp, int *that)
{
int the_other;
the_other = 5;
thisp = 2 + the_other;
*that = the_other * thisp;
return;
}
Answer
35 and 2
The function do_something contains 2 parameters.
normal integer (thisp)
pointer to an integer. (that)
What your teacher wanted you to learn is, pass by value and pass by address.
In pass by value, the original value doesn't change. This is because in the example given.
value of variable second is copied thisp variable.
In pass by address, the original value can be modified within the function.
This is because, the pointer that is pointing to the location of variable first. So if value of that is changed, the value of first will also change.
This is why, value of first is changed in the output and value of second is unaffected.
thisp = 2 + the_other;
*that = the_other * thisp;
Means:
thisp = 2 + 5
*that = 5 * 7
And that contains address of first in main which is overwritten in do_something as 35. Second remains 2.