grvs 17 Junior Poster in Training

and to you all, I updated my profile, will be fun reading it...

enjoy

grvs 17 Junior Poster in Training

thanks happy geek

grvs 17 Junior Poster in Training

Hi Gaurav:
Welcome to the forum!

Thank you very much
Self help books..... are you selling those things ??

grvs 17 Junior Poster in Training

Glad to have you here!

thank you zandiago.... nice location -home :D

grvs 17 Junior Poster in Training

If I am getting you the right way, I don't think thats the case with many of us MIdi, and soon you too will be out of this trouble, as if u want to navigate to other objects, just do it before posting, (is there any difficulties in that?... u can save ur post at ur computer also)

But I still want editing allowed after 30 mins, may be not always but in some cases. Like in threads like Introduction etc.
eg. your interest might changes after sometime, or you just learned something you want people to know, or you want to correct something (as it was the case with me, mods did that for me, but not exactly in the way I wanted, and now I don't want to give them trouble again) so.... I guess, if its not very difficult to allow editing some posts forever, while keeping the 30 mins rule with all other posts, it should be done.

grvs 17 Junior Poster in Training

hi lajocar.... thats jin kazama.. my fav. character too... :)

grvs 17 Junior Poster in Training

right dude.... anyways... I will try a suggestion in feedback forum....

grvs 17 Junior Poster in Training

Thank you peter for removing url, and jasimp for giving me correct info about editing post, which I also got from feedback forum.

Nice to see that people like you replied to my post :)

grvs 17 Junior Poster in Training
int * readArray(int size){  
    // free(A); this when uncommented cause the first element to be zero, makes sense, but       
    // why other values are not lost
    return A; // Here i return the pointer. /* What are you returning here if you freed it before? */
}

well that was one of my question. Why other elements of array are displayed correctly? So may be I'll try to figure it out.

You want to free memory when you don't need it anymore. So, thinking this logically, you want to return a pointer of that memory to the calling function in main. If you freed that memory you are passing a pointer to some unpredictable piece of memory.
When you free some memory using function free(), it doesn't mean that memory automatically is zeroed out, it means now that memory is available for new use.
In essence your function may or may not return the right value, you left it to chance, since it could have been altered already.

May be here are hints for my answer. but If I had left it to chance then, why every time other elements are correctly displayed?

[edit:] I leave for you to figure where then you need to use free()

Well while i was trying to figure out, Salem solved it for me, and you explained, I couldn't stop the temptation to read Salem's and then your post... :-/ so now I can just say thank you both a lot.

grvs 17 Junior Poster in Training

I allocated memory to a pointer, which i had to return. So I don't know where to free it as its a local variable. I was trying to create an array of variable length (user-specified). I got some help for that in this forum only under this thread

here is the code

#include <stdio.h>

void printArray(int arr [], int size);
int * readArray(int size);

int main(){    
    int size;
    printf("Enter the size of the array ");
    scanf("%d",&size);    
    printArray(readArray(size),size);
    getchar();    
    return 0;
}

int * readArray(int size){
    int *A = malloc ( size * sizeof *A );    
    int i;
    for (i=0;i<size;i++){
        printf("\nEnter the element no. %d: ",i+1);
        scanf("%d",(A+i));
    }
    // free(A); this when uncommented cause the first element to be zero, makes sense, but       
    // why other values are not lost
    return A; // Here i return the pointer.
}

void printArray(int *arr, int size){
     int i=0;
     for (i=0; i<size;i++){
         printf("%d  ", *(arr++));
     }
     printf("\n");
     getchar();
}

Should I free it before returning it? If yes then how? Or should I free it in main, will it be known there ?

A sample Output of the program when I free pointer A before returning it is uploaded in image.

grvs 17 Junior Poster in Training

More on the differences - http://david.tribble.com/text/cdiffs.htm

> Just an integer pointer.
A is the pointer.
What malloc returns is in effect the array, when you access it via A (or any other variable who's type is int*)

Thanks doubt solved completely...


> new allocates memory in C++ and java, how its different from malloc?
Closely related to the topic. C do not have garbage collector, therefore, every time you call malloc to allocate some block of memory you must make sure that when that memory is not needed you release it by making a call to the function free().
Get to the habit of making sure that each call to malloc needs to be verified by checking that there was not problem reserving that block of memory.
e.g.
example

when I run the program as Mr. Salem said, I didn't free the memory, so a lot of thanks.

Not related to the topic, but some how not possible to dismiss are those call to scanf() to read an integer from user. I don't want to overwhelm you, but feeling a waste of time to learn something you are going to have to unlearn later; hence my intention to warn you now.
Don't use scanf() for reading from input. Verifying what the user enters is very hard, specially strings.
Some alternative example here.

yeah I got a little overwhelmed but I don't want bad habits …

grvs 17 Junior Poster in Training

>

int *A = malloc ( size * sizeof *A );

yups that took the input from user and displayed the output correctly, but if I am right no array was created. Just an integer pointer.
So we have a way to dynamically allocate user inputs at contiguous location but not exactly in an array. Do we ?
Also I want to know exactly why my previous program behaving strangely.

And about C hat and C++ hat, I'll Google and see what they are.

thanks

grvs 17 Junior Poster in Training

thank you guys...

grvs 17 Junior Poster in Training

I edited a post once and now i don't see option to edit it again. Is that a problem with my browser only(mozilla 2.0) or its some feature of DeniWeb. Problem occurred as I added links to my personal blog in my introduction, but later while viewing welcome guide I found this is against rule. Here is the post containing my introduction

Thanks.

grvs 17 Junior Poster in Training

wow... that tells something like autosavers for all websites might exits. I'll google, thanks for such prompt reply

grvs 17 Junior Poster in Training

Well I watched the welcome guide after i wrote this post, as a result i came to know that those links to other sites/personal stuff which doesn't help answering a post are discouraged. So please bear with me till I find a way to remove this link. I used the edit post option once and now its no more there. I hope to find a solution soon enough

grvs 17 Junior Poster in Training

Hi all

This is my first post to feedback forum. Its the best forum I have ever been. :)

Also I was wondering, is there anything in DeniWeb which autosaves the posts (like gmail) while we type? If not is thee any plan to include that.

grvs 17 Junior Poster in Training

Hi All,
My second post in the forum, first was ofcourse about the question that made me join this forum. That reveals something about me...

Name: Gaurav Singhal
Nicks: grvs, goru, chhote, gharsingh.... (list goes on atleast 10, how much people love me ;) )
Age: 23
Location: Delhi, India
Interest: Maths, Games, puzzles, Cricket, Soccer, Tennis...

for more on me you can visit my <URL snipped>Orkut Profile or my <URL snipped> (blog will be a little fun I guess)
Thats all, I guess I can update this whenever I want....

cheers

grvs 17 Junior Poster in Training

Hey Narue,
I posted a thread twice, reason is explained in second among them. So please delete it.
links are
second post delete this please
first post

Thanks.

grvs 17 Junior Poster in Training

In C,how to define an array which is of a length specified by the user?
I tried writing a code, but outputs were strange. Sometimes first two elements are correctly displayed, sometimes only first. Other seems some random values (extremely large sometimes). May be the problem is in initializing the pointer. First I didn't use the pointer p in readArray() function. I don't really know why I am using it now, or whether there should be a separate pointer when &A[0] is same as A.

here is the code

#include <stdio.h>

void printArray(int arr [], int size);
int * readArray(int size);

int main(){    
    int size; // size of array to be given by user
    printf("Enter the size of the array ");
    scanf("%d",&size);    
    printArray(readArray(size),size);
    getchar();    
    return 0;
}

int * readArray(int size){
    int A [size], i, *p; 
    p=&A[0];
    for (i=0;i<size;i++){
        printf("\nEnter the element no. %d: ",i+1);
        scanf("%d",(p+i));        
        A[i]=*(p+i);
    }
    return &A[0]; 
}

void printArray(int *arr, int size){
     int i=0;
     for (i=0; i<size;i++){
         printf("%d  ", *(arr++));
     }
     printf("\n");
     getchar();
}

I searched and couldn't get a solution. I got some things for C++ like this but not for C. Is there anything like new in C? (new allocates memory in C++ and java, how its different from malloc?)

PS: This is my first post in this forum, though I went through posting instruction, tell me if I should keep something in mind. Also I have done coding in Java for like 1 year, so don't hesitate …

Salem commented: Congratulations! postcount==1 && useCodeTags => +ve rep +17