473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

some pointer questions

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);
}
Nov 14 '05 #1
2 1050
kernel.lover <cr**********@gmail.com> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2


kernel.lover wrote:
hello,
why following is not working in c program?
#include <unistd.h>
This is not a Standard C header. <off-topic>
Besides, you don't need it. </off-topic>
#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. </off-topic>
#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.

--
Er*********@sun.com

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ross Micheals | last post by:
All I have some general .NET questions that I'm looking for some help with. Some of these questions (like the first) are ones that I've seen various conflicting information on, or questions that...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
12
by: Vibhajha | last post by:
Hi friends, My sister is in great problem , she has this exam of C++ and she is stuck up with some questions, if friends like this group provides some help to her, she will be very grateful....
18
by: free2cric | last post by:
Hi, I attanded an interview on C++ Question asked were and my answers to them.. 1. In a CPP program what does memory leak occure? -- i said.. In a constructor , the pointer variables were...
50
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
18
by: Matt | last post by:
I had these questions in an interview today: 1. Define a class using C. 2. Write a C functions that does garbage collection: frees all allocated memories. 3. Define semaphore. 4. What is...
19
by: ash | last post by:
hi friends, i have some questions whch is in my last year question papers.i need some help to get logic of these questions. 1) write a C function, that takes two strings as arguments and...
39
by: Digital Puer | last post by:
I'm not the world's greatest C++ programmer, so I had a hard time with these. Some help would be appreciated. 1. Comment on the declaration of function Bar() below: class Foo { static int...
10
by: weird0 | last post by:
I heard of two c# interview questions that i still clearly don't know and havent understood the concept. One reason is also that I havent worked upon them 1. What is the difference between a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.