1

I have a working code for a function which takes a character and a number and forms a string by duplicating that character that number of times. Here is a piece that works perfectly fine.

char * buildstr(char c, int n)
{ 
   char * pc = new char[n+1];
   for (int i = 0; i < n; ++i)
   {
      *(pc+i) = c;
   }

   *(pc+n) = '\0';
   return pc;
}

But if I use another logic where I directly increment the pointer itself directly then I get an error when I free up the memory in the main function using delete [].

What could be my problem ?

Thanks,

char * buildstr(char c, int n)
{ 
   char * pc = new char[n+1];
   for (int i = 0; i < n; ++i)
   {
      *pc = c;
      pc++;

   }
   *(pc) = '\0';
   return pc;
}
4
  • 3
    You are returning the incremented pointer, not the one returned from the allocation. Also, instead of expressions like *(pc+i) you should use the equivalent and more readable pc[i]. Commented May 21, 2015 at 15:44
  • 2
    This should not be tagged as C, it's clearly C++. Commented May 21, 2015 at 15:48
  • 1
    Note that it's unnecessary to write such a function in C++ (except maybe for learning purposes) since std::string has a constructor (see (2)) that does exactly this (and another big plus: it managed the memory for you). Commented May 21, 2015 at 15:54
  • 1
    Function free expects as argument the starting address of a memory block that has been allocated using function malloc/calloc/realloc. Operator delete expects as operand the starting address of a memory block that has been allocated using operator new. You cannot just pass any address within that block of memory. I was about to ask you in a comment if you were serious about not being able to see the problem in your code, but then I realized your perception of a memory block as being some soft of "atomic unit". Commented May 21, 2015 at 16:00

3 Answers 3

7

The problem is that you're returning the incremented pointer. delete[] has to be invoked with a pointer value that was returned by a call to new[], otherwise you get undefined behaviour. The same applies to calls to free and pointers returned by malloc, calloc etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Sometimes it's a little tricky understanding where exactly OP's misconception. This is in fact a classic example. I was about to ask OP in a comment if he/she were serious about not being able to see the problem, but then I realized his/her perception of a memory block as being some soft of "atomic unit". Perhaps you should clarify this issue in your answer, because I think that's where OP's confusion really stems from (see my comment to the question).
2

In the second example, you are incrementing the value of the pointer. When you try to delete it, it is pointing to the last position of the array.

This way, when you call delete[] on it, it crashes as it expects pc to point to the address that was originally allocated using new[].

I encourage you to use the first approach as you will be able to keep track of the base address through the lifetime of pc. Moreover, I think it would be easier for you to do this using array notation.

Consider using:

pc[i] = c;

instead of

*(pc+i) = c;

As it is more idiomatic and easy to read. If you are simply exercising your pointer arithmetic, stick to the first approach that you described.

1 Comment

Thanks. I am familiar with array notation and that the second function is'nt a good way to implement that thing. But I was doing that for learning purpose and I got an Important concept that delete[] expects the same address allocated to new. I did'nt know that. Thanks everyone for the help.
0

In the first function let's say that your pointer start at 0x00100. when you do the loop you get to the next adress so for exemple let's say 4 loop:

0x00100
0x00101
0x00102
0x00103

but at the end you return the pointer on the 0x00100 beceause you used index *(pc+i) where i is the index, so it is good.

Now on the second function you are doing the same exept that your are moving your pointer itself without index so you return your pointer at the last adress

0x00103

That is wy your second function doesn't work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.