memcpy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
pair<char*,char*> tokenize_func(const char* ptr)
{
	size_t len = strlen(ptr);
	pair<char*,char*> alpha_val;
	char *mem = new char[len];
	if(mem == NULL)
	{
			cout<<"error allocating"<<endl;
			exit(0);
	}
	int j=0,i=0;
	
	if(charcmp(ptr,alpha))
	{
		mem[i] = *ptr;
		i++;
		while(charcmp((ptr+i),alpha) || isdigit(*(ptr+i)))
		{
			mem[i] = *(ptr+i);
			i++;
		}
		size_t spare_len =  len -i;
		char *spare  = new char[spare_len];
		if(spare == NULL)
		{
			cout<<"error allocating"<<endl;
			exit(0);
		}
		while(j<spare_len && spare_len > 0)
		{
				spare[j] = *(ptr+i);
				j++;i++;
		}
	
		
			memcpy(alpha_val.first,mem,strlen(mem));
			memcpy(alpha_val.second,spare,strlen(spare));//this causes seg fault

		delete [] mem;
		delete [] spare; 
	}//closes the first if
	
	
	return alpha_val;
}



How do you do a memcopy of the mem and alpha.first without a seg fault.
I think you need to allocate some memory to alpha_val.first and alpha_val.second using new[] before trying to write to them.
I agree but however you need to free that memory allocated by alpha_val now which is difficult since you need to return alpha_val. How do you free alpha_val in this case.
Remember that all character arrays containing strings have a null-terminated operator, so the size of the array is actually strlen(str)+1.
The srlen(str) + 1 is not the problem . How do we deallocate memory in this case since in the main there is a while loop that will continuously call tokenize and if you dont deallocate the memory, the program might just crash or memory leakage...
Last edited on
The calling function will have to deallocate the pair when its finished with it. Why don't you use string instead of char*?
How can calling function deallocate it when alpha_val is local to the fucntion tokenize?

I have not tried with strings before and i might try it. But another option left is to use a class and destroy the object via a destructor.
Although alpha_val is local to the function you return it at the end. So before the local alpha_val is destroyed a copy of it is returned to the caller. You can use delete[] on the pointers stored in the returned copy.
k so u r saying i can do a return alpha_val and and then delete alpha_val from the calling function. how can you do that when alpha_val from the perspective of the calling function does not exist as it is local only to tokenize func().

so what i did , i created a class and created an object but allow the object to continously go out of scope...but i find this weird.And now i have a problem as i push the values in a list but since the objects go out of scope, the alpha_val is destroyed via destructor and the list prints some garbage values. How do i copy memory from the heap to another region and add it to the list.


When you put a return var; instruction in your code, you are telling the compiler to make a copy of the variable var. So even though your variable in the function goes out of scope, a new variable is created in the scope of the caller that is a copy of the one that went out of scope.

Try it.
Topic archived. No new replies allowed.