Hi everyone,
if I had a char array and I want to somehow shorten it. what is the most easier way to do it? was it copy to another string? or is there a function to shorten it??
eg. ABCDEFG
I only need it to be ABCD.
Hi everyone,
if I had a char array and I want to somehow shorten it. what is the most easier way to do it? was it copy to another string? or is there a function to shorten it??
eg. ABCDEFG
I only need it to be ABCD.
put \0 after the last char you still want in your string
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Mmm, that would be somewhat of a memory leak. I'd copy it to a new string of a certain length and free the old one, but of course, this means you need to use malloc instead of static memory.
O, yeah, really? And what part of the memory is exactly leaking?Mmm, that would be somewhat of a memory leak
Is also makes a memory leak?Code:int a[2] = {0,1};
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Not even in the same context as strings but ...Code:int a[2] = {0,1};
The premise behind a terminating zero is that, in strings, "beyond this point is garbage." Since you would never access anything beyond that then why keep it around? It's really a rather simple idea to be smart instead of clever. And also, if you do keep accessible data after a terminating zero and then royally mess up your indexing, you've hidden a seg fault.
Last edited by whiteflags; 11-13-2006 at 03:15 AM.
The difference between leak and unused memory lives here - would never and could neverOriginally Posted by citizen
If you could never access the memory - it is a leak.
If you would never acess this memory - it just your preference of the working.
You can try to look at the strtok code to learn some new for you ideas of acessing memory after the '\0' charachter.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Meh.
> You can try to look at the strtok code to learn some new for you ideas of acessing memory after the '\0' charachter.
Or not worry about it and avoid screwing up my strings the way strtok does.
Still it just your way of working.Originally Posted by citizen
But putting any value inside the string even if it is zero does not produce the memory leak.
[code]
char a[100] = "A";
[code]
98 bytes are not leaked - they are just not used yet.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Then I apologize. It isn't a memory leak, just a bad practice that is as bad as one.
I disagree with you that it is bad practice. In fact, it is quite useful at times. For example;Originally Posted by citizen
This code allows inout to be read from stdin, one line at a time. If any line is long enough to overflow the buffer, the code copes by readin the line in pieces. The overwrite of *temp actually truncates buffer, as far as standard string functions are concerns (to strip off any trailing '\n'; if there is no trailing '\n' that is a sign of a long line). However, this does not cause a memory leak --- buffer still remains an array of 100 chars, so the next call to fgets() can happily read to the whole buffer without causing undefined behaviour.Code:#include <stdio.h> #include <string.h> int main() { char buffer[100]; char *temp; while (fgets(buffer, 100, stdin) != EOF) { temp = strchr(buffer, '\n'); if (temp != NULL) *temp = '\0'; /* buffer now has the input characters without the newline */ /* do something with this part of the line */ } }
While, yes, it would be possible to achieve the same effect by reading one character at a time from stdin, and dynamically allocating memory, there is no need to.
Isn't that a bit different from what we're actually doing here, since streams like stdin are supposed to be continuous in the first place?
There may be more data in the stream, but you aren't purposely shoving it in a buffer after the terminating zero.
Data after a string's terminating zero is just wrong. I won't debate it any further.
> Data after a string's terminating zero is just wrong. I won't debate it any further.
If you keep the size of the char[] you can probably look at it as an hack. However it is ugly, I agree.
Also in no way it answers the original OP question, which was how to shorten a string, not how to make a string look smaller.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
A string is zero or more characters terminated by a null character. Thus, if you have a string of more than zero non-null characters, by moving the null closer to the start of the string you have effectively shortened your string. Pretty simple.
He asked how to shorten a string, not how to reallocate a smaller block of memory and copy a string into it. That would be creating a new string. Not shortening an existing one.
It can be argued either way.
Quzah.
Hope is the first step on the road to disappointment.
std::vector regularly allocates more memory than it uses at the moment. Is making an algorithm O(1) wrong? Or would you have a "clean", O(n) dynamic array algorithm?
a bad practicePlease explain, elaborate on and defend your comments, which I consider rash and inappropriate. You don't need to debate it, but you do need to have solid ground to stand on.Data after a string's terminating zero is just wrong. I won't debate it any further.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
But is is still a strange thing to have
Code:char a[] = "look ma! No segfault!"; a[8] = '\0'; printf("%s%c", a, '\n'); printf(a + 9);
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.