You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

AP.Sakkthivel

Professional

  • "AP.Sakkthivel" is male
  • "AP.Sakkthivel" started this thread

Posts: 563

Location: India

Occupation: Application Development Using Qt

  • Send private message

1

Thursday, August 18th 2005, 8:00am

content copying to a pointer[Soved]

Hi All,

I need the content of 'a' should be copied to the area pointed by 'c'

here is my code...

Source code

1
2
3
4
5
6
7
8
9
10
#include <iostream.h>
#include <conio.h>
int main()
{
    int a=100;
    void *b = &a;
    void *c = new int[1];
    cout << a<< *b << *c;
    getch();
}



help me

Thanks in advance..
Regards
- $akthi

You never know, how soon it may be too late…

This post has been edited 1 times, last edit by "AP.Sakkthivel" (Sep 13th 2005, 6:09am)


2

Thursday, August 18th 2005, 10:25am

RE: content copying to a pointer

Source code

1
memcpy (c, &a, sizeof(int));
?
There's no place like ::1

This post has been edited 1 times, last edit by "zztop" (Aug 18th 2005, 10:26am)


AP.Sakkthivel

Professional

  • "AP.Sakkthivel" is male
  • "AP.Sakkthivel" started this thread

Posts: 563

Location: India

Occupation: Application Development Using Qt

  • Send private message

3

Thursday, August 18th 2005, 11:18am

RE: content copying to a pointer

Thanks zztop.

I thought of using ::CopyMemory() of windows.

Thanks for tip
Regards
- $akthi

You never know, how soon it may be too late…

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

4

Thursday, August 18th 2005, 1:59pm

Simple assignment?
*c = a

Cheers,
_
Qt/KDE Developer
Debian User

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

5

Thursday, August 18th 2005, 6:09pm

Using a copy constructor is a way too :)

Source code

1
c = new int(a);

6

Thursday, August 18th 2005, 6:17pm

Quoted

Originally posted by wysota
Using a copy constructor is a way too :)

Source code

1
c = new int(a);


No way. If you do this, the original address held by c will be replaced with whatever operator new returns (most probably causing a memory leak).
And if c is a parameter passed into a function, changing its value will do nothing, since pointers, as everything else, are passed by value.
If you want to write to the memory c points to, you must alter *c.

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

7

Thursday, August 18th 2005, 6:50pm

That is situation dependant, I didn't say it is foul proof (as anything). But if you want, you might modify it to:

Source code

1
*c = int(a);


Of course I could counter your argument and say that c might be an uninitialised pointer and you might be erasing some important data (or causing the application to crash), so you should create an object and assign value to it instead of a simple copy... Based on the example given in the initial post, my first approach would be correct (not counting the potential memory leak, but that is also present in the example, so that doesn't really matter here). The bottom line is: there are plenty ways of copying data, but the one to choose depends on the situation.

8

Thursday, August 18th 2005, 7:16pm

Well, of course there are many ways of copying data.
The thread's author asked us how to copy a value through a pointer (it implies that the pointer is already initialized as well! ;))
So I won't argue here... Except that writing int(a) when a is already of a basic type (int) is just a waste of keystrokes (IMHO). For a custom class, however, it might make some sense so that it would be copy-constructed and not produced via operator=.

AP.Sakkthivel

Professional

  • "AP.Sakkthivel" is male
  • "AP.Sakkthivel" started this thread

Posts: 563

Location: India

Occupation: Application Development Using Qt

  • Send private message

9

Friday, August 19th 2005, 7:06am

Thanks to all for the reply;

my actual motto is copying an array of values to a
pointer initialised with enough buffer to hold the array data.

So i assume, for this type of operation, we can definitely go for
memcpy or ::CopyMemory();

Is it not?

with regards
..
Regards
- $akthi

You never know, how soon it may be too late…