473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Copy dynamically allocated array of ints

10
Example code:

Expand|Select|Wrap|Line Numbers
  1.         int a, b, c;
  2.  
  3.         int *t1;
  4.         int *t2;
  5.         t1 = malloc(3*sizeof(int));
  6.         t1[0] = 1;
  7.         t1[1] = 2;
  8.         t1[2] = 3;
  9.         t2 = malloc(3*sizeof(int));
  10.         t2 = t1;
  11.  
  12.         a = t2[0];
  13.         b = t2[1];
  14.         c = t2[2];
  15.  
  16.         t2[0] = 5;
This appears to work fine (copying t1 into t2), until modifying t2, when t1 is modified as well since you have obviously only copied the pointer, rather than the data value.

I then pieced together a 'copy' method:
Expand|Select|Wrap|Line Numbers
  1. void copy(int *to, int *from)
  2. {
  3.     int c;
  4.     for(c=0; c<sizeof(from)/sizeof(int); c++)
  5.     {
  6.         to[c] = from[c];
  7.     }
  8. }
to replace 't2 = t1' with 'copy(t2, t1)', but this doesn't seem to work either. I think I'm just generally a bit confused, so can anyone poke my copy method in the correct direction?

Thanks,
Mar 27 '08 #1
4 6780
Laharl
849 Expert 512MB
You need to pass in a double pointer for to, that is an int**, since you're modifying a copy of the pointer otherwise. Also, from should be a const parameter, since it's not being changed.
Mar 27 '08 #2
Ganon11
3,652 Expert 2GB
The problem is, when you pass in an array to a function, all that shows up is a pointer. It's no longer an array, and thus, using your sizeof division will not give you the size of the array - it will give you the size of an integer pointer (probably 4 bytes) divided by the size of an integer (probably 4 bytes).

In order to know the size, you have to pass in an additional int value indicating the size. You would them copy elements from[0], from[1],..., from[size-1] to to[0], to[1],..., to[size-1], respectively.
Mar 27 '08 #3
Banfa
9,065 Expert Mod 8TB
You may wish to consider using the standard library function memcpy
Mar 28 '08 #4
dperren
10
Ahh, brilliant!

Combination of memcpy and remembering the amount of memory initially allocated did the trick.

Thanks everyone!
Mar 28 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: William Payne | last post by:
Hello, I was under the impression that if I made a class Foo and if I didn't specify a copy constructor I would get one anyway that simply assigns the member variables (and that won't work for...
7
by: masood.iqbal | last post by:
I am having lots of trouble getting a simple program that initializs a dynamically allocated 2D array to work. My 2D array is not getting initialized properly, and additionally I am getting a...
2
by: songfire | last post by:
Hi everybody! Just wondering if it is possible to point to variables in the heap. For example, is this okay? int * ptr_to_DAIA; // pointer to dynamically allocated integer array ptr_to_DAIA =...
11
by: binaya | last post by:
Dear all, Say I allocate a block of memory using the command malloc.I have a question. Can I deallocate certain portion of it only during runtime ? For eg: Say I allocate 5 pointers to int...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
4
by: wyrmmage | last post by:
hello :) I need to make a dynamically allocated array of pointers, using a .hpp and .cpp file; how do I accomplish this? I think I know how to make an array of pointers, and I know how to make a...
9
by: onkar | last post by:
how many bytes will be allocted by following code - #include<stdio.h> #define MAXROW 3 #define MAXCOL 4 int main(int argc,char **argv){ int (*p); p=(int (*))malloc(sizeof(*p)*MAXROW);...
7
by: Serpent | last post by:
The C-FAQ describes some techniques here: http://c-faq.com/aryptr/dynmuldimary.html I was using something slightly different from the C-FAQ and I was wondering if it was legal. Say I want a...
34
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
Hi, is there a way to avoid the automatic copy constructor generation. I do not want the object to be non-copyable. I simply do not want that copying is done by the default copy constructor. But...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.