This code snippet is an exercise on allocating two dimension array
dynamically. Though this one is trivial, is it a correct one?
Furthermore, when I tried to make these changes to the original
example:
for (i = 0; i < ROW + 2; ++i){ /*line 14*/
strcpy(array[i], "C! C!"); /*line 15*/
Apparently, the modified code wrote to the memory unit outside the
allocated array, but the program ran well and there was no Segmentation
fault. Thank you.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define ROW 4
#define COL 3
int main(void){
char (*array)[COL];
int i;
array = malloc(ROW * sizeof *array);
if (array){
for (i = 0; i < ROW; ++i){ /*line 14*/
strcpy(array[i], "C!"); /*line 15*/
printf("%s\n", array[i]);
}
}
free(array);
return 0;
} 27 3239
lovecreatesbea. ..@gmail.com wrote:
strcpy(array[i], "C!"); /*line 15*/
printf("%s\n", array[i]);
strcpy doesn't nul terminate, so your call to printf will fault.
Op 3 Sep 2006 10:28:48 -0700 schreef bw*****@yahoo.c om:
lovecreatesbea. ..@gmail.com wrote:
> strcpy(array[i], "C!"); /*line 15*/ printf("%s\n", array[i]);
strcpy doesn't nul terminate, so your call to printf will fault.
strcpy does, memcpy doesn't.
printf won't fail here.
--
Coos bw*****@yahoo.c om wrote:
lovecreatesbea. ..@gmail.com wrote:
> strcpy(array[i], "C!"); /*line 15*/ printf("%s\n", array[i]);
strcpy doesn't nul terminate,
What on earth makes you think that? strcpy is a *string* copy, so of
course it is defined to copy the null termination, otherwise it would be
called something like,
CopyJustTooLitt leOfAStringToBe Usefull99Percen tOfTheTimeOrMor e.
so your call to printf will fault.
Even if you were write about strcpy (which you are not) that would still
not guarantee that the call to printf would fault.
--
Flash Gordon
lovecreatesbea. ..@gmail.com wrote:
This code snippet is an exercise on allocating two dimension array
dynamically. Though this one is trivial, is it a correct one?
Furthermore, when I tried to make these changes to the original
example:
for (i = 0; i < ROW + 2; ++i){ /*line 14*/
strcpy(array[i], "C! C!"); /*line 15*/
Apparently, the modified code wrote to the memory unit outside the
allocated array, but the program ran well and there was no Segmentation
fault. Thank you.
Excuse me mister policeman, but why are you telling me I should not
drive with my eyes close? I just managed to drive 100 yards down the
road with my eyes closed and I didn't crash!
Just because you do something wrong it does not guarantee a crash. All
it guarantees is that you have done something wrong.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define ROW 4
#define COL 3
int main(void){
char (*array)[COL];
int i;
array = malloc(ROW * sizeof *array);
if (array){
for (i = 0; i < ROW; ++i){ /*line 14*/
strcpy(array[i], "C!"); /*line 15*/
printf("%s\n", array[i]);
}
}
free(array);
return 0;
}
For more flexible approaches I suggest you read question 6.16 of the
comp.lang.c FAQ.
--
Flash Gordon
Coos Haak wrote:
Op 3 Sep 2006 10:28:48 -0700 schreef bw*****@yahoo.c om:
lovecreatesbea. ..@gmail.com wrote:
strcpy(array[i], "C!"); /*line 15*/
printf("%s\n", array[i]);
strcpy doesn't nul terminate, so your call to printf will fault.
strcpy does, memcpy doesn't.
printf won't fail here.
You mis-read my comment. The OP only allocated array[0], so the
designation
array[1] through array[3] has not be allocated. Therefore, when you
strcpy, there is no way you can nul terminate since the designation is
smaller than the string being copied.
Here's one that will work since I have allocated the array each
time through the for loop. And strcpy will nul terminate because
the destination is big enough to hold the nul terminator.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void) {
char *string[20];
int i;
for (i = 0; i < 4; i++) {
string[i] = calloc(6, sizeof(char));
strcpy(string[i], "hello");
printf("%s\n", string[i]);
}
for (i = 0; i < 4; i++) {
free(string[i]);
}
return 0;
}
Flash Gordon wrote:
lovecreatesbea. ..@gmail.com wrote:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define ROW 4
#define COL 3
int main(void){
char (*array)[COL];
int i;
array = malloc(ROW * sizeof *array);
if (array){
for (i = 0; i < ROW; ++i){ /*line 14*/
strcpy(array[i], "C!"); /*line 15*/
printf("%s\n", array[i]);
}
}
free(array);
return 0;
}
For more flexible approaches I suggest you read question 6.16 of the
comp.lang.c FAQ.
Isn't the approach in my code example flexible enough? Isn't it correct?
Isn't the approach in my code example flexible enough? Isn't it correct?
No, it's not. For one,
char (*array)[COL];
Is errorneous. It is different from:
char *array[COL];
You should know that [] precedes the * operator. Insted of creating an
array of pointers to characters, you created a pointer to an array of
characters. See the difference?
Vardhan
lovecreatesbea. ..@gmail.com wrote:
This code snippet is an exercise on allocating two dimension array
dynamically. Though this one is trivial, is it a correct one?
Yes, its a 2 dimensional storage used for holding a 1 dimensional array
of C strings, being filled then output one at a time.
Furthermore, when I tried to make these changes to the original
example:
for (i = 0; i < ROW + 2; ++i){ /*line 14*/
strcpy(array[i], "C! C!"); /*line 15*/
Apparently, the modified code wrote to the memory unit outside the
allocated array, but the program ran well and there was no Segmentation
fault. Thank you.
Yes, this is called a buffer overflow. array is defined for ROW (== 4)
rows only, not ROW+2 rows. Furthermore each row can hold at most 3
characters, even though storing "C! C!" requires 6 characters. You can
read more about this condition here: http://en.wikipedia.org/wiki/Buffer_overflow
Just because there happened to be valid memory past the end of your
well defined objects doesn't mean that accessing them is valid. The
lack of a Segmentation fault is not proof of correctness. (In fact
more buffer overflows in real world programs do not lead to immediate
Segmentation Faults.)
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define ROW 4
#define COL 3
int main(void){
char (*array)[COL];
int i;
array = malloc(ROW * sizeof *array);
if (array){
for (i = 0; i < ROW; ++i){ /*line 14*/
strcpy(array[i], "C!"); /*line 15*/
printf("%s\n", array[i]);
}
}
free(array);
return 0;
}
--
Paul Hsieh http://www.pobox.com/~qed/ http://bstring.sf.net/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: nkw |
last post by:
Hi,
I'm new to C and I'm stuck in working out the logic code for a cyclic
array.
I have an array of 5 cells, starting at index cell 0 and last cell is
4.
ie
0 1 2 3 4 0 1 ..... and so on
|
by: Sameer |
last post by:
Hi friends,
I am using Mandriva Linux 9.2 and gcc.
My source code is,
int chunkin ; //no error
int i ;
for (i=0;i<7225;i++)
{
chunkin = somedata ;
|
by: manoharyes |
last post by:
hi experts,
i am getting a segmentation fault error in this piece of code..
node.c has some contents.
my intension is to read the contents of a file into an array then..
dynamically allocate memory to a character pointer to store contents of
that file.. i am doing this because the contents of the file may change
for each "read" operation. and i will use this poiter(dynamically
allocated) for sendto() call.
please help..
thnx in advance.
|
by: madunix |
last post by:
My Server is suffering bad lag (High Utlization)
I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2
Web visitors retrieve data from the web by php calls through oci
cobnnection from 10g release2
PHP is configured with the following parameters
'./configure' '--prefix=/opt/oracle/php'
'--with-apxs=/opt/oracle/apache/bin/apxs'
'--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode'
'--enable-session'...
|
by: hobbes992 |
last post by:
Howdy folks,
I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
| |
by: Peterwkc |
last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back.
By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this?
Second Program:
/* Best Method to allocate memory for 2D Array because it's
...
|
by: Jess |
last post by:
Hello, the following code failed with "segmentation fault". Could
someone tell me why it failed please? Thanks!
#include<iostream>
#include<string>
#include<cstring>
#include<cstddef>
using namespace std;
|
by: ptq2238 |
last post by:
Hi, I'm getting confused with arrays and hope someone can shed light
on my code.
I wrote this to try and learn about files and arrays as I thought if I
could grab each element and place them into an array I can manipulate
the stings from the file with array indexes.
Perhaps there's a better method but I'm learning.
#include <stdio.h>
#include <stdlib.h>
|
by: Anna |
last post by:
Could you please help me? I got a segmentation fault message while
trying to assign a pointer = pointer like this:
bufferlist=(buffer_t*)buffernew;
What's the error by doing this? Here is the full C script of what I
did. I would be really really appreciate your help. I need to finish
this code by monday but i'm stuck at this point and can't solve it :-(
Thank you very much
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |