XCode EXC_BAD_ACCESS by Changing Variable Name - c

Take this simple program and run it in Xcode:
#include <stdio.h>
#include "inttypes.h"
struct {
int32_t nwgts;
;
} clbint_;
#define clbint_1 clbint_
int main()
{
printf("clbint_1.nwgts: %d\n",clbint_1.nwgts);
clbint_1.nwgts=1; //Crash here
printf("clbint_1.nwgts: %d\n",clbint_1.nwgts);
return 0;
}
And when you run it you will get a crash on the line indicated that just says Thread 1: EXC_BAD_ACCESS (code=2, address=0x10078e18c). But if you change clbint_ to clbinta_ like this:
#include <stdio.h>
#include "inttypes.h"
struct {
int32_t nwgts;
;
} clbinta_;
#define clbint_1 clbinta_
int main()
{
printf("clbint_1.nwgts: %d\n",clbint_1.nwgts);
clbint_1.nwgts=1;
printf("clbint_1.nwgts: %d\n",clbint_1.nwgts);
return 0;
}
The results print exactly as I expect:
clbint_1.nwgts: 0
clbint_1.nwgts: 1
I am trying to understand why this is happening, clearly it has something to do with the variable name clbint_ but why does this behavior occur?

Related

How can I include a variable in remove() function? I'm trying to include an environment variable in the function

I'm attempting to learn the remove() function in C, and I want to make a program with first gets the environmental variable with getenv() function, then uses it inside the code.
However, I get the error
"too many arguments to function remove()".
#include <stdio.h>
#include <stdlib.h>
int main()
{
char* a = getenv("USERPROFILE");
remove("%s/Desktop/remove.txt", a);
return 0;
}
If you are simply trying to combine them this should work.
I commented out some lines and put a printf so you can run it to see the result.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXPATH 256
int main()
{
//char* a = getenv("USERPROFILE");
char* a = "userprofile";
char path[MAXPATH];
strcpy(path,a);
strcat(path,"/Desktop/remove.txt");
//remove(path);
printf("PATH: %s",path);
return 0;
}

How to play sound effect or Music in C?

I am making a game and I have to add some sounds effects and Music.
I Googled it and I found The flowing Code:
#include <conio.h>
#include "inc/fmod.h"
FSOUND_SAMPLE* handle;
int main ()
{
// init FMOD sound system
FSOUND_Init (44100, 32, 0);
// load and play mp3
handle=FSOUND_Sample_Load (0,"my.mp3",0, 0, 0);
FSOUND_PlaySound (0,handle);
// wait until the users hits a key to end the app
while (!_kbhit())
{
}
// clean up
FSOUND_Sample_Free (handle);
FSOUND_Close();
}
But when I compile it I got the flowing error:
➜ Desktop gcc main.c
main.c:1:10: fatal error: 'conio.h' file not found
#include <conio.h>
^~~~~~~~~
1 error generated.
Well, firstly <conio.h> is a C++ library and you're programming in C. It's different!
Then, I remember a C code I wrote years ago, main.c has got the following code (comments are in italian because I am italian):
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "header.h"
int main(){
register unsigned char x='2';
printf("digitare tasti:\n");
while(1){
while(1){
if(x=='2'){/*blocco2*/ while(x!='1' && x!='3'){x=getch(); scala2(x);}}
if(x=='1'){/*blocco1*/ while(x!='2' && x!='3'){x=getch(); scala1(x);}}
if(x=='3'){/*blocco3*/ while(x!='1' && x!='2'){x=getch(); scala3(x);}}
}
}
system("PAUSE");
return 0;
}
Then, this is the other source file, called file.c:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "header.h"
void scala1(unsigned char x){
if(x=='a')beep(131,50);
if(x=='s')beep(147,50);
if(x=='d')beep(165,50);
if(x=='f')beep(175,50);
if(x=='g')beep(196,50);
if(x=='h')beep(220,50);
if(x=='j')beep(247,50);
if(x=='k')beep(262,50);
if(x=='l')beep(294,50);
if(x=='w')beep(139,50);
if(x=='e')beep(156,50);
if(x=='r')beep(185,50);
if(x=='t')beep(208,50);
if(x=='y')beep(233,50);
}
void scala2(unsigned char x){
if(x=='a')beep(262,50);
if(x=='s')beep(294,50);
if(x=='d')beep(330,50);
if(x=='f')beep(349,50);
if(x=='g')beep(392,50);
if(x=='h')beep(440,50);
if(x=='j')beep(494,50);
if(x=='k')beep(523,50);
if(x=='l')beep(587,50);
if(x=='w')beep(277,50);
if(x=='e')beep(311,50);
if(x=='r')beep(370,50);
if(x=='t')beep(415,50);
if(x=='y')beep(466,50);
}
void scala3(unsigned char x){
if(x=='a')beep(523,50);
if(x=='s')beep(587,50);
if(x=='d')beep(659,50);
if(x=='f')beep(698,50);
if(x=='g')beep(784,50);
if(x=='h')beep(880,50);
if(x=='j')beep(988,50);
if(x=='k')beep(1046,50);
if(x=='l')beep(1175,50);
if(x=='w')beep(554,50);
if(x=='e')beep(622,50);
if(x=='r')beep(740,50);
if(x=='t')beep(831,50);
if(x=='y')beep(932,50);
}
The last one, the file header.h. It's code is the following one:
void scala1(unsigned char x);
void scala2(unsigned char x);
void scala3(unsigned char x);
All the source files must be in the same directory. You compile main.c and then, you just need to press a,s,d,..y and 1,2,3. Try! It works, of course if you want to change part of the code, you can do. I hope you enjoy my program, it's funny :)

Macro behavior in function

I expected output for the below program as 10 20 but it is 10 10.
#include <stdio.h>
#define i 10
int main()
{
printf("%d\t",i);
fun();
printf("%d",i);
return 0;
}
fun(){
#undef i
#define i 20
}
If I assume like if a function call fun() returned back to main() then the original i value is printed then again I am wrong by looking at the output the below program,
#include <stdio.h>
#define i 10
fun(){
#undef i
#define i 20
}
int main()
{
printf("%d\t",i);
fun();
printf("%d",i);
return 0;
}
Expected output: 10 20 but the output is: 20 20
Can anybody please explain me the behaviour?
The #define is a preprocessor MACRO. The value is substituted at compile time instead of runtime.
So, the processing happens as per the presence (sequence) of the #defines. That means, you cannot expect the #undef and #define to work on runtime.
To elaborate, your case 1 code looks like
#include <stdio.h>
#define i 10
int main()
{
printf("%d\t",10);
fun();
printf("%d",10);
return 0;
}
fun(){
#undef i
#define i 20
}//now i is 20, but no one is using it, at compile time
and, your second code looks like
#include <stdio.h>
#define i 10
fun(){
#undef i
#define i 20 // i get a new definition here
}
int main()
{
printf("%d\t",20);
fun();
printf("%d",20);
return 0;
}
A note: The recommended signature of main() is int main(void).
One of the very first steps when compiling is to replace all the PREPROCESSING TOKENS with their value. So the evaluation is done at compile time, not at run-time.
So what you get for your first example is:
#include <stdio.h>
int main()
{
printf("%d\t",10); // we have only seen define i 10 until now
fun();
printf("%d",10); // we have only seen define i 10 until now
return 0;
}
fun(){
// the two in here would have made any i after this location be replaced with 20
}
And similar for your second case.

C program compiles but no output

I am trying to learn creating header file in C and including it in my main.c func() . I created a simple tut1.c file with function named call() and a tut1.h header file which externs tut1.c function named call(). Thats it, now i am using eclipse Juno for C/C++ on linux fedora. I dont get any compile error but the code wont output? I tried on console and eclipse in vain. Can you check please? Thanks
---main.c-----
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "tut1.h"
int main (void)
{
int tut1(void);
return 0;
}
-----tut1.c------
#include <stdio.h>
#include <stdlib.h>
#include "tut1.h"
int call (void)
{
int *ptr;
int i;
ptr = &i;
*ptr = 10;
printf ("%d we are printing the value of &i\n", &i);
printf ("%d we are printing the value of *ptr\n", *ptr);
printf ("%d we are printing the value of ptr\n", ptr);
printf ("%d we are printing the value of &ptr\n", &ptr);
getchar();
return 0;
}
----tut1.h----
#ifndef TUT1_H_
#define TUT1_H_
extern int call (void);
#endif
You're not seeing anything because you're not calling the call() function from your main() function.
The main() function is the default entry point when you run the program, i.e. the first function that gets called during execution.
To execute the function call() you would need to call this from main() as follows :
int main (void)
{
int result = call();
return 0;
}
BTW, this line int tut1(void); within your main() just declares a function, which you do not seem to have defined anywhere. So I have removed it in the above shown code.

newbie print struct C programming question

I am new in C programming, and trying to create simple code below for printing a struct member using other function.
I do not understand this, as in the function funct_to_print_value, I already declare the struct variable "car", and I believe what I need is just to print is using (dot) notation to access it. Appereantly not, as I got the error above. Does anyone can share their knowledge, how I can print the value of buyer, and what mistake I had done above?
Thank you ..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct slot_car {
int buyer;
} slot_car;
int main() {
slot_car car;
memset(&car, 0, sizeof(car));
car.buyer = 1;
printf("value of car is .. %d\n", car.buyer);
funct_to_print_value();
printf("end of function..\n");
return 0;
}
int funct_to_print_value()
{
printf("you are in printlist function..\n");
slot_car car;
printf("value of car inside is %d\n", car.buyer);
return 1;
}
Since you declared car inside each function separately, they are separate (local) variables. You probably want to pass it from main to funct_to_print_value as a parameter instead. The warning is strange, but it is possible that the compiler detected the uninitiated value and gave this message because it is first used in printf.
This looks fine to me. It would be helpful to have more information. All I did was extract your example into a temp.c file and compile it using gcc -c temp.c. There were no errors.
On which OS is this?
Which build environment/compiler is this?
How are you building this? (commands used in the build environment)
I'm using gcc 4.4.3 on Ubuntu Linux 10.04.
Edit 1:
What happens if you cast car.buyer to an int?
printf("value of car is .. %d\n", (int ) car.buyer);
Edit 2:
How about this to print your 1?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct slot_car {
int buyer;
} slot_car;
int main() {
slot_car car;
memset(&car, 0, sizeof(car));
car.buyer = 1;
printf("value of car is .. %d\n", car.buyer);
{
int temp_ret;
temp_ret = funct_to_print_value();
printf("end of function..%d\n",temp_ret);
}
return 0;
}
int funct_to_print_value()
{
printf("you are in printlist function..\n");
slot_car car;
printf("value of car inside is %d\n", car.buyer);
return 1;
}
value of car is .. 1
you are in printlist function..
value of car inside is 134514096
end of function..1
cnorton#steamboy:~/scratch$

Resources