malloc question

Hello,

I have a question about malloc. I am working on a network application and when i receive data i copy it to a new correctly sized buffer. i make it using malloc now i know that when you want to let say print out a buffer it will continue till it finds an escape character. so i manually put one in on the end but if i do the the free command crashes. now i don't really understand why this happens. can anyone explain this behavior to me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>
typedef unsigned char byte;

int main()
{
	void* data = malloc(11);
	byte* data2 = (byte*)data;
	data2[11] = 0;
	char* h = "Hello World";

	memcpy(data, h, 11);

	printf("%s\n", data);
	printf("%s\n", data2);

	free(data2);

	return 0;
}
Have you tried freeing data instead of data2? Also why do you need 2 pointers to the same location in memory? data2[11] is out of bounds in your example. In c++ arrays start at 0, so in an array of 11 elements the last index is 10. And how your compiler found malloc, memcpy, free, and printf is just magic. You should consider a different compiler that makes you include necessary header files.
Your code should look more like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdio>
#include <cstdlib>
#include <cstring>
typedef unsigned char byte;
     
int main() {
    char* h = "Hello World";
    int length = strlen(h);
     
    byte* data = (byte*) malloc(length+1);
    data[length] = 0;
     
    memcpy(data, h, length);
     
    printf("%s", data);
    free(data);
     
    return 0;
}

http://ideone.com/8OczMA
If you are learning C++, I suggest that you try to ditch the old C libraries in favor of their C++ replacements.
Last edited on
I am using visual studio, and yes it sometimes magically finds stuff.

how would you use new when you don't know what data type it is.

 
byte* data = new byte[11];


and it was indeed an out of bounds error. its just weird it did not crash on line 8;
Line 8. Recall the basics: If an array has N elements, then what are the valid index values?

Line 11. The string literal pointed to by h contains 12 characters (11 printable + null). If your array has only 11 elements and you write 11 printable characters to it, then there is no terminating null within the array any more.


Are you forced to use C?
nope c++ is fine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
typedef unsigned char byte;

int main()
{
	byte* data = new byte[12];
	data[11] = 0;

	char* h = "Hello World";

	memcpy(data, h, 11);

	printf("%s\n", data);
	delete data;

	return 0;
}


got this working so i guess i will stick to this.
If you are going to use that, at least put in the includes if it is a homework assignment.
its not homework is a side project. thanks for the help
how your compiler found malloc, memcpy, free, and printf is just magic

It's not magic, just lucky. With his compiler, iostream must include the necessary C header file (directly or indirectly).
Topic archived. No new replies allowed.