Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Saving data

Reply
Thread Tools

Saving data

 
 
CPlusPlus
Guest
Posts: n/a
 
      04-06-2010
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

 
Reply With Quote
 
 
 
 
CPlusPlus
Guest
Posts: n/a
 
      04-06-2010
On Apr 6, 12:07*pm, CPlusPlus <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      04-06-2010
CPlusPlus wrote:

> On Apr 6, 12:07 pm, CPlusPlus <(E-Mail Removed)> 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.


 
Reply With Quote
 
James Lothian
Guest
Posts: n/a
 
      04-06-2010
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

 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      04-07-2010
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
EXCEL question saving a file saving the the first column as read only Luis Esteban Valencia ASP .Net 0 01-06-2005 07:02 PM
Saving DataTable to session vs saving a Custom object. John Kandell ASP .Net 4 12-10-2004 05:08 AM
Saving Images While Saving ASP Pages ! Lovely Angel For You ASP General 1 10-03-2003 12:03 AM
Saving serialized data to database problem Tommy Christian ASP .Net 0 07-25-2003 08:26 AM
Saving Data When ASP.Net Page Unloads Roy Scarisbrick ASP .Net 2 07-08-2003 01:27 AM



Advertisments