Saving data

Discussion in 'C++' started by CPlusPlus, Apr 6, 2010.

  1. CPlusPlus

    CPlusPlus Guest

    typedef struct dummy // global definition
    {
    char* p1;
    char* p2;
    }d;

    d d1;

    void foobar()
    {
    // allocate memory from heap
    d1.p1 = (char*)malloc(strlen("hello"+1)*sizeof(char));
    strcpy(d1.p1, "hello");
    }

    void main()
    {
    foobar();
    printf("d1.p1 = %s\n", p1.d1); // PROBLEM: junk is printed.
    free(d1.p1);

    }

    I think I know why junk is printed because when foo exits, p1 data
    vanishes, i.e, goes out of scope. How can I fix this?

    Bare with me, I started out on writing a C++ program in MS VS2005 but
    C has me figuring it out.

    Thanks
     
    CPlusPlus, Apr 6, 2010
    #1
    1. Advertising

  2. CPlusPlus

    CPlusPlus Guest

    On Apr 6, 12:07 pm, CPlusPlus <> wrote:
    > typedef struct dummy  // global definition
    > {
    >     char* p1;
    >     char* p2;
    >
    > }d;
    >
    > d d1;
    >
    > void foobar()
    > {
    >     // allocate memory from heap
    >     d1.p1 = (char*)malloc(strlen("hello"+1)*sizeof(char));
    >     strcpy(d1.p1, "hello");
    >
    > }
    >
    > void main()
    > {
    >    foobar();
    >    printf("d1.p1 = %s\n", p1.d1);  // PROBLEM: junk is printed.
    >    free(d1.p1);
    >
    > }
    >
    > I think I know why junk is printed because when foo exits, p1 data
    > vanishes, i.e, goes out of scope.  How can I fix this?
    >
    > Bare with me, I started out on writing a C++ program in MS VS2005 but
    > C has me figuring it out.
    >
    > Thanks


    typo fix: printf("d1.p1 = %s\n", d1.p1); // PROBLEM: junk is
    printed.
     
    CPlusPlus, Apr 6, 2010
    #2
    1. Advertising

  3. CPlusPlus

    osmium Guest

    CPlusPlus wrote:

    > On Apr 6, 12:07 pm, CPlusPlus <> wrote:
    >> typedef struct dummy // global definition
    >> {
    >> char* p1;
    >> char* p2;
    >>
    >> }d;
    >>
    >> d d1;
    >>
    >> void foobar()
    >> {
    >> // allocate memory from heap
    >> d1.p1 = (char*)malloc(strlen("hello"+1)*sizeof(char));
    >> strcpy(d1.p1, "hello");
    >>
    >> }
    >>
    >> void main()
    >> {
    >> foobar();
    >> printf("d1.p1 = %s\n", p1.d1); // PROBLEM: junk is printed.
    >> free(d1.p1);
    >>
    >> }
    >>
    >> I think I know why junk is printed because when foo exits, p1 data
    >> vanishes, i.e, goes out of scope. How can I fix this?
    >>
    >> Bare with me, I started out on writing a C++ program in MS VS2005 but
    >> C has me figuring it out.
    >>
    >> Thanks

    >
    > typo fix: printf("d1.p1 = %s\n", d1.p1); // PROBLEM: junk is
    > printed.


    I don't know what your problem is. It works OK for me on DevC as a C++
    program. Have to make it compile first: provide three missing includes,
    change return type of main, fix typo you describe in your second post.

    I hope you realize by now that Usenet people want *programs*, not fragments
    of programs. Use copy and paste, don't retype.
     
    osmium, Apr 6, 2010
    #3
  4. CPlusPlus wrote:
    > typedef struct dummy // global definition
    > {
    > char* p1;
    > char* p2;
    > }d;
    >
    > d d1;
    >
    > void foobar()
    > {
    > // allocate memory from heap
    > d1.p1 = (char*)malloc(strlen("hello"+1)*sizeof(char));
    > strcpy(d1.p1, "hello");
    > }
    >
    > void main()
    > {
    > foobar();
    > printf("d1.p1 = %s\n", p1.d1); // PROBLEM: junk is printed.
    > free(d1.p1);
    >
    > }
    >
    > I think I know why junk is printed because when foo exits, p1 data
    > vanishes, i.e, goes out of scope. How can I fix this?
    >
    > Bare with me, I started out on writing a C++ program in MS VS2005 but
    > C has me figuring it out.
    >
    > Thanks
    >

    There are various typos, header files not included &c here. After fixing
    those, the big problem is this:
    malloc(strlen("hello"+1)*sizeof(char))
    You take the start address of the literal string "hello", add one to it,
    and measure its length from there. In other words, rather than adding
    one to the length of the string, to account for the terminating null,
    you've effectively subtracted one. The strcpy() then writes off the end
    of the allocated block of memory, evoking undefined behaviour.

    The fix for this is pretty self-evident. BTW, this would probably be better
    posted in a C newsgroup, as there's no C++ at all in this.

    James
     
    James Lothian, Apr 6, 2010
    #4
  5. CPlusPlus

    Bo Persson Guest

    Juha Nieminen wrote:
    > On 04/06/2010 07:07 PM, CPlusPlus wrote:
    >> // allocate memory from heap
    >> d1.p1 = (char*)malloc(strlen("hello"+1)*sizeof(char));
    >> strcpy(d1.p1, "hello");

    >
    > No offense, but that's the most horrendous piece of code I have
    > seen in a long, long time.
    >


    Yes, there are some *really* good reasons for using C++ std::string!

    struct d
    {
    std::string s1;
    std::string s2;
    };

    d d1;

    void foobar()
    { d1.s1 = "hello"; }



    Bo Persson
     
    Bo Persson, Apr 7, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy Scarisbrick

    Saving Data When ASP.Net Page Unloads

    Roy Scarisbrick, Jul 7, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    419
    David Waz...
    Jul 8, 2003
  2. Tommy Christian

    Saving serialized data to database problem

    Tommy Christian, Jul 25, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,289
    Tommy Christian
    Jul 25, 2003
  3. John Kandell
    Replies:
    4
    Views:
    4,228
    eeebop
    Dec 10, 2004
  4. Luis Esteban Valencia
    Replies:
    0
    Views:
    2,555
    Luis Esteban Valencia
    Jan 6, 2005
  5. Lovely Angel For You

    Saving Images While Saving ASP Pages !

    Lovely Angel For You, Oct 2, 2003, in forum: ASP General
    Replies:
    1
    Views:
    228
    Curt_C [MVP]
    Oct 3, 2003
Loading...

Share This Page