Re: Basic, but what the hell am I doing wrong here?

From: Kevin Mark (kmark+debian-user_at_pipeline.com)
Date: 06/11/04

  • Next message: Katipo: "Re: How to get better quality display"
    Date: Thu, 10 Jun 2004 22:49:42 -0400
    To: debian-user@lists.debian.org
    
    
    

    On Thu, Jun 10, 2004 at 09:36:04PM -0400, Stephen Touset wrote:
    > I've opened and read files hundreds of times in my life. What gives now?
    >
    > --
    > Stephen Touset <stephen@touset.org>

    > #include <fstream>
    >
    > using namespace std;
    >
    > int main(void)
    > {
    >
    > ifstream fin("test.cc");
    > char* str;
    >
    > if (!fin.is_open())
    > {
    > exit(1);
    > }
    >
    > while (!fin.eof())
    > {
    > fin.getline(str, 80);
    > }
    >
    > fin.close();
    >
    > return 0;
    >
    > }
    Hi Stephen,
    I can understand your confusion. you have a perception issue that is
    clouding your understanding. either that or you have been looking at
    this for umteen hours and too much caffine x-). The issue is the
    understading of memory allocation versus pointer definition.

    'char * str' defines a 'char *' called str. it allocates memory for this
    pointer. so, str has an address of 0xabcd (for example). It has not been
    initialized with anything, so it contains undeterminate data (eg.
    garbage). lets say it the contents of address 0xabcd is 'Z'. so, *str =
    'Z'. What does *(str + 1) contain? undeterminate data. As does *(str +
    2) ... *(str + 79). So you define str, don't initialize it and then read
    data into *(str) ... *(str + 79). Well not really, becase you have no
    right to write to those areas. Why? because you have not allocate memory
    at those addresses. How do you do this? well in c you'd 'malloc. But
    this is c++, so, you'd use 'new', I think.

    This should shead some light on you problem.
    -Kev

    
    

    -- 
    To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org 
    with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
    


  • Next message: Katipo: "Re: How to get better quality display"

    Relevant Pages

    • Re: Basic, but what the hell am I doing wrong here?
      ... > clouding your understanding. ... > understading of memory allocation versus pointer definition. ... stringstream object or similar, which is much safer than ... calling malloc or new. ...
      (Debian-User)
    • Doubts on Defining and declaring variables
      ... Kindly Correct the places where my understanding is wrong. ... 2.Dim iage As Integer (My understanding:-Still No memory allocation ... What happens in case of arrays when I give dim arr?? ... How is the memory allocation done when we use Redim? ...
      (microsoft.public.scripting.vbscript)
    • Re: Linked List & Dynamic Memory Allocation
      ... I have better understanding of how programs relate to stack ... and heap. ...
      (microsoft.public.vc.mfc)
    • static memory allocation versus dynamic memory allocation
      ... static memory allocation instead of dynamic memory allocation. ... understanding is that static memory allocation like using array is ... but dynamic memory allocation is more flexible. ...
      (comp.lang.c)

    Loading