CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2008
    Posts
    17

    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.

  2. #2
    Join Date
    Jul 2008
    Posts
    17

    Re: Tricky Issue Involving Pointers and Segfaults

    that line should read double * randomField.

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Tricky Issue Involving Pointers and Segfaults

    You are providing far to little information for us to help you with the problem!
    Quote Originally Posted by Nirf View Post
    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.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Tricky Issue Involving Pointers and Segfaults

    Quote Originally Posted by Nirf View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured