|
-
March 17th, 2009, 01:07 AM
#1
Tricky Issue Involving Pointers and Segfaults
Hello,
I have the following very bizarre problem. I have written some code I have to integrate with a larger part of C++ programming. This program has some sketchy segfault issues. In particular, the program has to declare an array:
randomField = new double [N];
If however I add the line
double * randomfield;
before this line, then i get segfaults at some point in the program. The program is quite big and segfaults can be hard to track down (I'm planning to use a debugger), but can people here perhaps point me in the right direction from experience? My best guess is that when you just make the matrix using new, randomField is still a pointer but its a constant pointer. When you add the pointer declaration its not a constant pointer. I'm not sure how that could affects things. Help?
PS it also may be relevant to note that the program generates segfaults in certain other situations, most notably its supposed to output data to files and if that output is not disabled then I can get segfaults as well. I'm still tracking that down separately though.
-
March 17th, 2009, 01:14 AM
#2
Re: Tricky Issue Involving Pointers and Segfaults
that line should read double * randomField.
-
March 17th, 2009, 02:56 AM
#3
Re: Tricky Issue Involving Pointers and Segfaults
I think this case not related to const or not constant pointer but is related you access memory that you doesn't have permission.
Thanks for your help.
-
March 17th, 2009, 03:42 AM
#4
Re: Tricky Issue Involving Pointers and Segfaults
You are providing far to little information for us to help you with the problem!
 Originally Posted by Nirf
In particular, the program has to declare an array:
randomField = new double [N];
If however I add the line
double * randomfield;
before this line, then [...]
Are you implying that the program even compiles if you don't declare randomField as double* ? If so, that would mean it is declared somewhere else and by adding the line you are shadowing the old declaration, which could easily lead to memory leaks and memory access violations.
Please provide a large enough section of your code if you want help on this issue.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
March 17th, 2009, 04:25 AM
#5
Re: Tricky Issue Involving Pointers and Segfaults
 Originally Posted by Nirf
Hello,
I have the following very bizarre problem. I have written some code I have to integrate with a larger part of C++ programming. This program has some sketchy segfault issues. In particular, the program has to declare an array:
randomField = new double [N];
If however I add the line
double * randomfield;
before this line, then i get segfaults at some point in the program.
Your error is on line 63, 89, and 103.
Seriously, no one can answer your question with this information. We need to see a full example of this behaviour.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|