By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,111 Members | 1,640 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,111 IT Pros & Developers. It's quick & easy.

Newbee array pointer question

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Martin Andert <ma***********@gmx.de> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"No, Maggie, not Aztec, Olmec! Ol-mec!"
- Lisa Simpson
Nov 14 '05 #2

P: n/a
Martin Andert wrote:

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #3

P: n/a
Martin Andert wrote:

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;
}

--
pete
Nov 14 '05 #4

P: n/a
On Mon, 1 Nov 2004 17:56:41 UTC, ma***********@gmx.de (Martin Andert)
wrote:
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().

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation

Nov 14 '05 #5

P: n/a
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);
}
Nov 14 '05 #6

P: n/a
Excluded_Middle wrote:
#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.

exit(1);


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

--
pete
Nov 14 '05 #7

P: n/a
ma***********@gmx.de (Martin Andert) wrote in message news:<9a**************************@posting.google. com>...
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;
}
Nov 14 '05 #8

P: n/a
John Bode <jo*******@my-deja.com> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"A bicycle cannot stand up by itself because it's two-tyred."
- Sky Text
Nov 14 '05 #9

P: n/a
Joona I Palaste wrote:
John Bode <jo*******@my-deja.com> scribbled the following:
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.


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

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #10

P: n/a
CBFalconer <cb********@yahoo.com> wrote in message news:<41***************@yahoo.com>...
Joona I Palaste wrote:
John Bode <jo*******@my-deja.com> scribbled the following:
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.


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.
Nov 14 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.