Hi,
I would like to declare a global pointer. I have started with this very simple code, which does not work. I am using Xcode on a Mac running Tiger (10.4)
double *F;
int main () {
double *G;
// this part works just fine
*G = 4.0;
printf("G=%f\n",*G);
// at this line, my program crashes due to:
// FEApackageC has exited due to signal 10 (SIGBUS).
*F = 3.0;
printf("F=%f\n",*F)
return 0;
}
Ultimately, the task I have in mind is more complicated than what I show above, because I will need to dynamically allocate some memory in this big structure. However, I can't even seem to get this to work.
Thanks,
Andy
6 8797
Sorry, when posting my code, I forgot the #include<stdio.h> at the top. This was not the reason for my code failing, I just forgot to cut and paste it in.
I see you have pointers to double but I don't see the doubles they point to.
Unless you point to a double, your pointer has a garbage address and if you use that address and drop a 4.0 there, I'm not surpised your code crashes.
I am also surprised your compiler did not warn you about using uninitialized pointers.
What about my line "*F = 3.0;" That is where I assign my pointer to a double, isn't it? It works fine for the local pointer *G.
I see you have pointers to double but I don't see the doubles they point to.
Unless you point to a double, your pointer has a garbage address and if you use that address and drop a 4.0 there, I'm not surpised your code crashes.
I am also surprised your compiler did not warn you about using uninitialized pointers.
Again, where is the double?
I expect to see: -
-
double data;
-
-
double* F = & data;
-
-
*F = 3.0;
-
-
Thank so much for your help! I knew you had to allocate memory for arrays, but for some reason, I thought if you declared a pointer, the program automatically allocated an address for it. Thanks again, I think I should be fine, now.
Best Regards,
Andy
You are correct. When you define a pointer the compiler allocates memory for it. However, the address inside the pointer is garbage. The compiler does not create the variable to point at. You have to do that yourself.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Matt |
last post by:
Greetings,
What are people's thoughts on global variables in C++?
Why are we taught not to use them in programming?
Is it true that if you are running two copies of the C program one
copy can...
|
by: whisper |
last post by:
My question is whether it is better to use a global variable to hold
a dynamically malloced multidim array or pass around pointers to it.
The details are below (forgive the long winded explanation)...
|
by: Sunil |
last post by:
Hi all,
I want to know how good or bad it is using global variables i.e
advantages
and disadvantages of using global variables.
Sunil.
|
by: zeecanvas |
last post by:
Hi,
First of all: Yes, I know global variables are bad, but I've a huge
amount of legacy code, and I've to maintain it _as_is_.
I'm maintaining a big program. I moved all (program-wide scope)...
|
by: eoindeb |
last post by:
Sorry to ask another global variable question, but from reading other
posts I'm still not sure whether to use them or not.
I have a program with a set function that calls 4 other functions in...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |