Bit wise or of two Unsigned Integer (Program Crashes) [closed] - c

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Sorry if my problem is not very clear.
I've a structure like this:
typedef struct
{
uint32_t typeSet;
}DataTypeTagInfo;
The following function is for unifying two typeSet:
DataTypeTagInfo* unifyTagInfo(DataTypeTagInfo* tag1, DataTypeTagInfo* tag2){
if(tag1 == NULL) return tag2;
else if(tag2 == NULL) return tag1;
tag1->typeSet |= tag2->typeSet;
return tag1;
}
The program exits while executing the following line:
tag1->typeSet |= tag2->typeSet;
On a sample run I've following value:
tag1->typeSet = 3917954189
tag2->typeSet = 2536589
There is no error message. Just quits. Please help.

The code you show is perfectly sound. Consequently, it's very likely that either tag1 or tag2 is an invalid pointer at the time of the abort. This will have nothing to do with the code you've posted. The pointers could be set invalid in many, many ways.
To figure out what's happening, I'd start with a careful review of the code setting tag1 and tag2 at the call site and then - if the answer does not appear - move on to using valgrind to check for memory overwrite errors.
NB this what makes C(++) so challenging.

Related

After I use malloc, program suddenly asks for input [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm a newbie programmer
//allocating space for final output
//text_count is given by another function, let's just say i have a value for it
//out is my typedef struct
out *fin[text_count];
for(i=0; i<text_count; i++){
fin[i] = malloc(sizeof(out));
}
//this is a test
fin[1]->appearances = 1;
printf("%d",fin[1]->appearances);
// ..other code
I noticed that after this allocation, program suddenly outputs this:
1_
The underscore is blinking, indicating it needs an input.
What can be my problem here? Is it the allocation? Or the codes down below?
okay sorry, let me clarify this, im using codeblocks . so when i get a blinking cursor it means it needs an input. and after i got this output, an infinite loop of inputs seems to be happening.
You're simply seeing the text terminal's cursor. It may be blinking or it may not, that doesn't mean anything in itself.
You might want to add a linefeed:
printf("%d\n", fi[1]->appearances);
to get the output on a line of its own.
A cursor after your printf does not mean, that it is waiting for input.
You can confirm this, by using strace, if it is waiting at read syscall.
Usage:
strace ./myApplication arg1 arg2...

printf to prevent segmentation fault in c [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
The following is a part of a function.
l->length[l->cl] = atoi(p->wds[p->cw]);
printf("FD %d\n", l->length[l->cl]);
p->cw = p->cw + 1;
l->cl = l->cl + 1;
printf("prevent from seg fault\n");
instr(p);
If I remove the printf("prevent from seg fault\n"); I get segmentation fault, but if I keep it it runs and go to the nest function and so on.
So my question is just in general why does this happen, I know I am not showing enough of my code so you can see why the segmentation happen, but if there is any general explanation for this, I also had this in another place in my program but at the end I could remove it?
Regards Orri
As far as the standard is concerned any changes made to a program that invokes undefined behaviour can have any result - there doesn't have to be a logic behind it. And in fact it is very seldom fruitful to try to reason about how a given implementation behaves when encountering undefined behaviour.
That said if a call to printf (or any other function) changes the behaviour of your program (beyond the obvious change of printing what it's supposed to, of course), one possible explanation is that you have an invalid pointer somewhere that points to a local variable that's out of scope. If that variable previously lived in the stack memory that's now used by printf for its own local variables, that means the memory that the pointer points to will now be overridden with a new value. And that can of course change the behaviour of any code that use the pointer.

Output from printf does not show in cygwin terminal? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I wrote a very small program called cow.c. When I try to run it from the cygwin terminal after compiling, I get no output. There are no errors on compile.
#include <stdio.h>
int main() {
printf("%s\n", "COW?");
return(0);
}
EDIT: I have a feeling that the problem is with cygwin...are there any steps I need to take to ensure that output is printed to the cygwin terminal instead of lost in the void?
Run:
gcc cow.c -o app
Then type:
./app
This should work.

Why does char * malloc give me a seg fault with scanf? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
char * input = (char*)malloc(256);
scanf("%s", input);
Seg fault. Why? I have a lot of trouble with strings in C, but I feel like everything is done right here. I even followed an example from a 100k+ SO user to the letter.
The bug could be caused by:
malloc() failing to allocate any memory, check the result against NULL.
by a buffer overrun from scanf, more than 256 characters.
by forgetting to include <stdlib.h> and then typecasting the result of malloc. More info here.
You need to ensure your buffer is large enough, including enough space for the trailing NUL (0 byte) at the end of the string. Otherwise, you get a buffer overflow that may lead to a segfault.

UAL Assembly from C to UAL assembly [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
Rewrite the following program in UAL assembly, given its C code. Assume that gcd()
and print() are ABI compliant functions that calculate the greatest common divider
and print the variable respectively. Make sure to annotate your assembly code
int main() {
uint32_t a=0x5, b, i;
b = 4*a;
while(i<10) {
b = gcd(a, b);
i++;
}
print(b);
}
this is for a pre-lab that i am trying to do ... thank you
This does not look like a prelab. It does however look like problem 1 (worth 20 points) that was just assigned in Embedded Systems. You should take the time to learn assembly and the equivalent in C. You will struggle in the class otherwise, since Stack Overflow wont help you on your upcoming exam.... Join a study group if you are having problems with assembly, there are other student in the class dealing with the same issues as you. There is almost always students in the lab as well.
Also, UAL is a ARM Assembly syntax see link:
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0473c/BABJIHGJ.html

Resources