some pointer questions

K

kernel.lover

hello,
why following is not working in c program?
#include <unistd.h>
#include <stdlib.h>

#define ONE_K (1024)

int main() {
char *some_memory;
char *scan_ptr;

some_memory = (char *)malloc(ONE_K);
if (some_memory == NULL) exit(EXIT_FAILURE);

scan_ptr = some_memory;
while(1) {
*scan_ptr = 'p'; //problem statement
scan_ptr++;
}

exit(EXIT_SUCCESS);
}

Also in following program what should be correct assignment to *some_memory ???
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>


int main() {
//char *some_memory = (char *)0;
char *some_memory = "\0";

printf("A read from null %s\n", some_memory);
sprintf(some_memory, "A write to null\n");

exit(EXIT_SUCCESS);
}
 
E

Eric Sosman

kernel.lover said:
hello,
why following is not working in c program?
#include <unistd.h>

This is not a Standard C header. <off-topic>
Besides said:
#include <stdlib.h>

#define ONE_K (1024)

int main() {
char *some_memory;
char *scan_ptr;

some_memory = (char *)malloc(ONE_K);
if (some_memory == NULL) exit(EXIT_FAILURE);

scan_ptr = some_memory;
while(1) {
*scan_ptr = 'p'; //problem statement
scan_ptr++;
}

This is an infinite loop, but the size of the
allocated memory is less than infinite. The first
1024 iterations fill that memory with 'p' characters,
and then the subsequent iterations try to store 'p'
outside the allocated region. Any such attempt invokes
undefined behavior, so there's no telling what might
happen.
exit(EXIT_SUCCESS);
}

Also in following program what should be correct assignment to *some_memory ???
#include <unistd.h>

Not a Standard header. <off-topic> And you still
don't need it. said:
#include <stdlib.h>
#include <stdio.h>


int main() {
//char *some_memory = (char *)0;

This would set `some_memory' to NULL, a pointer
value that "points nowhere."
char *some_memory = "\0";

This sets `some_memory' to point to the first
character of an anonymous two-character array, whose
two characters are '\0' and '\0'.
printf("A read from null %s\n", some_memory);

No problem here.
sprintf(some_memory, "A write to null\n");

Undefined behavior, for two reasons. First, the
anonymous arrays generated from string literals may be
read-only; any attempt to modify them produces undefined
behavior. Second, the array is only two characters long,
much too short for the seventeen characters you attempt
to store there.
exit(EXIT_SUCCESS);
}

I can't answer your question about what the "correct"
value of `some_memory' ought to be, because I do not know
what you are trying to accomplish. `double pi = 42.0;'
could be correct or incorrect, depending on what `pi' is
supposed to mean; one cannot decide without knowing the
intention.
 
J

Jens.Toerring

kernel.lover said:
why following is not working in c program?
#include <unistd.h>

Take care, that's not a standard C include file (and not needed
for your program).
#include <stdlib.h>
#define ONE_K (1024)
int main() {

You better make that

int main( void )

when you want to say that main() receives no arguments.
char *some_memory;
char *scan_ptr;
some_memory = (char *)malloc(ONE_K);
if (some_memory == NULL) exit(EXIT_FAILURE);
scan_ptr = some_memory;
while(1) {
*scan_ptr = 'p'; //problem statement
scan_ptr++;
}

You created an infinite loop here, so you don't stop after you filled
up the buffer you allocated and, in the process, overwrite all the
memory following the buffer. And that, of course, isn't allowed and
won't work. What do you think it would be doing?

BTW, you can shorten the loop body to

*scan_ptr++ = 'p';

doing the assignement and increment in a single line.
exit(EXIT_SUCCESS);
}
Also in following program what should be correct assignment to *some_memory
???

Sorry, but that question doesn't seem to make sense...
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
//char *some_memory = (char *)0;

I would be a bit careful with comments starting with "//" - not all
compilers support it.
char *some_memory = "\0";

That's fine, you assign the address of a literal string to the pointer.
Since you can't modify literal strings it might be prudent to make
that a 'const char *' instead.
printf("A read from null %s\n", some_memory);
sprintf(some_memory, "A write to null\n");

And here things go wrong. 'some_memory' is pointing to a literal
string, and you can't modify literal strings (think of them as
residing in read-only memory, even if on some platforms you can
get away with changing them). Moreover, even if you could, there
would not enough space for the string you want to store there.
exit(EXIT_SUCCESS);
}
Regards, Jens
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top