Pointers and Arrays in C??

T

Terence

I usually program in Java, so I am a bit confused about usint pointers in C.
I would really appreciate if someone can help me.

Case 1:
======

// Is this function no good? Since str is a variable created on stack,
// I am assuming it will be deallocated immedately after the function returns,
// right??
char * generateString()
{
char str[100] = "abc";
return str;
}

Case 2:
=====

// str is a pointer pointing to a literal
// Can I use str just like other char arrays?
// How are memory allocated for literals and when are they deallocated??
void main()
{
char * str;
str = "abc";
}

Case 3:
=====

// Similar to case 2 except the literal is returned from a function
// Again, can I use str for anything? When is it deleted from memory??
void main()
{
char * str;
str = generateString();
}

char * generateString()
{
return "abc";
}

Case 4:
=====

// assigning dynamically allocated array to pointer
void main()
{
char * str;
str = generateString();
...
delete str; // I have to delete str at the end, Right?
}

char * generateString()
{
char * charArray = new char[20];
strcpy(charArray, "abc");
return charArray;
}

Case 5:
=====

// str points to a char array created in a C library function
// How was this char array allocated? When will it be deallocated?
// Do I have to delete it explicitly??
void main()
{
char line[20] = "abc#def";
char * str;

str = strtok(line, "#");

delete str; // is this necessary?
// can I modify the content of the array??
}
 
A

Artie Gold

Terence said:
I usually program in Java, so I am a bit confused about usint pointers in C.
I would really appreciate if someone can help me.

Answered in (your use of `new' makes it OT on c.l.c).

Posting to multiple newsgroups is usually not a good idea -- and
when it is, crosspost, DO NOT multipost.

--ag
Case 1:
======

// Is this function no good? Since str is a variable created on stack,
// I am assuming it will be deallocated immedately after the function returns,
// right??
char * generateString()
{
char str[100] = "abc";
return str;
}

Case 2:
=====

// str is a pointer pointing to a literal
// Can I use str just like other char arrays?
// How are memory allocated for literals and when are they deallocated??
void main()
{
char * str;
str = "abc";
}

Case 3:
=====

// Similar to case 2 except the literal is returned from a function
// Again, can I use str for anything? When is it deleted from memory??
void main()
{
char * str;
str = generateString();
}

char * generateString()
{
return "abc";
}

Case 4:
=====

// assigning dynamically allocated array to pointer
void main()
{
char * str;
str = generateString();
...
delete str; // I have to delete str at the end, Right?
}

char * generateString()
{
char * charArray = new char[20];
strcpy(charArray, "abc");
return charArray;
}

Case 5:
=====

// str points to a char array created in a C library function
// How was this char array allocated? When will it be deallocated?
// Do I have to delete it explicitly??
void main()
{
char line[20] = "abc#def";
char * str;

str = strtok(line, "#");

delete str; // is this necessary?
// can I modify the content of the array??
}
 
C

Christian Bau

"Terence said:
I usually program in Java, so I am a bit confused about usint pointers in C.
I would really appreciate if someone can help me.

Your last two examples look more like C++ than C, and the main function
must be declared as returning int, not void. Apart from that:

1. Automatic variables disappear when you return from the function
containing them. Any pointer to such an auto variable becomes invalid,
and using it invokes undefined behavior.

2. Static and extern variables live as long as the program is running;
they never disappear. String literals like "abc" behave like static char
arrays; they live as long as the program is running and never disappear.
A pointer to a static or extern variable stays valid forever.

3. Calling free () with an argument that is the address of an auto,
static or extern variable invokes undefined behavior; most likely your
computer will crash rather soon.
 
K

Keith Thompson

Christian Bau said:
3. Calling free () with an argument that is the address of an auto,
static or extern variable invokes undefined behavior; most likely your
computer will crash rather soon.

Crashing your computer is certainly a possible consequence of
undefined behavior (as is making demons fly out of your nose), but in
a well-designed operating system you're far more likely to crash just
your application. (Discussion of which operating systems are
well-designed is off-topic.)
 
S

Sheldon Simms

I usually program in Java, so I am a bit confused about usint pointers in C.
I would really appreciate if someone can help me.

Case 1:

// Is this function no good?
char * generateString()
{
char str[100] = "abc";
return str;
}

You are correct, it is no good. The pointer returned will be invalid
because the array 'str' will no longer exist.
Case 2:

// Can I use str just like other char arrays?
// How are memory allocated for literals and when are they deallocated??
void main()

This is not correct. Use:

int main (void)
{
char * str;
str = "abc";
}

This is ok. String literals exist the entire time the program is
running. However, writing to string literals is a no-no.

Same as Case 2
Case 4:

// assigning dynamically allocated array to pointer
void main()

int main (void)
{
char * str;
str = generateString();
...
delete str; // I have to delete str at the end, Right?

There is no such thing as 'delete' in C. You meant to type

free(str);

You do not have to free the memory pointed to by str before your
memory ends if you don't want to make the memory available for
further allocation, but it is a good idea to do it anyway.
}

char * generateString()
{
char * charArray = new char[20];

This is no such thing as 'new' in C. You meant to type

char * charArray = malloc(20);

You must also check to make sure that the call to malloc
succeeded, like this:

if (charArray == NULL)
{
printf("memory allocation failed\n");
exit (EXIT_FAILURE);
}

Note that in order to use the NULL and EXIT_FAILURE macros
you need to #include <stdlib.h> before the use (the top of
the source file is a good place)

Dynamically allocated memory exists from the point of allocation
with malloc() until it is deallocated using free() (or until
your program ends).
strcpy(charArray, "abc");
return charArray;
}

Case 5:
=====

// str points to a char array created in a C library function
// How was this char array allocated? When will it be deallocated?
// Do I have to delete it explicitly??
void main()

{
char line[20] = "abc#def";
char * str;

str = strtok(line, "#");

delete str; // is this necessary?

You meant to type

free(str);

It is not necessary. In fact, it is very very wrong.
strtok() does not return any newly allocated memory. The pointers
it returns are pointers into your array.
// can I modify the content of the array??

Yes. It is your array line, which is modifiable.


-Sheldon
 
C

Christopher Benson-Manica

Terence said:
// Is this function no good? Since str is a variable created on stack,

FWIW, whether str ends up on a stack is implementation-dependent - some
implementations do not maintain stacks. In any case, you are correct about the
behavior of your example.
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top