Hello,
I have a quick question I want to ask. How can I copy the data from a allocated variable, so that when I free up the allocated variable the copied data isn't erased with it.
Thanks,
-A2H
Hello,
I have a quick question I want to ask. How can I copy the data from a allocated variable, so that when I free up the allocated variable the copied data isn't erased with it.
Thanks,
-A2H
I'm not sure if I understand your question correctly, but you could just copy the data like you would with non-allocated data. So something like this is what I mean:
#include <stdlib.h>
#include <stdio.h>
#define AMOUNT_OF_DATA 20
int main (int argc, char *argv[])
{
int *allocatedData = (int*) malloc(AMOUNT_OF_DATA * sizeof(int));
int i = 0;
//putting data in the allocated memory:
for (i = 0; i < AMOUNT_OF_DATA; i++)
allocatedData[i] = i + 1;
printf("Contents of allocatedData:\n");
for (i = 0; i < AMOUNT_OF_DATA; i++)
printf("%d ", allocatedData[i]);
//Make a copy
int dataCopy[AMOUNT_OF_DATA];
//Copy the data from the allocated memory to the new variable.
for (i = 0; i < AMOUNT_OF_DATA; i++)
dataCopy[i] = allocatedData[i];
//Freeing the allocated memory
free(allocatedData);
//Showing contents of the copy
printf("\n\nContents of dataCopy:\n");
for (i = 0; i < AMOUNT_OF_DATA; i++)
printf("%d ", dataCopy[i]);
return 0;
}
Will result in the following output:
Contents of allocatedData:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Contents of dataCopy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Yes that's exactly what I'm talking about. How ever, when I free the allocated data. The newly copied data is erase as well.
Maybe this will give a clearer meaning of what I'm trying to do.
struct{
char *info[256];
int ipid;
}CopyAllc;
...
char *token[256];
char *FBUFFER;
FBUFFER = malloc(sizeof(char)*lSize);
if(FBUFFER == NULL){
DisplayError("Memory Allocation Error!");
return 0;
}
fread(FBUFFER, sizeof(lSize), lSize, infoFile);
fclose(infoFile);
token[i] = strtok(FBUFFER, "\n\r ");
while(token[i] != NULL){
++i;
token[i] = strtok(NULL, "\n\r ");
}
for(i = 0; i < sizeof(token); i++){
if(token[i] == NULL) break;
CopyAllc.info[i] = token[i];
}
free(FBUFFER); //erases the copied data from CopyAllc.info as well.
>Maybe this will give a clearer meaning of what I'm trying to do.
strtok doesn't make a copy of the token, it modifies the original string by adding a null character at the end of each token and then returns a pointer to it. Every pointer in token, and consequently every pointer in CopyAllc.info, is actually a pointer to your dynamically allocated memory. When the memory goes away, so do all references to it.
You need to make the copy yourself:
// Was CopyAllc.info[i] = token[i];
{
CoypAllc.info[i] = malloc ( strlen ( token[i] ) + 1 );
if ( CopyAllc[i] == NULL ) {
/* Handle the error */
}
strcpy ( CopyAllc[i], token[i] );
}
Now for some general commments.
>FBUFFER = malloc(sizeof(char)*lSize);
sizeof(char) is guaranteed to evaluate to 1, so you can simplify your call to malloc:
FBUFFER = malloc ( lSize );
Also, an unadorned size is suspicious when allocating memory for a string. Usually I'd expect +1 for the null character:
FBUFFER = malloc ( lSize + 1 );
>for(i = 0; i < sizeof(token); i++){
There are two things I'd recommend on this:
sizeof(token)
is not sufficient to get the size of an array unless it's an array of char. You need sizeof token / sizeof *token
because token is an array of pointers to char. sizeof gives you the total bytes in the array, and dividing it by the size of a single element gives you the number of elements.while ( i >= 0 ) { /* Assuming i is an int */
/* Copy token[i] to CopyAllc.info[i] */
--i;
}
In this case the order is irrelevant. You can copy from back to the front without altering the result.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.