Saving data

C

CPlusPlus

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
 
C

CPlusPlus

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.
 
O

osmium

CPlusPlus said:
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.
 
J

James Lothian

CPlusPlus said:
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
 
B

Bo Persson

Juha said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top