Thread: struct access

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    struct access

    thanks it was answered.
    Last edited by complicato; 04-20-2008 at 08:41 PM. Reason: answered.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Once something is freed (and I'm assuming this works for kfree too), it doesn't exist anymore. So you immediately destroy your filename and filedata after you copy it in. It seems unlikely that that's what you want.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Bud you can't just go around allocating memory then comparing it as if it contains useful data. That is like a commandment of programming... Thou shalt not useth uninitialized data.

    Heh.. by the way, noticed how your program is behaving in an undefined way and you are practicing something that is undefined. Who would have thought

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    yeah i'm new to this malloc thing. I don't quite understand it. So how can i store and retrieve the data without losing it?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Don't free it?

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Look

    f_info[i].filename = (char*) kmalloc(fn_size+1, GFP_KERNEL);
    strcpy(f_info[i].filename, arg2);
    kfree(f_info[i].filename);
    Do you understand that ? You first allocated memory for the filename, then you copied arg2 into filename, and then you freed it right after that.

    It doesn't make any sense. So don't free it, as tabstop says.
    =========================================
    Everytime you segfault, you murder some part of the world

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    yeah i'm an idiot but i'm trying to learn. I'm really new to this malloc and not really savvy at c as you can tell :P

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's easy. Malloc simply gives you right to use some memory, somewhere in the memory. When you free it, you don't have access to it anymore.
    So don't free it until you don't need it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by complicato View Post
    yeah i'm an idiot but i'm trying to learn. I'm really new to this malloc and not really savvy at c as you can tell :P
    So don't mess about in kernel code... Or is kmalloc() not a Linux kernel malloc in this piece of code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    And another thing don't destroy the thread after you have an answer, it's rude and stupid.

    For example, if someone had already asked the same or similar question to yours, you wouldn't have to have asked... saving everyone's time.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    And another thing don't destroy the thread after you have an answer, it's rude and stupid.

    For example, if someone had already asked the same or similar question to yours, you wouldn't have to have asked... saving everyone's time.
    Yes, but consider the time saved by no one copying the broken code and thus spending hours figuring out how to fix it? ;-)

    [Seriously, yes, I agree. Don't ever edit the original post - it makes the whole thread quite useless].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic access to components of a struct
    By Kempelen in forum C Programming
    Replies: 1
    Last Post: 04-01-2009, 04:18 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. How can I access a struct (from a header file)?
    By loxslay in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2006, 01:25 PM
  5. Access a struct elements from...
    By Blackroot in forum C++ Programming
    Replies: 3
    Last Post: 08-26-2006, 05:00 AM