Re: pointer concepts



geshule@xxxxxxxxxxx wrote:

elu wrote:
You can run into the problem again if you read more
characters than you allocated.

I agree with you on that.
But the codes below works even if you input more than 10 characters?
Why?

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char *str;
str = new char[10];
printf("Enter a string to reverse\n");
scanf("%s",str);
cout << strlen(str)<< endl;
system("pause");
return 0;
}

I don't know, it's not C!
Please post C++ questions to comp.lang.c++

<OT>
I don't see why would it work for more characters than you allocate.
</OT>

--
BR, Vladimir

Goto, n.:
A programming tool that exists to allow structured programmers
to complain about unstructured programmers.
-- Ray Simard

.



Relevant Pages

  • Re: fdopen() on unix sockets and getline()
    ... You need to allocate memory for reading anyway, ... variable for the length of the allocated buffer and an index ... But the function should not be blocking and, in a general way, I should correctly manage receiving sequence of characters like: ... After reading all the available characters of the above sequence, I have to check not only the last character, but all the newly arrived characters looking for a newline. ...
    (comp.unix.programmer)
  • Re: A C showstopper
    ... format to storage, sprintf has a built in danger that as far as I know ... The C99 solution (limit the number of characters) is extraordinarily ... its contract is to always allocate enough ...
    (comp.lang.c)
  • Re: pointer arithmetic help
    ... char * kernel_buf; ... No storage has been allocated for the 5 characters you are trying to copy ... for user_buf the compiler allocates storage for the string. ... You must allocate storage... ...
    (comp.lang.c)
  • Re: CString - Addition of large strings
    ... A CString object can store up to INT_MAX characters. ... > bad for potentially very long strings, such as might be used to represent ... The function lstrcat doesn't allocate memory, ...
    (microsoft.public.vc.mfc)
  • Re: Variable-sized lines of text in linked list
    ... list structure, however, I am unsure of how to dynamically allocate ... You start by allocating a small array of characters and then start ... reading characters into it. ...
    (comp.lang.c)