473,320 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

global pointer variables in C++

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
Apr 1 '08 #1
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.
Apr 1 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 1 '08 #3
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.
Apr 1 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
Again, where is the double?

I expect to see:

Expand|Select|Wrap|Line Numbers
  1.  
  2. double data;
  3.  
  4. double* F = & data;
  5.  
  6. *F = 3.0;
  7.  
  8.  
Apr 1 '08 #5
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
Apr 1 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 1 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

10
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...
35
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)...
13
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.
7
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)...
37
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
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...
0
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.