Newbee array pointer question

M

Martin Andert

Why ist the following code not working?
I want to initialize a string in function, but I am
getting those STATUS_ACCESS_VIOLATION exceptions.


#include <stdio.h>
#include <stdlib.h>

void init(char* buffer)
{
buffer = (char*) malloc(6 * sizeof(char));
buffer[0] = 'H';
buffer[1] = 'e';
buffer[2] = 'l';
buffer[3] = 'l';
buffer[4] = 'o';
buffer[5] = '\0';
}

int main()
{
char* buffer;
init(buffer);
printf("%s", buffer);
}


Any workaround or how to for this?
TIA,
Martin
 
J

Joona I Palaste

Martin Andert said:
Why ist the following code not working?
I want to initialize a string in function, but I am
getting those STATUS_ACCESS_VIOLATION exceptions.

#include <stdio.h>
#include <stdlib.h>
void init(char* buffer)
{
buffer = (char*) malloc(6 * sizeof(char));

This is a very common newbie problem. C parameter passing is strictly
by value, even if the parameter's type is a pointer. Therefore you're
only modifying the init() function's local copy of buffer here. Try
passing a pointer to buffer instead.
 
C

CBFalconer

Martin said:
Why ist the following code not working?
I want to initialize a string in function, but I am
getting those STATUS_ACCESS_VIOLATION exceptions.

#include <stdio.h>
#include <stdlib.h>

void init(char* buffer)

char *init(void)
{
char *buffer;

so you don't create a memory leak, and can return the pointer.
{
buffer = (char*) malloc(6 * sizeof(char));

buffer = malloc(6);

suffices and is better. By definition sizeof char is 1.
buffer[0] = 'H';
buffer[1] = 'e';
buffer[2] = 'l';
buffer[3] = 'l';
buffer[4] = 'o';
buffer[5] = '\0';

return buffer;
}

int main()
{
char* buffer;
init(buffer);

buffer = init();
printf("%s", buffer);
}

Any workaround or how to for this?

See above.
 
P

pete

Martin said:
Why ist the following code not working?
I want to initialize a string in function, but I am
getting those STATUS_ACCESS_VIOLATION exceptions.

#include <stdio.h>
#include <stdlib.h>

void init(char* buffer)
{
buffer = (char*) malloc(6 * sizeof(char));
buffer[0] = 'H';
buffer[1] = 'e';
buffer[2] = 'l';
buffer[3] = 'l';
buffer[4] = 'o';
buffer[5] = '\0';
}

int main()
{
char* buffer;
init(buffer);
printf("%s", buffer);
}

Any workaround or how to for this?


#include <stdio.h>
#include <stdlib.h>

void init(char **buffer)
{
*buffer = malloc(6 * sizeof **buffer);
if (*buffer != NULL) {
buffer[0][0] = 'H';
buffer[0][1] = 'e';
buffer[0][2] = 'l';
buffer[0][3] = 'l';
buffer[0][4] = 'o';
buffer[0][5] = '\0';
}
}

int main(void)
{
char *buffer;

init(&buffer);
if (buffer != NULL) {
printf("%s\n", buffer);
free(buffer);
}
return 0;
}
 
H

Herbert Rosenau

Why ist the following code not working?
I want to initialize a string in function, but I am
getting those STATUS_ACCESS_VIOLATION exceptions.

It works perfectly as designed.
#include <stdio.h>
#include <stdlib.h>

void init(char* buffer)

You receives the address of a buffer here.

You should change the interface to this function as you has to deliver
a pointer here.

void *init(void)

{
buffer = (char*) malloc(6 * sizeof(char));

Now you overreides the local copy of the bufferaddress. By that: the
cast is superflous. You tries to hide the possible case that you miss
the prototype of malloc, going into the lands of undefined behavior.
But even here as you have included stdlib.h the cast does nothing.

sizeof char is always and under any circumstance 1. Mulitplies you in
reality each and anyxthing with one? No? Why not? You should do that
as you does in your applications.

malloc() can fail. It indicates this by returning a NULL pointer
constant. It is on you to check that and act on the error.

buffer[0] = 'H';
buffer[1] = 'e';
buffer[2] = 'l';
buffer[3] = 'l';
buffer[4] = 'o';
buffer[5] = '\0';

You fille the malloced buffer.

But NOW you looses the address of the allocated buffer, producing a
memory leak.
}

int main()
{
char* buffer;
init(buffer);

As you had changed the interface
buffer = init();
printf("%s", buffer);
}


Any workaround or how to for this?

You creates a local pointer and gives its currently undefined content
to a function that does nothing with that but overwrites it with an
address it gets from malloc. As your function returns nothing it can't
change the content of your local pointer - so you works in any way
with an uninitialised pointer, having produced a memory leak as the
address you gots from malloc gets loose during return from init().
 
E

Excluded_Middle

Try This

#include <stdio.h>
#include <stdlib.h>

void init(char * buffer)
{
int i = 0;
*buffer = 'H';
*(buffer + (++i))= 'e';
*(buffer + (++i))= 'l';
*(buffer + (++i))= 'l';
*(buffer + (++i))= 'o';
*(buffer + (++i))= '\0';
}

int main()
{
char* buffer;
buffer = (char*) malloc(6 * sizeof(char));
if(buffer == NULL){
printf("cannot allocate memory\n");
exit(1);
}

init(buffer);
printf("string is %s", buffer);
}
 
P

pete

Excluded_Middle said:
#include <stdlib.h>
char* buffer;
buffer = (char*) malloc(6 * sizeof(char));

That cast isn't needed in C, and could have supressed a warning
if you had forgotten to include stdlib.h

This newsgroup's idiomatic way of dynamically allocating
memory for an array of any type is:
#include <stdlib.h>
pointer = malloc(nmemb * sizeof *pointer);

sizeof(char) is always equal to one, so for strings
pointer = malloc(string_length + 1);
and
pointer = malloc(sizeof "string literal");
are also good ways.


That (1) doesn't have a portable meaning in C.
 
J

John Bode

Why ist the following code not working?
I want to initialize a string in function, but I am
getting those STATUS_ACCESS_VIOLATION exceptions.


#include <stdio.h>
#include <stdlib.h>

void init(char* buffer)
{
buffer = (char*) malloc(6 * sizeof(char));
buffer[0] = 'H';
buffer[1] = 'e';
buffer[2] = 'l';
buffer[3] = 'l';
buffer[4] = 'o';
buffer[5] = '\0';
}

int main()
{
char* buffer;
init(buffer);
printf("%s", buffer);
}


Any workaround or how to for this?
TIA,
Martin

If you need to write to a parameter, then you need to pass a pointer
to it. In this case, you want to modify a pointer value, so you pass
a pointer to it:

void init(char **buffer)
{
*buffer = malloc(6);
(*buffer)[0] = 'H';
(*buffer)[1] = 'e';
/* yadda yadda yadda */
}

int main (void)
{
char *buffer;
init(&buffer);
printf("%s\n", buffer);
return 0;
}

Although Joona's method (returning the pointer value) is preferable.
In case malloc() fails, it will return NULL. That way, you can test
to make sure the malloc() succeeded before trying to access the
buffer:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *init(void)
{
char *buf = malloc(6);
if (buf)
{
strcpy (buf, "Hello");
}

return buf;
}

int main(void)
{
char *buffer = init();
if (buffer)
printf ("%s\n", buffer);
else
printf ("malloc() failed in init()!\n");

return 0;
}
 
J

Joona I Palaste

John Bode said:
Although Joona's method (returning the pointer value) is preferable.
In case malloc() fails, it will return NULL. That way, you can test
to make sure the malloc() succeeded before trying to access the
buffer:

Surely you mean Herbert's method? Mine was indeed passing a pointer to
a pointer.
 
C

CBFalconer

Joona said:
Surely you mean Herbert's method? Mine was indeed passing a
pointer to a pointer.

Poking around, AFAICS I was the only one suggesting returning the
pointer.
 
J

John Bode

CBFalconer said:
Poking around, AFAICS I was the only one suggesting returning the
pointer.

This is what I get for posting before I'm actually awake.

But it's still the right thing to do, no matter who suggested it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top