Access violation using malloc

Posts 1–4 of 4 · Page 1 of 1
Access violation using malloc
Code:
void	PermbysVektor(int n)
{
	int *pp = (int*)malloc(n*sizeof(int)); /// Creates the vector
	for(int i = 0; i < n; i+=1)
	{
		int x;
		printf("Vendosi nje numer qe doni te shtoni tek vektori:");
		scanf("%d", &x);
		pp[i] = x;  ///Adds a new number
	}

	//// reverses the vector
	int *pp2 = (int*)malloc(n*32);
	for(int i = (n-1),
		f = 0; i >= 0,
		f < n; i-= 1,
		f += 1)
	{
		pp2[f] = pp[1];
	}

	char* prf = "";

	for(int i = 0; i < n; i+= 1)
	{
		sprintf(prf, "%d", pp2[i]);
	}
	printf(prf);
	system("pause");
}
It shows this:
Unhandled exception at 0x77951e0e (msvcr100d.dll) in Olimpiade.exe: 0xC0000005: Access violation writing location 0x003f3132.
EDIT: I'm on C tho
Alright, let's go.

First, why are you allocated n*32 bytes? That's way more than needed
Code:
int *pp2 = (int*)malloc(n*32); //why?
int *pp2 = (int*)malloc(n*sizeof(int)); //right
second, this is a problem
Code:
char* prf = "";
I assume you are trying to use C strings similar to String objects in C++.

Since you are using prf as a buffer, you need to allocate as many characters as needed:
Code:
char* prf = (char*)malloc(sizeof(char) * n);
3rd, your method of reversing the vector is pretty stupid, and it doesn't even work since you are setting everything to pp[1]
I rewrote it, it's way easier to read and less complex.

Code:
	//// reverses the vector
	int *pp2 = (int*)malloc(n*sizeof(int));
	for(int i = 0; i < n; i++)
	{
		pp2[(n-1)-i] = pp[i];
	}
Lastly, you're not really using sprintf correctly. Each time you iterate through the for loop that contains sprintf you overwrite the buffer with the new value. Just do it like this:

Code:
	char* prf = (char*)malloc(sizeof(char)*n);
	for(int i = 0; i < n; i++)
	{
		prf[i] = pp2[i];
		printf("%d ",prf[i]);
	}
OR even better, you don't even need the buffer, and can directly print the reversed vector (really it's just an array though)

Here is the output after my changes:


Basically, malloc wasn't the problem, it was you using prf as a buffer for sprintf
On a final note: they invented the ++ operator for a reason. Use it please

Edit: Actually, to get the effect you're going after, you don't even need to do all of this. Just print the original array in reverse order
Quote Originally Posted by Auxilium View Post
Alright, let's go.

First, why are you allocated n*32 bytes? That's way more than needed
Code:
int *pp2 = (int*)malloc(n*32); //why?
int *pp2 = (int*)malloc(n*sizeof(int)); //right
second, this is a problem
Code:
char* prf = "";
I assume you are trying to use C strings similar to String objects in C++.

Since you are using prf as a buffer, you need to allocate as many characters as needed:
Code:
char* prf = (char*)malloc(sizeof(char) * n);
3rd, your method of reversing the vector is pretty stupid, and it doesn't even work since you are setting everything to pp[1]
I rewrote it, it's way easier to read and less complex.

Code:
	//// reverses the vector
	int *pp2 = (int*)malloc(n*sizeof(int));
	for(int i = 0; i < n; i++)
	{
		pp2[(n-1)-i] = pp[i];
	}
Lastly, you're not really using sprintf correctly. Each time you iterate through the for loop that contains sprintf you overwrite the buffer with the new value. Just do it like this:

Code:
	char* prf = (char*)malloc(sizeof(char)*n);
	for(int i = 0; i < n; i++)
	{
		prf[i] = pp2[i];
		printf("%d ",prf[i]);
	}
OR even better, you don't even need the buffer, and can directly print the reversed vector (really it's just an array though)

Here is the output after my changes:


Basically, malloc wasn't the problem, it was you using prf as a buffer for sprintf
On a final note: they invented the ++ operator for a reason. Use it please

Edit: Actually, to get the effect you're going after, you don't even need to do all of this. Just print the original array in reverse order
Alright I figured it out. That pp[1] was a typo I did notice it but was lazy to edit the post again lol. And we needed a function that can reverse an array and return it. Ikr it didn't return shit there but it was just to test that it could work. Also here's the code
Code:
void    PermbysVektor(int n)
{
        int *pp = (int*)malloc(n*32);
        for(int i = 0; i < n; i+=1)
        {
                int x;
                printf("Vendosi nje numer qe doni te shtoni tek vektori:");
                scanf("%d", &x);
                pp[i] = x;
        }
       
        int *pp2 = (int*)malloc(n*32);
        int l =0;
        for(int i = (n-1),
                f = 0; i >= 0,
                f < n; i-= 1,
                f += 1)
        {
                pp2[f] = pp[i];
        }
        system("pause");
}
As for the n*32, my brain was fucked and I forgot it's 32 bits not bytes. Alright I'm more of a .NET guy but still coding path is aaa really long piece of shit. Oh and I was in C and as far as I'm concerned there's was no "++" operator in C.
Quote Originally Posted by **Seals** View Post
Alright I figured it out. That pp[1] was a typo I did notice it but was lazy to edit the post again lol. And we needed a function that can reverse an array and return it. Ikr it didn't return shit there but it was just to test that it could work. Also here's the code
Code:
void    PermbysVektor(int n)
{
        int *pp = (int*)malloc(n*32);
        for(int i = 0; i < n; i+=1)
        {
                int x;
                printf("Vendosi nje numer qe doni te shtoni tek vektori:");
                scanf("%d", &x);
                pp[i] = x;
        }
       
        int *pp2 = (int*)malloc(n*32);
        int l =0;
        for(int i = (n-1),
                f = 0; i >= 0,
                f < n; i-= 1,
                f += 1)
        {
                pp2[f] = pp[i];
        }
        system("pause");
}
As for the n*32, my brain was fucked and I forgot it's 32 bits not bytes. Alright I'm more of a .NET guy but still coding path is aaa really long piece of shit. Oh and I was in C and as far as I'm concerned there's was no "++" operator in C.
of course there is a ++ operator in C.
C Operator Reference

I mainly program with the C89 standard of C, and I recommend you learn how to properly program with C89 as well. Here's why: Even though it's a really old standard, it's the only real "portable" kind. Most mainstream compilers, (except Microsoft's compiler) support C99 and some C11, but if you want to compile for anything like embedded systems, you'll have to stick with the C89 compilers, since they are the only ones that exist. Not to mention, most C development these days are done in C89 anyway, so you better learn it.

I'm saying this, because your code isn't C89 compliant at all. It would generate dozens of errors, because C89 doesn't allow mixed declarations and statements in a block. A block is anything between a { and }. So, you'd have to declare everything you use first, at the beginning of the block, and then use it later. This includes for statements, you can't do for(int i = 0; ...), you'd have to declare i before.
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?