problems with a string ...

I have a basic problem concerning strings and such. I don't know what is a possible reason for the following:
I want to have string concatenanted and then i send it via a http connection.
What i do is i allocate my string and set its value like that.
1
2
3
4
5
6
7
8
9
// ... content is "appended" to respStr  and then copied to *pBuffer, where *pBuffer is 
// allocated in the function containing the code below ...
retVal   = respStr.size() + 1;
*pBuffer = (char*)calloc(retVal , sizeof(char));
// sprintf((char*)*pBuffer, "%s", (respStr.c_str());

			
strcpy((char*)*pBuffer, (char*)(respStr).c_str());
		

Well, one might find that a little strange, but anyway. When the function returns the char array is "broken", i.e. there are strange special characters at the beginning of the array. What reason can that possibly have ?
As i said, i think it is a basic problem. But still, i don't know, and i'd be very glad if you could help me further.

Thanks a lot,
...
p.s: i forgot an important note: In the debugger i do not have that problem, so I think that it must be something "system dependent".
Last edited on
Line 9 looks odd.
 
strcpy((char*)*pBuffer, (char*)(rspWrtr->respStrQualities).c_str());

Both of the (char *) casts are unnecessary. But the real problem is the dereferencing of the buffer *pBuffer. The * is not needed.

This would be better:
 
strcpy(pBuffer, rspWrtr->respStrQualities.c_str()); 

Or, in C++ to avoid the need for allocating and de-allocating memory yourself
 
std::string buffer = rspWrtr->respStrQualities.c_str();

(the .cstr() may not be needed, depends on the type of rspWrtr->respStrQualities).
Hi, thanks for the reply.

I have to dereference "pBuffer" because it is allocated in the same function ... i call the function with a "pointer to pointer" that in the end contains "pBuffer" ... but i guess you are right about the string-buffer.
I have seen that the whole trouble starts when i "realloc" pBuffer ... i just wanted it to have the length of "respStr" ... another thing is that i messed the variable names in the post ... edited that ... in a second. So, better now ...
Do you have any idea why a "realloc" should fail ?

It still works in Debug mode, so, well ... i don't get it.
I have to dereference "pBuffer" because it is allocated in the same function

But you allocate a pointer to char, not a pointer to pointer to char.

??? did you edit the original post??? I'm lost now. It's hard to respond to a moving target.

Could you just post the complete function, at least you need to show the type of the variable pBuffer. (Please don't edit previous posts - it makes it difficult to track what has already been attempted and what is new).

If you are having to use the cast to (char *) in the call to strcpy(), that's a pretty good indication that you are doing something wrong. I don't believe they should be necessary at all.
Something like this maybe?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>

void change(char **pBuffer)
{
    std::string example = "This is a test string 1234567890";
    
    *pBuffer = (char *) malloc(example.size() + 1);
    strcpy(*pBuffer, example.c_str());
}

int main () {

    char * buffer = nullptr; 
    change(&buffer);
    std::cout << "buffer = " << buffer << '\n'; 
    
    free (buffer);
}


I'm using C++ cout rather than C printf etc. Not sure what language your code should use. If using C++ I'd prefer to pass a std::string by reference rather than a char pointer by pointer.

If eventually you need a char * to pass to some other function, then you could just use the .c_str() member function to give that.
Last edited on
... yes, exactly like that.
It is a lengthy function that cover a lot of cases with different string being concatenated, but in the end it all ends up with putting the string into the buffer and return it's length.
Sorry for editing the post ... i thought it would become more clear.
Anyway, i removed the obsolete casts and it still works in Debug mode. It seems like my problem is (see comments ...) best described with a slight edit of your code:

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

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>

void change(char **pBuffer)
{
    std::string example = "This is a test string 1234567890";
    
    *pBuffer = (char *) calloc(example.size() + 1, sizeof(char) );
    strcpy(*pBuffer, example.c_str());

    // that seems to spoil it ... it is obsolete here, but not in som other cases i have to 
    // cope with in that real function ...
    // the program crashes then later when "free" is called on "buffer" in main ...
    realloc(*pBuffer, (example.size()+1)*sizeof(char));
    
}

int main () {

    char * buffer = nullptr; 
    change(&buffer);
    std::cout << "buffer = " << buffer << '\n'; 
    
    free (buffer);
}


I don't know why, but it might be that that "realloc" does not work as intended here ... maybe because i corrupted heap before?

Thanks a lot so far
void change(char **pBuffer)
Instead of using multi-dimension pointers, I strongly suggest using reference in this case.

So the code would become :
void change(char *&pBuffer)
I strongly suggest using reference in this case.
No reason for that.

I don't know why, but it might be that that "realloc" does not work as intended here ... maybe because i corrupted heap before?
See:

http://www.cplusplus.com/reference/cstdlib/realloc/?kw=realloc

realloc(...) returns the new memory and free()s the provided memory. You need to assigne the result of realloc(...).
Cool,

that sounds easy. I'll do that immediately.
Thank you very much,

...
(edited again, 04.11, 9:45UTC : )
Worked great, thanks again
Last edited on
Topic archived. No new replies allowed.